controller:
//批量导入 法源信息(多文件上传)
@RequestMapping(value = "/addlot")
public String addlot(@RequestParam MultipartFile[] myfiles,
HttpServletRequest request, HttpSession httpSession,
RedirectAttributesModelMap model) throws IOException {
List<YellowpagesitemDO> list = new ArrayList<YellowpagesitemDO>();
String parentId = (String) httpSession.getAttribute(SESSION_LAW02_PARENTID);
int ok = 0;
int no = 0;
int total = 0;
// 如果只是上传一个文件,则只需要MultipartFile类型接收文件即可,而且无需显式指定@RequestParam注解
// 如果想上传多个文件,那么这里就要用MultipartFile[]类型来接收文件,并且还要指定@RequestParam注解
// 并且上传多个文件时,前台表单中的所有<input type="file"/>的name都应该是myfiles,否则参数里的myfiles无法获取到所有上传的文件
for (MultipartFile myfile : myfiles) {
YellowpagesitemDO yellowpagesitemDO = new YellowpagesitemDO();
boolean flag = false;
if (myfile.isEmpty()) {
System.out.println("文件未上传");
} else {
total = total + 1;
String filename = myfile.getOriginalFilename();
if ((filename != null) && (filename.length() > 0)) {
// 查找字符‘.‘出现的最后一个索引位置
int x = filename.lastIndexOf('.');
// 判断此索引是否存在,并且不是最后一个字符
if ((x > -1) && (x < filename.length() - 1)) {
if (filename.substring(x + 1).equals("html")
|| filename.substring(x + 1).equals("htm")) {// 调用subString方法从索引位开始截取到最后,并且不截取字符'.'
flag = true;
}
;
}
}
if (flag) {
String path3 = request.getSession().getServletContext().getRealPath("")+ "\\views\\law02\\" + parentId;
File targetFile = new File(path3,myfile.getOriginalFilename());
if (!targetFile.exists()) {
targetFile.mkdirs();
}
try {
myfile.transferTo(targetFile);
yellowpagesitemDO.setName(myfile.getOriginalFilename());
yellowpagesitemDO.setValue("添加成功!");
list.add(yellowpagesitemDO);
ok = ok + 1;
String content=path3+"\\"+myfile.getOriginalFilename();
LawDO llDo=law01Service.getlaw(content);
if(llDo!=null){
}else {
LawDO lawDO =new LawDO();
lawDO.setContent(content);
lawDO.setDelFlag(0);
law01Service.insertLaw(lawDO);
}
}catch(Exception e){
//e.printStackTrace();
}
} else {
no = no + 1;
yellowpagesitemDO.setName(myfile.getOriginalFilename());
yellowpagesitemDO.setValue("格式错误,导入失败!");
list.add(yellowpagesitemDO);
}
}
}
model.addAttribute("id", parentId);
model.addFlashAttribute("total", total);
model.addFlashAttribute("no", no);
model.addFlashAttribute("ok", ok);
model.addFlashAttribute("mess", list);
return "redirect:/law02/show";
}
Freemarker:
<h1>批量导入 :</h1><br/>
<form id="uploadForm" name ="uploadForm" method ="post" action ="${base}/law02/addlot" enctype ="multipart/form-data" >
<p><input type ="button" value ="增加上传按钮" onclick ="addUploadButton()" /></p>
<p><input id ="files" type ="file" name ="myfiles" value ="浏览文件" /></p>
</form >
<button id="lotsub" class="button bg-main">提交</button>
</br>
<hr class="bg-gray" />
<h1>批量导入信息反馈 :</h1>
<br/>
<#if total??>
<h1>共计导入 : ${total!}条, 成功 :${ok!}条, 失败 :${no!}条 </h1>
<br/>
</#if>
<table class="table table-bordered table-striped">
<tbody>
<tr>
<th width="30%">导入内容</th>
<th width="30%">导入状态</th>
</tr>
<#list mess! as x>
<tr>
<td>${x.name!}</td>
<td>${x.value!}</td>
</tr>
</#list>
</tbody>
</table>
$('#lotsub').click(function() {
var files = $('#files').val();
if(files =="" ){
alert("请添加文件");
return false ;
}
else{
$('form').submit();
}
});
function addUploadButton(){
// 按ID找到FOrm
var uploadForm = document.getElementById( "uploadForm" );
// 创建P元素
var pNode = document.createElement( "p" );
// 累加Count以观察次数
pNode.innerHTML = " <input type='file' name='myfiles' value='浏览文件'/> " ;
// 将P元素添加到body中
uploadForm.appendChild(pNode);
}