多文件上传

作者: 代码墨白 | 来源:发表于2018-07-13 18:44 被阅读2次

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> uploadFile(MultipartFile[] multipartFiles, HttpServletRequest request, String savePath) throws IllegalStateException, IOException, ManagerException

{

List> fileList = new ArrayList<>();

ServletContext servletContext = request.getServletContext();

for (MultipartFile multipartFile : multipartFiles)

{MapfileMap = new HashMap();

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;

} }

相关文章

网友评论

    本文标题:多文件上传

    本文链接:https://www.haomeiwen.com/subject/ozpjpftx.html