【fix sonar】CommonController文件
This commit is contained in:
+94
-100
@@ -66,6 +66,8 @@ public class CommonController {
|
|||||||
|
|
||||||
private static final String UTF8 ="UTF-8";
|
private static final String UTF8 ="UTF-8";
|
||||||
|
|
||||||
|
private static final String FILE_VIEW_ERROR = "预览文件失败";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author 政辉
|
* @Author 政辉
|
||||||
* @return
|
* @return
|
||||||
@@ -95,8 +97,9 @@ public class CommonController {
|
|||||||
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
||||||
// 获取上传文件对象
|
// 获取上传文件对象
|
||||||
MultipartFile file = multipartRequest.getFile("file");
|
MultipartFile file = multipartRequest.getFile("file");
|
||||||
if(file==null)
|
if(file==null) {
|
||||||
return result;
|
return result;
|
||||||
|
}
|
||||||
// 文件类型是否处于黑名单
|
// 文件类型是否处于黑名单
|
||||||
if(CommonUtils.limitFileSuffix(file.getOriginalFilename(),fileSuffixLimits)){
|
if(CommonUtils.limitFileSuffix(file.getOriginalFilename(),fileSuffixLimits)){
|
||||||
result.setMessage("该文件类型不允许上传");
|
result.setMessage("该文件类型不允许上传");
|
||||||
@@ -150,64 +153,65 @@ public class CommonController {
|
|||||||
throw new JeroBootException("文件不存在..");
|
throw new JeroBootException("文件不存在..");
|
||||||
}
|
}
|
||||||
String fileUrl = ossFile.getUrl();
|
String fileUrl = ossFile.getUrl();
|
||||||
InputStream inputStream = null;
|
if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){
|
||||||
OutputStream outputStream = null;
|
//本地下载
|
||||||
try {
|
String filePath = uploadpath + File.separator + fileUrl;
|
||||||
String fileName = "";
|
File file = new File(filePath);
|
||||||
if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){
|
if(!file.exists()){
|
||||||
//本地下载
|
response.setStatus(404);
|
||||||
String filePath = uploadpath + File.separator + fileUrl;
|
throw new JeroBootException("文件不存在..");
|
||||||
File file = new File(filePath);
|
|
||||||
if(!file.exists()){
|
|
||||||
response.setStatus(404);
|
|
||||||
throw new RuntimeException("文件不存在..");
|
|
||||||
}
|
|
||||||
// 文件名称
|
|
||||||
fileName = file.getName();
|
|
||||||
inputStream = new BufferedInputStream(new FileInputStream(filePath));
|
|
||||||
}else if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){
|
|
||||||
// minio 下载
|
|
||||||
// 通过MinioUtil查询时 只需要桶后面的路径
|
|
||||||
String minioUrl = MinioUtil.getMinioUrl();
|
|
||||||
// Linux/unix 系统下文件路径分隔符为"/" 获取minio与存储桶的路径
|
|
||||||
minioUrl = minioUrl + MinioUtil.getBucketName() + "/";
|
|
||||||
String url = fileUrl.replace(minioUrl, "");
|
|
||||||
// 文件名称
|
|
||||||
fileName = ossFile.getFileName();
|
|
||||||
inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url);
|
|
||||||
}
|
}
|
||||||
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1));
|
// 文件名称
|
||||||
response.setContentType("application/force-download");// 设置强制下载不打开
|
String fileName = file.getName();
|
||||||
outputStream = response.getOutputStream();
|
setResponse(response, fileName);
|
||||||
byte[] buf = new byte[1024];
|
try (InputStream inputStream = new BufferedInputStream(new FileInputStream(filePath));
|
||||||
int len;
|
OutputStream outputStream = response.getOutputStream()
|
||||||
if(inputStream==null)
|
) {
|
||||||
return;
|
byte[] buf = new byte[1024];
|
||||||
while ((len = inputStream.read(buf)) > 0) {
|
int len;
|
||||||
outputStream.write(buf, 0, len);
|
while ((len = inputStream.read(buf)) > 0) {
|
||||||
}
|
outputStream.write(buf, 0, len);
|
||||||
response.flushBuffer();
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error("预览文件失败" + e.getMessage());
|
|
||||||
response.setStatus(404);
|
|
||||||
e.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
if (inputStream != null) {
|
|
||||||
try {
|
|
||||||
inputStream.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error(e.getMessage(), e);
|
|
||||||
}
|
}
|
||||||
|
response.flushBuffer();
|
||||||
|
}catch (Exception e){
|
||||||
|
log.error(FILE_VIEW_ERROR + e.getMessage());
|
||||||
|
response.setStatus(404);
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
if (outputStream != null) {
|
}else if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){
|
||||||
try {
|
// minio 下载
|
||||||
outputStream.close();
|
// 通过MinioUtil查询时 只需要桶后面的路径
|
||||||
} catch (IOException e) {
|
String minioUrl = MinioUtil.getMinioUrl();
|
||||||
log.error(e.getMessage(), e);
|
// Linux/unix 系统下文件路径分隔符为"/" 获取minio与存储桶的路径
|
||||||
|
minioUrl = minioUrl + MinioUtil.getBucketName() + "/";
|
||||||
|
String url = fileUrl.replace(minioUrl, "");
|
||||||
|
// 文件名称
|
||||||
|
String fileName = ossFile.getFileName();
|
||||||
|
setResponse(response, fileName);
|
||||||
|
try (InputStream inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url);
|
||||||
|
OutputStream outputStream = response.getOutputStream()
|
||||||
|
) {
|
||||||
|
byte[] buf = new byte[1024];
|
||||||
|
int len;
|
||||||
|
if(inputStream==null) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
while ((len = inputStream.read(buf)) > 0) {
|
||||||
|
outputStream.write(buf, 0, len);
|
||||||
|
}
|
||||||
|
response.flushBuffer();
|
||||||
|
}catch (Exception e){
|
||||||
|
log.error(FILE_VIEW_ERROR + e.getMessage());
|
||||||
|
response.setStatus(404);
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setResponse(HttpServletResponse response, String fileName) {
|
||||||
|
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
|
||||||
|
// 设置强制下载不打开
|
||||||
|
response.setContentType("application/force-download");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -246,11 +250,7 @@ public class CommonController {
|
|||||||
// 中转请求method、body
|
// 中转请求method、body
|
||||||
HttpMethod method = httpRequest.getMethod();
|
HttpMethod method = httpRequest.getMethod();
|
||||||
JSONObject params;
|
JSONObject params;
|
||||||
try {
|
params = getJsonObject(httpRequest);
|
||||||
params = JSON.parseObject(JSON.toJSONString(httpRequest.getBody()));
|
|
||||||
} catch (Exception e) {
|
|
||||||
params = new JSONObject();
|
|
||||||
}
|
|
||||||
// 中转请求问号参数
|
// 中转请求问号参数
|
||||||
JSONObject variables = JSON.parseObject(JSON.toJSONString(request.getParameterMap()));
|
JSONObject variables = JSON.parseObject(JSON.toJSONString(request.getParameterMap()));
|
||||||
variables.remove("url");
|
variables.remove("url");
|
||||||
@@ -267,14 +267,7 @@ public class CommonController {
|
|||||||
result.setCode(statusCode);
|
result.setCode(statusCode);
|
||||||
result.setSuccess(statusCode == 200);
|
result.setSuccess(statusCode == 200);
|
||||||
String responseBody = response.getBody();
|
String responseBody = response.getBody();
|
||||||
try {
|
getResult(result, responseBody);
|
||||||
// 尝试将返回结果转为JSON
|
|
||||||
Object json = JSON.parse(responseBody);
|
|
||||||
result.setResult(json);
|
|
||||||
} catch (Exception e) {
|
|
||||||
// 转成JSON失败,直接返回原始数据
|
|
||||||
result.setResult(responseBody);
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.debug("中转HTTP请求失败", e);
|
log.debug("中转HTTP请求失败", e);
|
||||||
@@ -282,6 +275,27 @@ public class CommonController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void getResult(Result<Object> result, String responseBody) {
|
||||||
|
try {
|
||||||
|
// 尝试将返回结果转为JSON
|
||||||
|
Object json = JSON.parse(responseBody);
|
||||||
|
result.setResult(json);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 转成JSON失败,直接返回原始数据
|
||||||
|
result.setResult(responseBody);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private JSONObject getJsonObject(ServletServerHttpRequest httpRequest) {
|
||||||
|
JSONObject params;
|
||||||
|
try {
|
||||||
|
params = JSON.parseObject(JSON.toJSONString(httpRequest.getBody()));
|
||||||
|
} catch (Exception e) {
|
||||||
|
params = new JSONObject();
|
||||||
|
}
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 预览图片&下载文件
|
* 预览图片&下载文件
|
||||||
* 请求地址:http://localhost:8080/common/static/{user/20190119/e1fe9925bc315c60addea1b98eb1cb1349547719_1547866868179.jpg}
|
* 请求地址:http://localhost:8080/common/static/{user/20190119/e1fe9925bc315c60addea1b98eb1cb1349547719_1547866868179.jpg}
|
||||||
@@ -296,24 +310,21 @@ public class CommonController {
|
|||||||
if(oConvertUtils.isEmpty(imgPath) || "null".equals(imgPath)){
|
if(oConvertUtils.isEmpty(imgPath) || "null".equals(imgPath)){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
imgPath = imgPath.replace("..", "").replace("../","");
|
||||||
|
if (imgPath.endsWith(",")) {
|
||||||
|
imgPath = imgPath.substring(0, imgPath.length() - 1);
|
||||||
|
}
|
||||||
|
String filePath = uploadpath + File.separator + imgPath;
|
||||||
|
File file = new File(filePath);
|
||||||
|
if(!file.exists()){
|
||||||
|
response.setStatus(404);
|
||||||
|
throw new JeroBootException("文件["+imgPath+"]不存在..");
|
||||||
|
}
|
||||||
|
response.setContentType("application/force-download");// 设置强制下载不打开
|
||||||
|
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
|
||||||
// 其余处理略
|
// 其余处理略
|
||||||
InputStream inputStream = null;
|
try (InputStream inputStream = new BufferedInputStream(new FileInputStream(filePath));
|
||||||
OutputStream outputStream = null;
|
OutputStream outputStream = response.getOutputStream()) {
|
||||||
try {
|
|
||||||
imgPath = imgPath.replace("..", "").replace("../","");
|
|
||||||
if (imgPath.endsWith(",")) {
|
|
||||||
imgPath = imgPath.substring(0, imgPath.length() - 1);
|
|
||||||
}
|
|
||||||
String filePath = uploadpath + File.separator + imgPath;
|
|
||||||
File file = new File(filePath);
|
|
||||||
if(!file.exists()){
|
|
||||||
response.setStatus(404);
|
|
||||||
throw new RuntimeException("文件["+imgPath+"]不存在..");
|
|
||||||
}
|
|
||||||
response.setContentType("application/force-download");// 设置强制下载不打开
|
|
||||||
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
|
|
||||||
inputStream = new BufferedInputStream(new FileInputStream(filePath));
|
|
||||||
outputStream = response.getOutputStream();
|
|
||||||
byte[] buf = new byte[1024];
|
byte[] buf = new byte[1024];
|
||||||
int len;
|
int len;
|
||||||
while ((len = inputStream.read(buf)) > 0) {
|
while ((len = inputStream.read(buf)) > 0) {
|
||||||
@@ -321,26 +332,9 @@ public class CommonController {
|
|||||||
}
|
}
|
||||||
response.flushBuffer();
|
response.flushBuffer();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("预览文件失败" + e.getMessage());
|
log.error(FILE_VIEW_ERROR + e.getMessage());
|
||||||
response.setStatus(404);
|
response.setStatus(404);
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
|
||||||
if (inputStream != null) {
|
|
||||||
try {
|
|
||||||
inputStream.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (outputStream != null) {
|
|
||||||
try {
|
|
||||||
outputStream.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user