多文件自定义文件夹上传图片
JSP页面
<form role="form" style="padding:20px;" action="uploadFiles.do" method="post" enctype="multipart/form-data">
<input type="hidden" value="${sessionScope.user.username}" name="uname"/>
<h1>上传资料</h1>
<div class="form-group">
<label for="exampleInputEmail1">营业执照</label> <input
type="file" class="form-control" name="files"/>
<label for="exampleInputEmail1">组织机构</label><input
type="file" class="form-control" name="files"/>
</div>
<input type="submit" class="btn btn-default" value="上传" />
</form>
Controller<pre name="code" class="java">public boolean uploadFile(MultipartFile file,String uname){
if(!file.isEmpty()){
String filePath = "F://upload//"+uname+"//"
+ file.getOriginalFilename();
File fi = new File("F://upload//"+uname);
if(!fi.exists()){
fi.mkdir();
}
if(!fi.isDirectory()){
throw new IllegalArgumentException("不是目录");
}
try {
file.transferTo(new File(filePath));
return true;
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
//多文件上传
@RequestMapping("/uploadFiles.do")
public String uploadFiles(@RequestParam("files") MultipartFile[] files,String uname){
//判断file数组不能为空并且长度大于0
if(files!=null&&files.length>0){
//循环获取file数组中得文件
for(int i = 0;i<files.length;i++){
MultipartFile file = files[i];
//保存文件
uploadFile(file,uname);
}
}
// 重定向
return "redirect:supermarket.jsp";
}applicationContext.xml
<!-- 用于上传 -->
<span style="white-space:pre"> </span><bean id="multipartResolver"
<span style="white-space:pre"> </span>class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<span style="white-space:pre"> </span><!-- 设置上传文件大小的参数 -->
<span style="white-space:pre"> </span><property name="maxUploadSize" value="1000000" />
<span style="white-space:pre"> </span></bean>
本文介绍了如何使用SpringMvc在JavaWeb应用中实现多文件上传,并将图片保存到自定义文件夹。涉及到的技术包括JSP页面交互和文件处理。
1033

被折叠的 条评论
为什么被折叠?



