首页 > 技术文章 > 文件上传

zeevy 2020-03-24 10:15 原文

单个文件上传:

// 通过MultipartFile获取上传的单个文件流 

public
@ResponseBody String uploadFile(@RequestParam(value = "file", required = false) MultipartFile mFile, String relativedId, String strRelativedType, String strDesc) { ResponseData ajaxObject = new ResponseData(); try { NjAttach attach = attachService.uploadFile(mFile, relativedId, strRelativedType, strDesc); ajaxObject.setMessage(attach.getStrName() + "文件上传成功"); ajaxObject.setStatusCode(AjaxObject.STATUS_CODE_SUCCESS); Map<String, Object> fileInfo = new HashMap<String, Object>(); fileInfo.put("id", attach.getId()); fileInfo.put("strName", attach.getStrName()); fileInfo.put("doubleSize", attach.getDoubleSize()); fileInfo.put("strDocType", attach.getStrDocType()); fileInfo.put("strPath", attach.getStrPath()); fileInfo.put("relativedId", relativedId); ajaxObject.setData(fileInfo); }catch (ServiceException e) { log.error(e); ajaxObject.setStatusCode(AjaxObject.STATUS_CODE_FAILURE); ajaxObject.setMessage(e.getMessage()); } catch (Exception e) { log.error(e); ajaxObject.setStatusCode(AjaxObject.STATUS_CODE_FAILURE); ajaxObject.setMessage("系统错误,上传文件失败"); } return ajaxObject.toString(); }

 

多个文件上传:

//通过HttpServletRequest获取文件流
public String uploadListFile(HttpServletRequest request, String relativedId, String relativedType) {
    AjaxObject ajaxObject = new AjaxObject();
    try {
        MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> files = multiRequest.getFileMap();
        String attachIds = attachService.uploadListFile(files, relativedId, relativedType);
        ajaxObject.setRel(attachIds);
        ajaxObject.setMessage("上传成功");
    } catch (Exception e) {
        e.printStackTrace();
        ajaxObject.setStatusCode(AjaxObject.STATUS_CODE_FAILURE);
        ajaxObject.setMessage("上传失败");
    }
    return ajaxObject.toString();
}

 

推荐阅读