feat: 上传接口实现
This commit is contained in:
@@ -5,9 +5,13 @@ import com.chaozhanggui.system.cashierservice.entity.dto.ClearTableDTO;
|
||||
import com.chaozhanggui.system.cashierservice.service.ShopInfoService;
|
||||
import com.chaozhanggui.system.cashierservice.sign.CodeEnum;
|
||||
import com.chaozhanggui.system.cashierservice.sign.Result;
|
||||
import com.chaozhanggui.system.cashierservice.util.AliUploadUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@CrossOrigin(origins = "*")
|
||||
@RestController
|
||||
@@ -78,4 +82,9 @@ public class ShopInfoController {
|
||||
public Result queryPwdInfo(@RequestParam("shopId") String shopId) {
|
||||
return shopInfoService.queryShopPwdInfo(shopId);
|
||||
}
|
||||
|
||||
@PostMapping("upload")
|
||||
public Result upload(@RequestParam("file") MultipartFile file) throws Exception {
|
||||
return Result.success(CodeEnum.SUCCESS, AliUploadUtils.uploadSuffix(file.getInputStream(), file.getOriginalFilename()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,12 +16,15 @@ public class CustomFilter implements Filter {
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
log.info(">>>> customFilter init <<<<");
|
||||
}
|
||||
|
||||
public boolean isMultipartRequest(HttpServletRequest request) {
|
||||
String contentType = request.getContentType();
|
||||
return contentType != null && contentType.toLowerCase().startsWith("multipart/form-data");
|
||||
}
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
|
||||
log.info(">>>> customFilter doFilter start <<<<");
|
||||
RequestWrapper requestWapper = null;
|
||||
if (servletRequest instanceof HttpServletRequest) {
|
||||
if (servletRequest instanceof HttpServletRequest && !isMultipartRequest((HttpServletRequest)servletRequest)) {
|
||||
requestWapper = new RequestWrapper((HttpServletRequest) servletRequest);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.chaozhanggui.system.cashierservice.interceptor;
|
||||
|
||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
|
||||
import javax.servlet.ReadListener;
|
||||
import javax.servlet.ServletInputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@@ -9,73 +11,70 @@ import java.io.*;
|
||||
public class RequestWrapper extends HttpServletRequestWrapper {
|
||||
private final String body;
|
||||
|
||||
public RequestWrapper(HttpServletRequest request) {
|
||||
public RequestWrapper(HttpServletRequest request) throws IOException {
|
||||
super(request);
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
BufferedReader bufferedReader = null;
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
|
||||
// 仅处理普通请求体,不影响multipart请求
|
||||
if (!(request instanceof MultipartHttpServletRequest)) {
|
||||
inputStream = request.getInputStream();
|
||||
if (inputStream != null) {
|
||||
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
char[] charBuffer = new char[128];
|
||||
int bytesRead = -1;
|
||||
int bytesRead;
|
||||
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
|
||||
stringBuilder.append(charBuffer, 0, bytesRead);
|
||||
}
|
||||
} else {
|
||||
stringBuilder.append("");
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
try {
|
||||
inputStream.close();
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (bufferedReader != null) {
|
||||
try {
|
||||
bufferedReader.close();
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
body = stringBuilder.toString();
|
||||
} else {
|
||||
// 如果是Multipart请求,不做处理,直接返回
|
||||
body = null;
|
||||
}
|
||||
body = stringBuilder.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServletInputStream getInputStream() throws IOException {
|
||||
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
|
||||
ServletInputStream servletInputStream = new ServletInputStream() {
|
||||
@Override
|
||||
public boolean isFinished() {
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
public boolean isReady() {
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
public void setReadListener(ReadListener readListener) {
|
||||
}
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
return byteArrayInputStream.read();
|
||||
}
|
||||
};
|
||||
return servletInputStream;
|
||||
if (body != null) {
|
||||
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
|
||||
return new ServletInputStream() {
|
||||
@Override
|
||||
public boolean isFinished() {
|
||||
return byteArrayInputStream.available() == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReady() {
|
||||
return byteArrayInputStream.available() > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReadListener(ReadListener readListener) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
return byteArrayInputStream.read();
|
||||
}
|
||||
};
|
||||
} else {
|
||||
// 如果是Multipart请求,返回空的输入流,交给Spring处理
|
||||
return super.getInputStream();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public BufferedReader getReader() throws IOException {
|
||||
return new BufferedReader(new InputStreamReader(this.getInputStream()));
|
||||
if (body != null) {
|
||||
return new BufferedReader(new InputStreamReader(this.getInputStream()));
|
||||
} else {
|
||||
// 如果是Multipart请求,返回空的reader
|
||||
return super.getReader();
|
||||
}
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
|
||||
@@ -15,6 +15,7 @@ import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -121,7 +122,7 @@ public class SignInterceptor implements HandlerInterceptor {
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
private Map<String, Object> getParameterMap(HttpServletRequest request) {
|
||||
private Map<String, Object> getParameterMap(HttpServletRequest request) throws IOException {
|
||||
RequestWrapper requestWrapper = new RequestWrapper(request);
|
||||
String body = requestWrapper.getBody();
|
||||
if (ObjectUtil.isNotEmpty(body)) {
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* Copyright (c) 2018 人人开源 All rights reserved.
|
||||
*
|
||||
* https://www.renren.io
|
||||
*
|
||||
* 版权所有,侵权必究!
|
||||
*/
|
||||
|
||||
package com.chaozhanggui.system.cashierservice.util;
|
||||
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 阿里云存储
|
||||
*
|
||||
* @author Mark sunlightcs@gmail.com
|
||||
*/
|
||||
@Component
|
||||
public class AliUploadUtils {
|
||||
private static String secret;
|
||||
private static String key;
|
||||
private static String endPoint;
|
||||
private static String bucketName;
|
||||
private static String prefix;
|
||||
private static String url;
|
||||
|
||||
// 构造器注入
|
||||
public AliUploadUtils(@Value("${aliyun.keysecret}") String secret,
|
||||
@Value("${aliyun.keyid}") String key,
|
||||
@Value("${aliyun.oss.endpoint}") String endPoint,
|
||||
@Value("${aliyun.oss.bucketname}") String bucketName,
|
||||
@Value("${aliyun.prefix}") String prefix,
|
||||
@Value("${aliyun.url}") String url) {
|
||||
AliUploadUtils.secret = secret;
|
||||
AliUploadUtils.key = key;
|
||||
AliUploadUtils.endPoint = endPoint;
|
||||
AliUploadUtils.bucketName = bucketName;
|
||||
AliUploadUtils.prefix = prefix;
|
||||
AliUploadUtils.url = url;
|
||||
}
|
||||
/**
|
||||
* 文件路径
|
||||
* @param prefix 前缀
|
||||
* @param suffix 后缀
|
||||
* @return 返回上传路径
|
||||
*/
|
||||
public static String getPath(String prefix, String suffix) {
|
||||
//生成uuid
|
||||
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
||||
//文件路径
|
||||
String path = DateUtils.getDays() + "/" + uuid;
|
||||
|
||||
if(StringUtils.isNotBlank(prefix)){
|
||||
path = prefix + "/" + path;
|
||||
}
|
||||
|
||||
return path + "." + suffix;
|
||||
}
|
||||
|
||||
|
||||
public static String upload(InputStream inputStream, String path) throws Exception {
|
||||
OSSClient client = new OSSClient(endPoint, key,
|
||||
secret);
|
||||
try {
|
||||
client.putObject(bucketName, path, inputStream);
|
||||
client.shutdown();
|
||||
} catch (Exception e){
|
||||
throw new Exception("上传异常");
|
||||
}
|
||||
|
||||
return "https://"+ url + "/" + path;
|
||||
}
|
||||
|
||||
|
||||
public static String uploadSuffix(InputStream inputStream, String fileName) throws Exception {
|
||||
return upload(inputStream, getPath(prefix, getFileExtension(fileName)));
|
||||
}
|
||||
|
||||
public static String getFileExtension(String fileName) {
|
||||
int dotIndex = fileName.lastIndexOf(".");
|
||||
if (dotIndex > 0) {
|
||||
return fileName.substring(dotIndex).replace(".", "");
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -83,3 +83,13 @@ mybatis-plus:
|
||||
|
||||
# php服务器地址
|
||||
phpServer: https://czgdoumei.sxczgkj.com/index.php/api
|
||||
|
||||
# oss配置
|
||||
aliyun:
|
||||
prefix: upload
|
||||
keyid: LTAI5tPdEfYSZcqHbjCrtPRD
|
||||
keysecret: DZjyHBq3nTujF0NMLxnZgsecU8ZCvy
|
||||
url: cashier-oss.oss-cn-beijing.aliyuncs.com
|
||||
oss:
|
||||
bucketname: cashier-oss
|
||||
endpoint: oss-cn-beijing.aliyuncs.com
|
||||
|
||||
Reference in New Issue
Block a user