欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

多文件上传

程序员文章站 2022-07-09 18:34:39
package com.test.test; import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import ......

package com.test.test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;

import cn.com.taiji.common.manager.ManagerException;

public class TestUploadFile {
/**
*
* @param multipartFiles 多文件上传流
* @param request request请求
* @param savePath 保存路径
* @return 返回包括文件名称、文件路径组成的map集合的list集合
* @throws IllegalStateException
* @throws IOException
* @throws ManagerException
*/
public List<Map<String, String>> uploadFile(MultipartFile[] multipartFiles, HttpServletRequest request, String savePath) throws IllegalStateException, IOException, ManagerException {

List<Map<String, String>> fileList = new ArrayList<>();

ServletContext servletContext = request.getServletContext();
for (MultipartFile multipartFile : multipartFiles) {
Map<String, String> fileMap = new HashMap<String,String>();
if (multipartFile!=null&&multipartFile.getSize()!=0) {
String imageName = multipartFile.getOriginalFilename();
String path = servletContext.getRealPath(savePath)+imageName;
File file=new File(path);
multipartFile.transferTo(file);
fileMap.put("fileName", imageName);
fileMap.put("savePath", savePath);
}
fileList.add(fileMap);
}
return fileList;
}
}