文件批量上传

 class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    
        
       
         
    
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传下载title>
head>
<body>
    <form action="${pageContext.request.contextPath }/file/upload.html"
        method="post" enctype="multipart/form-data">
        选择文件:<input type="file"  multiple="multiple" name="files" width="120px" > <input
            type="submit" value="上传">
    form>
body>
html>
/***
* 文件的批量上传
* @param files
* @param request
* @return
* @throws Exception
* @throws IOException
*/
@RequestMapping(value="/file/upload.html",method=RequestMethod.POST)
public String upload(@RequestParam("files") MultipartFile[] files,MultipartHttpServletRequest request) throws Exception, IOException{
//获取文件夹的名字
List filess=new ArrayList();
String path = request.getSession().getServletContext().getRealPath("/upload");
StringBuffer stringbuffer=new StringBuffer();
//对传进来的文件数组,进行 循环复制
for(MultipartFile multipartFile:files){
//判断文件是否为空
if(!multipartFile.isEmpty()) {
//将多个文件名拼接在一个字符串中,用;分隔
stringbuffer.append(multipartFile.getOriginalFilename());    
stringbuffer.append(";");
File dir=new File(path, multipartFile.getOriginalFilename());
//将文件名和对应的路径存放在数组中
String[] files1={multipartFile.getOriginalFilename(),dir.toPath().toString()};
//将一个文件的标识信息存入集合中
filess.add(files1);
//System.out.println(dir.toPath());
//文件不存则在创建
if(!dir.exists()&&!dir.isDirectory()){ 
dir.mkdirs(); 
} 
//文件进行复制
multipartFile.transferTo(dir);
}
}
String s=stringbuffer.substring(0, stringbuffer.length()-1);
//将文件信息集合存入数据库中
fileOperateService.insertfiles(filess);
//跳转到调用文件显示的界面
return "redirect:/file/showall.html"; 
}

 



你可能感兴趣的:(文件批量上传)