spring mvc 批量上传+文件上传

来源:互联网 发布:pos机小票打印软件 编辑:程序博客网 时间:2024/05/16 06:11

spring mvc 批量上传+文件上传

简单3步走。搞定!

上传文件成功后:



1 上传文件核心方法

  1. public static String saveWebImgFile(MultipartFile imgFile){  
  2.         String webFilePath = "";  
  3.         if(imgFile.getSize() > 0 && isImage(imgFile.getContentType())){  
  4.             FileOutputStream fos = null;  
  5.             try {  
  6.                 byte[] b = imgFile.getBytes();  
  7.                 /* 构造文件路径 */  
  8.                 String webRoot = PathUtil.classPath();  
  9.                 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");  
  10.                 SimpleDateFormat sdf2 = new SimpleDateFormat("HHmmssSSS");  
  11.                 String datePath = sdf.format(new Date());  
  12.                 String datePath2 = sdf2.format(new Date());  
  13.                 String dirPath = webRoot + "/../../upload/img/" + datePath;  
  14.                 File dir = new File(dirPath);  
  15.                 if(!dir.exists()){  
  16.                     dir.mkdirs();  
  17.                 }  
  18.                 String fileName = imgFile.getOriginalFilename();  
  19.                 String filePath = dirPath + "/" + datePath2 + getImgSuffix(fileName);  
  20.                 webFilePath = "/upload/img/" + datePath + "/" + datePath2 + getImgSuffix(fileName);  
  21.                 File file = new File(filePath);  
  22.                   
  23.                 dir.setWritable(true);  
  24.                 file.setWritable(true);  
  25.                   
  26.                 fos = new FileOutputStream(file);  
  27.                 fos.write(b);  
  28.                   
  29.             } catch (IOException e) {  
  30.                 throw new RuntimeException("文件上传失败!" ,e);  
  31.             }finally{  
  32.                 if(fos != null){  
  33.                     try {  
  34.                         fos.close();  
  35.                     } catch (IOException e) {  
  36.                         throw new RuntimeException("文件上传->输出流关闭失败!!!!" ,e);  
  37.                     }  
  38.                 }  
  39.             }  
  40.         }  
  41.           
  42.         return webFilePath;  
  43.     }  



2控制器代码

  1. @RequestMapping(value = "/imgupload")  
  2.      public String handleFormUpload(MultipartHttpServletRequest request) throws Exception{    
  3.          List<MultipartFile> files = request.getFiles("file");  
  4.          Map<String, String> imgs = new HashMap<String, String>();  
  5.          for (int i = 0; i < files.size(); i++) {  
  6.             String webpath = FileUtil.saveWebImgFile(files.get(i));  
  7.             imgs.put("img"+i, webpath);  
  8.         }  
  9.         request.setAttribute("imgs", imgs);  
  10.         return showImageList(request);  
  11.      }  



3 页面代码

  1. <body>  
  2.     商品图片设置:  
  3.     <form method="post"  enctype="multipart/form-data" action="product/imgupload" name="imagefrm">  
  4.     <table>  
  5.         <tr>  
  6.         <td>商品大图:默认宽度600px,高度600px</td>  
  7.         </tr>  
  8.         <tr>  
  9.         <td><input type="file" name="file"/><c:if test="${imgs.img0!=null&&imgs.img0!=''}"><img src="<%=basePath %>${imgs.img0 }" style="width:40;height:40px;"/></c:if></td>  
  10.         </tr>  
  11.         <tr>  
  12.         <td>添加多角度图片:默认宽度50px,高度50px</td>  
  13.         </tr>  
  14.         <tr>  
  15.         <td><input type="file" name="file"/><c:if test="${imgs.img1!=null&&imgs.img1!=''}"><img src="<%=basePath %>${imgs.img1 }" style="width:40;height:40px;"/></c:if></td>  
  16.         </tr>  
  17.         <tr>  
  18.         <td><input type="file" name="file"/><c:if test="${imgs.img2!=null&&imgs.img2!=''}"><img src="<%=basePath %>${imgs.img2 }" style="width:40;height:40px;"/></c:if></td>  
  19.         </tr>  
  20.         <tr>  
  21.         <td><input type="file" name="file"/><c:if test="${imgs.img3!=null&&imgs.img3!=''}"><img src="<%=basePath %>${imgs.img3 }" style="width:40;height:40px;"/></c:if></td>  
  22.         </tr>  
  23.         <tr>  
  24.         <td><input type="file" name="file"/><c:if test="${imgs.img4!=null&&imgs.img4!=''}"><img src="<%=basePath %>${imgs.img4 }" style="width:40;height:40px;"/></c:if></td>  
  25.         </tr>  
  26.         <tr>  
  27.         <td><input type="submit" value="上传所有图片"/></td>   
  28.         </tr>  
  29.     </table>  
  30. </form>  
  31.  </body>  
0 0
原创粉丝点击