Bulk upload file

 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    
     <property name="defaultEncoding" value="utf-8"></property>   
      <!-- 配置文件的最大上传容量限制 --> 
     <property name="maxUploadSize" value="50242440"></property>    
    </bean>
<%@ 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<String[]> filess=new ArrayList<String[]>();
String path Request.getSession = () GetServletContext () the getRealPath ( "/ Upload.". ); 
The StringBuffer StringBuffer = new new the StringBuffer ();
 // for array passed in files, copying cycle 
for (a MultipartFile MultipartFile: Files) {
 // Analyzing whether the file is empty 
iF (! multipartFile.isEmpty ()) {
 // the plurality of file names in a string spliced, separated by; 
StringBuffer.append (multipartFile.getOriginalFilename ());     
StringBuffer.append ( ";" ) ; 
file the dir = new new file (path, multipartFile.getOriginalFilename ());
 // the file name and path stored in an array corresponding to the 
String [] files1 = {multipartFile.getOriginalFilename (), dir.toPath () toString ().} ;
// The identification information is stored in a file set 
filess.add (files1);
 // System.out.println (dir.toPath ());
 // file does not exist then create 
IF (dir.exists ()! ! && dir.isDirectory ()) { 
dir.mkdirs (); 
} 
// file for replication 
multipartFile.transferTo (the dir); 
} 
} 
String S = stringbuffer.substring (0, stringbuffer.length () -. 1 );
 // the collection of information stored in the database file 
fileOperateService.insertfiles (FileSS);
 // jump to the interface to call the file displayed in 
return "redirect: /file/showall.html" ; 
}

 



Guess you like

Origin www.cnblogs.com/itzyz/p/11516755.html