1、上传文件夹,提取文件夹中多个文件;若文件夹中存在目录,也会提取目录中的文件;
html处理方式:
<input type="file" name="file" multiple="multiple" webkitdirectory>
function getImpImageTpl() {
var tpl = \'<div style="padding: 10px;">\' +
\'<form id="impForm" action="<%=basePath%>commericalPart/importModelPart" method="POST" enctype="multipart/form-data" target="impFrame">\' +
\'<iframe name="impFrame" style="display: none;"></iframe>\' +
\'<table id="impTable" class="table">\' +
\'<tr>\' +
\'<td>选择文件:</td>\' +
\'<td><input type="file" name="file" multiple="multiple" webkitdirectory></td>\' +
\'</tr>\' +
\'<tr>\' +
\'<td>导入配件图片的目录</td>\' +
\'<td><input type="text" name="imagePath" ></td>\' +
\'</tr>\' +
\'<tr>\' +
\'<td></td>\' +
\'<td><input type="button" value="开始导入" class="btn btn-primary" id="submitForm" onclick="doStartImpImage();" ></td>\' +
\'</tr>\' +
\'</table>\' +
\'</form>\' +
\'</div>\';
return tpl;
}
后台接收:
/**
* 导入配件图片
* @return
*/
@RequestMapping("/importPartImage")
@ResponseBody
public Object importPartImage(@RequestParam("file") List<MultipartFile> file,String imagePath){
Map<String,Object> map = new HashMap<String, Object>();
String filePath = "E:/lazyli/image/part/" + imagePath+"/";
File file2 = new File(filePath);
if(!file2.exists()){
file2.mkdirs();
}
OutputStream os = null;
String exceptionImage = "";
try{
for(MultipartFile file1 : file){
//图片的原始名称
String oriName = file1.getOriginalFilename();
exceptionImage = oriName;
String filePath2 = filePath+oriName;
File file3 = new File(filePath2);
if(file3.exists()){
System.out.println("已存在图片,无需写入磁盘中");
continue;
}
System.out.println("没有存在,将图片存放到磁盘中");
os = new FileOutputStream(filePath2);
os.write(file1.getBytes());
}
map.put("success",true);
System.out.println("图片存放磁盘目录:"+imagePath);
}catch (Exception e){
e.printStackTrace();
map.put("success",false );
map.put("exceptionImage", exceptionImage);
}
return map;
}