【fix sonar】CommonController文件

This commit is contained in:
梁琦涛
2023-03-08 17:51:52 +08:00
parent 4c56a27523
commit aae655dbd7
@@ -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,21 +153,31 @@ public class CommonController {
throw new JeroBootException("文件不存在.."); throw new JeroBootException("文件不存在..");
} }
String fileUrl = ossFile.getUrl(); String fileUrl = ossFile.getUrl();
InputStream inputStream = null;
OutputStream outputStream = null;
try {
String fileName = "";
if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){ if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){
//本地下载 //本地下载
String filePath = uploadpath + File.separator + fileUrl; String filePath = uploadpath + File.separator + fileUrl;
File file = new File(filePath); File file = new File(filePath);
if(!file.exists()){ if(!file.exists()){
response.setStatus(404); response.setStatus(404);
throw new RuntimeException("文件不存在.."); throw new JeroBootException("文件不存在..");
} }
// 文件名称 // 文件名称
fileName = file.getName(); String fileName = file.getName();
inputStream = new BufferedInputStream(new FileInputStream(filePath)); setResponse(response, fileName);
try (InputStream inputStream = new BufferedInputStream(new FileInputStream(filePath));
OutputStream outputStream = response.getOutputStream()
) {
byte[] buf = new byte[1024];
int len;
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();
}
}else if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){ }else if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){
// minio 下载 // minio 下载
// 通过MinioUtil查询时 只需要桶后面的路径 // 通过MinioUtil查询时 只需要桶后面的路径
@@ -173,41 +186,32 @@ public class CommonController {
minioUrl = minioUrl + MinioUtil.getBucketName() + "/"; minioUrl = minioUrl + MinioUtil.getBucketName() + "/";
String url = fileUrl.replace(minioUrl, ""); String url = fileUrl.replace(minioUrl, "");
// 文件名称 // 文件名称
fileName = ossFile.getFileName(); String fileName = ossFile.getFileName();
inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url); setResponse(response, fileName);
} try (InputStream inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url);
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1)); OutputStream outputStream = response.getOutputStream()
response.setContentType("application/force-download");// 设置强制下载不打开 ) {
outputStream = response.getOutputStream();
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
int len; int len;
if(inputStream==null) if(inputStream==null) {
return; return;
}
while ((len = inputStream.read(buf)) > 0) { while ((len = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len); outputStream.write(buf, 0, len);
} }
response.flushBuffer(); response.flushBuffer();
} catch (IOException e) { }catch (Exception 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);
} }
} }
} }
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,6 +267,15 @@ 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();
getResult(result, responseBody);
return result;
} catch (Exception e) {
log.debug("中转HTTP请求失败", e);
return Result.error(e.getMessage());
}
}
private void getResult(Result<Object> result, String responseBody) {
try { try {
// 尝试将返回结果转为JSON // 尝试将返回结果转为JSON
Object json = JSON.parse(responseBody); Object json = JSON.parse(responseBody);
@@ -275,11 +284,16 @@ public class CommonController {
// 转成JSON失败,直接返回原始数据 // 转成JSON失败,直接返回原始数据
result.setResult(responseBody); result.setResult(responseBody);
} }
return result;
} catch (Exception e) {
log.debug("中转HTTP请求失败", e);
return Result.error(e.getMessage());
} }
private JSONObject getJsonObject(ServletServerHttpRequest httpRequest) {
JSONObject params;
try {
params = JSON.parseObject(JSON.toJSONString(httpRequest.getBody()));
} catch (Exception e) {
params = new JSONObject();
}
return params;
} }
/** /**
@@ -296,10 +310,6 @@ public class CommonController {
if(oConvertUtils.isEmpty(imgPath) || "null".equals(imgPath)){ if(oConvertUtils.isEmpty(imgPath) || "null".equals(imgPath)){
return; return;
} }
// 其余处理略
InputStream inputStream = null;
OutputStream outputStream = null;
try {
imgPath = imgPath.replace("..", "").replace("../",""); imgPath = imgPath.replace("..", "").replace("../","");
if (imgPath.endsWith(",")) { if (imgPath.endsWith(",")) {
imgPath = imgPath.substring(0, imgPath.length() - 1); imgPath = imgPath.substring(0, imgPath.length() - 1);
@@ -308,12 +318,13 @@ public class CommonController {
File file = new File(filePath); File file = new File(filePath);
if(!file.exists()){ if(!file.exists()){
response.setStatus(404); response.setStatus(404);
throw new RuntimeException("文件["+imgPath+"]不存在.."); throw new JeroBootException("文件["+imgPath+"]不存在..");
} }
response.setContentType("application/force-download");// 设置强制下载不打开 response.setContentType("application/force-download");// 设置强制下载不打开
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1)); 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(); try (InputStream inputStream = new BufferedInputStream(new FileInputStream(filePath));
OutputStream 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);
} }
} }
} }
}
}