首页 > 代码库 > 上传图片
上传图片
********
1,jsp页面
2,后台struts的action中
public boolean saveAttach(TBasePhotoAttach photo) throws Exception{ /*System.out.println(upload1FileName); //flower.jpg System.out.println(upload1); System.out.println(upload1ContentType); //image/pjpeg */
//只能上传这些格式的图片
if(!"image/jpeg".equals(upload1ContentType) &&!"image/gif".equals(upload1ContentType) &&!"image/pjpeg".equals(upload1ContentType)){ return false; }
//图片名 String photoName = upload1FileName.substring(0,upload1FileName.lastIndexOf("."));
//图片后缀 String suffix=upload1FileName.substring(upload1FileName.lastIndexOf(".")+1); SimpleDateFormat format=new SimpleDateFormat("yyyyMMddhhmmss"); //使用当前时间为图片命名 String newName=format.format(new Date())+"."+suffix; //用于保存上传照片的文件夹 String uploadPath=this.getRequest().getRealPath("/")+"uploadImages"; // E:JavaWorkspace dp WebRoot uploadImages File file=new File(uploadPath); if(!file.exists()){ file.mkdirs(); } String saveUrl=uploadPath +"\\"+ newName; //完整保存路径 //文件输入流 FileInputStream fis = new FileInputStream(upload1); //文件输出流 FileOutputStream fos = new FileOutputStream(saveUrl, false); byte buffer[] = new byte[4096]; int readSize = 0; //存储文件 while ((readSize = fis.read(buffer)) > 0) { fos.write(buffer, 0, readSize); } //photo.setPhotoUrl(saveUrl); //URLEncoder.encode(upload1FileName, "UTF-8"); //DATE.getCurrentDate().toString() String photoUrl=this.getRequest().getContextPath()+"/uploadImages/"+newName; photo.setPhotoUrl(photoUrl); photo.setOldname(upload1FileName); photo.setNewname(newName); stuService.savePhoto(photo); return true; }
*************
上传图片
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。