码迷,mamicode.com
首页 > Web开发 > 详细

文件上传

时间:2020-03-24 11:02:21      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:ati   set   double   required   Servle   tps   系统错误   tip   文件上传   

单个文件上传:

// 通过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();
}

 

文件上传

标签:ati   set   double   required   Servle   tps   系统错误   tip   文件上传   

原文地址:https://www.cnblogs.com/zeevy/p/12557230.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!