Created
April 28, 2019 03:37
-
-
Save logicjwell/d87c2fbf07cae2df736614d24af70dbf to your computer and use it in GitHub Desktop.
[springboot中上传文件的服务接口] #springboot #上传
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ApiOperation("上传需求单附件") | |
@PostMapping(value = "${order-trade.mgmt.requirement}/upload/{requirementUuid}", | |
consumes = "multipart/*", | |
headers = "content-type=multipart/form-data") | |
public JsonResult upload(@PathVariable String requirementUuid, | |
@ApiParam(value = "上传文件", required = true) MultipartFile file, | |
@RequestHeader(Constant.HTTP_SECURITY_HEADER) String token){ | |
UserInfo userinfo = getUserInfo(token); | |
String myUuid = userinfo.getUuid(); | |
log.debug("[上传文件]"); | |
try { | |
String remoteStoreFileName = upyunUtils.getRemoteUploadDir() + StringUtils.stripFilenameExtension(file.getOriginalFilename())+"-"+System.currentTimeMillis() + "." + StringUtils.getFilenameExtension(file.getOriginalFilename()); | |
boolean storeToRemoteFolderResult = upyunUtils.upload(remoteStoreFileName, file.getBytes()); | |
if (storeToRemoteFolderResult) { | |
log.debug("[上传文件] 成功存储到云盘,文件名: {}", remoteStoreFileName); | |
CreateRequirementAttachmentDto dto = new CreateRequirementAttachmentDto(); | |
dto.setCreateBy(myUuid); | |
dto.setRequirementUuid(requirementUuid); | |
dto.setFilePath(remoteStoreFileName); | |
requirement.createAttachment(dto); | |
return JsonResult.newResult(JsonResult.Status.OK, "上传成功", upyunUtils.getUpyunHost() + remoteStoreFileName); | |
} else { | |
log.debug("[上传文件] 存储到云盘时失败"); | |
return JsonResult.newResult(JsonResult.Status.FAILED, "存储到云盘时失败", null); | |
} | |
}catch (IOException ioe){ | |
log.error("上传文件时遇到错误", ioe); | |
return JsonResult.newResult(JsonResult.Status.FAILED,"上传文件时遇到错误", ioe.getMessage()); | |
} | |
} | |
代码中的关键点: | |
1. @PostMapping(value = "${order-trade.mgmt.requirement}/upload/{requirementUuid}", | |
consumes = "multipart/*", //关键点1 | |
headers = "content-type=multipart/form-data") //关键点2 | |
2. @ApiParam(value = "上传文件", required = true) MultipartFile file //只能上传一个文件,这是个限制 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment