文件上传
Posted zeevy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件上传相关的知识,希望对你有一定的参考价值。
单个文件上传:
// 通过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(); }
以上是关于文件上传的主要内容,如果未能解决你的问题,请参考以下文章
ajaxFileUpload上传带参数文件及JS验证文件大小