spring mvc 批量上传+文件上传

spring mvc 批量上传+文件上传

简单3步走。搞定!

上传文件成功后:



1 上传文件核心方法

public static String saveWebImgFile(MultipartFile imgFile){ 		String webFilePath = ""; 		if(imgFile.getSize() > 0 && isImage(imgFile.getContentType())){ 			FileOutputStream fos = null; 			try { 				byte[] b = imgFile.getBytes(); 				/* 构造文件路径 */ 				String webRoot = PathUtil.classPath(); 				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 				SimpleDateFormat sdf2 = new SimpleDateFormat("HHmmssSSS"); 				String datePath = sdf.format(new Date()); 				String datePath2 = sdf2.format(new Date()); 				String dirPath = webRoot + "/../../upload/img/" + datePath; 				File dir = new File(dirPath); 				if(!dir.exists()){ 					dir.mkdirs(); 				} 				String fileName = imgFile.getOriginalFilename(); 				String filePath = dirPath + "/" + datePath2 + getImgSuffix(fileName); 				webFilePath = "/upload/img/" + datePath + "/" + datePath2 + getImgSuffix(fileName); 				File file = new File(filePath); 				 				dir.setWritable(true); 				file.setWritable(true); 				 				fos = new FileOutputStream(file); 				fos.write(b); 				 			} catch (IOException e) { 				throw new RuntimeException("文件上传失败!" ,e); 			}finally{ 				if(fos != null){ 					try { 						fos.close(); 					} catch (IOException e) { 						throw new RuntimeException("文件上传->输出流关闭失败!!!!" ,e); 					} 				} 			} 		} 		 		return webFilePath; 	}



2控制器代码

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



3 页面代码

 <body>     	商品图片设置:    	<form method="post"  enctype="multipart/form-data" action="product/imgupload" name="imagefrm"> 	   	<table> 	   		<tr> 	   		<td>商品大图:默认宽度600px,高度600px</td> 	   		</tr> 	   		<tr> 	   		<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> 	   		</tr> 	   		<tr> 	   		<td>添加多角度图片:默认宽度50px,高度50px</td> 	   		</tr> 	   		<tr> 	   		<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> 	   		</tr> 	   		<tr> 	   		<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> 	   		</tr> 	   		<tr> 	   		<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> 	   		</tr> 	   		<tr> 	   		<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> 	   		</tr> 	   		<tr> 	   		<td><input type="submit" value="上传所有图片"/></td>  	   		</tr> 	   	</table> 	</form>   </body>






      本文转自yjflinchong 51CTO博客,原文链接:http://blog.51cto.com/yjflinchong/1165010,如需转载请自行联系原作者




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值