springmvc上传文件不用MultipartFile接收

直接用原始的request接收,然后判断是否含有MultipartFile类型:

@ResponseBody
@RequestMapping(value = "verUpload", method = RequestMethod.POST)
public UploadUtil verUpload(HttpServletRequest request) throws Exception {
    
    if (FileUpload.isMultipartContent(request)) {
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding("utf-8");
        upload.setSizeMax(-1);
        List items = new ArrayList();
        try {
            items = upload.parseRequest(request);
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
        Iterator it = items.iterator();
        while (it.hasNext()) {
            FileItem file = (FileItem) it.next();
            ......
        }
return null;
}

使用到的jar包:

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

 

你可能感兴趣的:(java)