提炼字段关键信息:图片二进制字节流,最小50K,最大为5M,支持png/bmp/gif/jpg/jpeg格式
@Slf4j
@Service
public class BaseAlipayApiImageSerImpl implements BaseAlipayApiImageSer {
@Autowired
AlipayApiProperties alipayApiProp;
@Autowired
BizLogSer bizLogSer;
@SneakyThrows
@Override
public BaseAlipayImageUploadOut upload(BaseAlipayImageUploadIn in) {
/** 操作:调用支付宝接口 */
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConstant.COMMON_SERVER_URL, alipayApiProp.getAppid(), alipayApiProp.getPrivateKey(), AlipayConstant.COMMON_FORMAT, AlipayConstant.COMMON_CHARSET, alipayApiProp.getPublicKey(), AlipayConstant.COMMON_SIGN_TYPE);
AlipayOpenSpImageUploadRequest request = new AlipayOpenSpImageUploadRequest();
request.setImageContent(new FileItem(in.getImageFile()));
AlipayOpenSpImageUploadResponse response = alipayClient.execute(request);
/** 操作:记录业务日志 */
bizLogSer.save(new BizLogSave(new BizLogDataTemp(in, response), AlipayConstant.IMAGE_UPLOAD_LOG));
/** 操作:结果转换 */
BaseAlipayImageUploadOut result = ObjectUtil.copy(response, BaseAlipayImageUploadOut.class);
BaseAlipayCommonOut commonOut = result.commonOut(response);
ObjectUtil.copy(commonOut, result);
return result;
}
BaseAlipayApiImageSerImpl.java 详情:https://gitee.com/zhaifengxi/zhai-docking-alipay-open/blob/master/src/main/java/zhai/docking/alipay/service/base/alipay/api/BaseAlipayApiImageSerImpl.java
@RestController
@RequestMapping("/api/alipay/img")
public class AlipayImgCtr {
@Autowired
AlipayImgSer alipayImgSer;
@RequestMapping(path = "/upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity upload(@RequestParam(name = "file") MultipartFile file) {
/** 校验:是否为空 */
if (file.isEmpty()) {
return ResponseEntity.ok(Result.failure(ResultConstant.FAIL_SYS_NO_IN_ERROR_CODE, ResultConstant.FAIL_SYS_NO_IN_ERROR_MSG));
}
/** 校验:文件类型 */
String fileType = FileUtil.getFileType(FileUtil.getFileName(file));
if (StringUtil.isBlank(fileType)) {
return ResponseEntity.ok(Result.failure(ResultConstant.FAIL_SYS_DEF_CODE, "该文件没有文件类型"));
}
if (!FileUtil.isFileType(fileType, FileUtil.FILE_TYPE_JPG, FileUtil.FILE_TYPE_JPEG)) {
return ResponseEntity.ok(Result.failure(ResultConstant.FAIL_BIZ_DEF_CODE, StringUtil.join("不支持上传该文件格式:", fileType)));
}
/** 校验:文件大小 */
boolean minSize = FileUtil.checkMinSize(file.getSize(), 50, FileUtil.UNIT_KB);
if (!minSize) {
return ResponseEntity.ok(Result.failure(ResultConstant.FAIL_BIZ_DEF_CODE, "文件大小不能小于50KB"));
}
boolean maxSize = FileUtil.checkMaxSize(file.getSize(), 5, FileUtil.UNIT_MB);
if (!maxSize) {
return ResponseEntity.ok(Result.failure(ResultConstant.FAIL_BIZ_DEF_CODE, "文件大小不能大于5MB"));
}
Result result = alipayImgSer.upload(file);
return ResponseEntity.ok().body(result);
}
}
AlipayImgCtr.java 详情:https://gitee.com/zhaifengxi/zhai-docking-alipay-open/blob/master/src/main/java/zhai/docking/alipay/api/AlipayImgCtr.java
@Service
public class AlipayImgSerImpl implements AlipayImgSer {
@Autowired
BaseAlipayApiImageSer baseAlipayApiImageSer;
@Autowired
SnowflakeUtil snowflakeUtil;
@SneakyThrows
@Override
public Result upload(MultipartFile in) {
/** 操作:重命名文件名 */
String imageName = StringUtil.join(snowflakeUtil.nextId(), StringConstant.POINT, FileUtil.getFileType(FileUtil.getFileName(in)));
File file = new File(imageName);
FileUtil.copyInputStreamToFile(in.getInputStream(), file);
BaseAlipayImageUploadOut upload = null;
try {
/** 操作:上送-支付宝图片接口 */
BaseAlipayImageUploadIn uploadIn = new BaseAlipayImageUploadIn();
uploadIn.setImageFile(file);
upload = baseAlipayApiImageSer.upload(uploadIn);
} finally {
if (file.exists()) {
file.delete(); // 删除临时文件
}
}
/** 结果:转换 */
if (!upload.isSuccess()) {
log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_UPLOAD, LogConstant.RESULT, LogConstant.FAIL, upload.getCode(), upload.getMsg()));
return Result.failure(upload.getCode(), upload.getMsg());
}
AlipayImageUploadOut result = new AlipayImageUploadOut();
result.setImgCode(upload.getImageId());
return Result.success(result);
}
}
AlipayImgSerImpl.java 详情:https://gitee.com/zhaifengxi/zhai-docking-alipay-open/blob/master/src/main/java/zhai/docking/alipay/service/biz/AlipayImgSerImpl.java
if (!FileUtil.isFileType(fileType, FileUtil.FILE_TYPE_JPG, FileUtil.FILE_TYPE_JPEG)) {
return ResponseEntity.ok(Result.failure(ResultConstant.FAIL_BIZ_DEF_CODE, StringUtil.join("不支持上传该文件格式:", fileType)));
}
ERROR sdk.biz.err - 2021-01-19 14:34:04^_^
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>支付宝 - 网上支付 安全快速!title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="x-ua-compatible" content="ie=7" />
<meta name="description" content="中国最大的第三方电子支付服务提供商" />
...
<li>支付宝版权所有 2004-2021 ALIPAY.COMli>
ul>
<div id="ServerNum">openapi-49-14424div>
div>
/** 操作:重命名文件名 */
String imageName = StringUtil.join(snowflakeUtil.nextId(), StringConstant.POINT, FileUtil.getFileType(FileUtil.getFileName(in)));
支付宝报错特点:ERROR sdk.biz.err
/** MultipartFile 转 File */
File file = new File(imageName);
图片文件名:你好_12313.jpg
后端接收:???_12313.jpg
/** 操作:重命名文件名 */
String imageName = StringUtil.join(snowflakeUtil.nextId(), StringConstant.POINT, FileUtil.getFileType(FileUtil.getFileName(in)));
org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:111)
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.(StandardMultipartHttpServletRequest.java:85)
at org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:79)
servlet: # 文件上传配置
multipart:
max-file-size: 10MB
max-request-size: 10MB