修复部分sonar问题

This commit is contained in:
zer0Black
2023-12-01 16:15:23 +08:00
parent d936a816fd
commit 988dee844d
14 changed files with 244 additions and 234 deletions
@@ -369,7 +369,7 @@ public class ReportManageConroller {
@ApiOperation(value = "上传图片")
@PostMapping("/uploadPicFile")
public ResponseMessage<Map<String, Object>> uploadPicFile(MultipartFile file, HttpServletRequest request) throws IOException {
Random r = new Random();
try (InputStream input = file.getInputStream()) {
Map<String, Object> resultMap = new HashMap();
String path = uploadFile;//此路径映射到localhost
@@ -381,13 +381,12 @@ public class ReportManageConroller {
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String date = dateFormat.format(now);
Random r = new Random();
String num = "";
for (int i = 0; i < 3; i++) {
int s = r.nextInt(10);
num += String.valueOf(s);
}
//获取上传文件名
String fileName = file.getOriginalFilename();
String contentType = file.getContentType();
@@ -500,6 +499,7 @@ public class ReportManageConroller {
//如果只下载一个文件,则不压缩
if (downLoadFileIds.size() == 1) {
String fileId = downLoadFileIds.get(0);
FileOutputStream outputStream = null;
try {
FileEntity entity = fileService.getFile(fileId);
String fileName = entity.getFileName();
@@ -510,7 +510,7 @@ public class ReportManageConroller {
//PDF文件需要加水印
if (FILE_EXTENSIONS_PDF.equalsIgnoreCase(FileUtil.getFileExtension(fileName))) {
String path = uploadFile + entity.getSavePath();
FileOutputStream outputStream = new FileOutputStream(path);
outputStream = new FileOutputStream(path);
// 上传方式:阿里云:alioss 华为云:hwobs
if ("hwobs".equals(uploadType)) {
obsBootUtil.getFileFromOBS(entity.getSavePath(), outputStream);
@@ -521,7 +521,7 @@ public class ReportManageConroller {
}
//给文件添加水印
waterMarkUtil.addWatermarkToPdfAndWriteToResponse(new FileInputStream(path), response);
outputStream.close();
} else {
// 上传方式:阿里云:alioss 华为云:hwobs 本地:local
if ("hwobs".equals(uploadType)){
@@ -535,6 +535,10 @@ public class ReportManageConroller {
} catch (Exception ex) {
log.info(ex.getMessage(),ex);
} finally {
if (outputStream != null){
outputStream.close();
}
}
} else {
List<String> filePaths = new ArrayList<>();
@@ -545,14 +549,14 @@ public class ReportManageConroller {
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "no-store");
response.setHeader("Expires", "0");
FileOutputStream outputStream = null;
for (String fileId : downLoadFileIds) {
try {
FileEntity entity = fileService.getFile(fileId);
String fileName = entity.getFileName();
String path = uploadFile + entity.getSavePath();
FileOutputStream outputStream = new FileOutputStream(path);
outputStream = new FileOutputStream(path);
filePaths.add(path);
// 上传方式:阿里云:alioss 华为云:hwobs
@@ -567,9 +571,12 @@ public class ReportManageConroller {
if (FILE_EXTENSIONS_PDF.equalsIgnoreCase(FileUtil.getFileExtension(fileName))) {
waterMarkUtil.savePdfToFile(new FileInputStream(path), path);
}
outputStream.close();
} catch (Exception ex) {
log.info(ex.getMessage(),ex);
} finally {
if (outputStream != null){
outputStream.close();
}
}
}
//将文件压缩 写入Response
@@ -656,6 +663,7 @@ public class ReportManageConroller {
//如果只下载一个文件,则不压缩
if (downLoadFileIds.size() == 1) {
String fileId = downLoadFileIds.get(0);
FileOutputStream outputStream = null;
try {
FileEntity entity = fileService.getFile(fileId);
String fileName = entity.getFileName();
@@ -666,7 +674,7 @@ public class ReportManageConroller {
//PDF文件需要加水印
if (FILE_EXTENSIONS_PDF.equalsIgnoreCase(FileUtil.getFileExtension(fileName))) {
String path = uploadFile + entity.getSavePath();
FileOutputStream outputStream = new FileOutputStream(path);
outputStream = new FileOutputStream(path);
// 上传方式:阿里云:alioss 华为云:hwobs
if ("hwobs".equals(uploadType)) {
obsBootUtil.getFileFromOBS(entity.getSavePath(), outputStream);
@@ -677,7 +685,6 @@ public class ReportManageConroller {
}
//给文件添加水印
waterMarkUtil.addWatermarkToPdfAndWriteToResponse(new FileInputStream(path), response);
outputStream.close();
} else {
// 上传方式:阿里云:alioss 华为云:hwobs 本地:local
if ("hwobs".equals(uploadType)){
@@ -691,6 +698,10 @@ public class ReportManageConroller {
} catch (Exception ex) {
log.info(ex.getMessage(),ex);
} finally {
if (outputStream != null){
outputStream.close();
}
}
} else {
List<String> filePaths = new ArrayList<>();
@@ -701,14 +712,14 @@ public class ReportManageConroller {
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "no-store");
response.setHeader("Expires", "0");
FileOutputStream outputStream = null;
for (String fileId : downLoadFileIds) {
try {
FileEntity entity = fileService.getFile(fileId);
String fileName = entity.getFileName();
String path = uploadFile + entity.getSavePath();
FileOutputStream outputStream = new FileOutputStream(path);
outputStream = new FileOutputStream(path);
filePaths.add(path);
// 上传方式:阿里云:alioss 华为云:hwobs
@@ -723,9 +734,12 @@ public class ReportManageConroller {
if (FILE_EXTENSIONS_PDF.equalsIgnoreCase(FileUtil.getFileExtension(fileName))) {
waterMarkUtil.savePdfToFile(new FileInputStream(path), path);
}
outputStream.close();
} catch (Exception ex) {
log.info(ex.getMessage(),ex);
} finally {
if (outputStream != null){
outputStream.close();
}
}
}
//将文件压缩 写入Response
@@ -18,6 +18,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;
@@ -49,6 +50,12 @@ public class IFileServiceImpl implements IFileService {
@Resource
private OSSClientUtil ossClientUtil;
private static Random r;
static {
r = new Random();
}
/**
* 添加文件
* @param entity
@@ -82,7 +89,7 @@ public class IFileServiceImpl implements IFileService {
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String date = dateFormat.format(now);
Random r = new Random();
String num = "";
for (int i = 0; i < 3; i++) {
int s = r.nextInt(10);
@@ -137,7 +144,6 @@ public class IFileServiceImpl implements IFileService {
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String date = dateFormat.format(now);
Random r = new Random();
String num = "";
for (int i = 0; i < 3; i++) {
int s = r.nextInt(10);
@@ -950,14 +950,23 @@ public class IReportServiceImpl extends ServiceImpl<ReportDao, ReportEntity>
public String createHtmlString(String fileId) throws Exception {
FileEntity fileEntity = fileDao.selectById(fileId);
String path = uploadFile + fileEntity.getSavePath();
FileOutputStream outputStream = new FileOutputStream(path);
// 上传方式:阿里云:alioss 华为云:hwobs
if ("hwobs".equals(uploadType)) {
obsBootUtil.getFileFromOBS(fileEntity.getSavePath(), outputStream);
} else if ("alioss".equals(uploadType)) {
ossClientUtil.getFileFromOSS(fileEntity.getSavePath(), outputStream);
} else if ("local".equals(uploadType)) {
localFileUtil.getFileFromLocal(fileEntity.getSavePath(), outputStream);
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(path);
// 上传方式:阿里云:alioss 华为云:hwobs
if ("hwobs".equals(uploadType)) {
obsBootUtil.getFileFromOBS(fileEntity.getSavePath(), outputStream);
} else if ("alioss".equals(uploadType)) {
ossClientUtil.getFileFromOSS(fileEntity.getSavePath(), outputStream);
} else if ("local".equals(uploadType)) {
localFileUtil.getFileFromLocal(fileEntity.getSavePath(), outputStream);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (outputStream != null){
outputStream.close();
}
}
String html = null;
@@ -91,9 +91,13 @@ public class FileContentUtil {
default:
}
} catch (NotOfficeXmlFileException e) {
fis.close();
log.info("文件内容抽取失败,原因可能是上传的文件为空文件");
} finally {
if (fis != null){
fis.close();
}
}
return cleanInvalidCharacters(content.toString());
}
@@ -40,15 +40,24 @@ public class LocalFileUtil {
ZipEntry zipEntry = new ZipEntry(sourceFile.getName());
zipOutputStream.putNextEntry(zipEntry);
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(sourceFile);
FileInputStream fileInputStream = new FileInputStream(sourceFile);
int len;
while ((len = fileInputStream.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, len);
}
int len;
while ((len = fileInputStream.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, len);
fileInputStream.close();
} catch (Exception e){
e.printStackTrace();
} finally {
if (fileInputStream != null){
fileInputStream.close();
}
}
fileInputStream.close();
zipOutputStream.closeEntry();
}
@@ -62,14 +71,28 @@ public class LocalFileUtil {
public void saveFile(InputStream fileInputStream, String fileName) throws IOException {
String path = localFilePath + fileName;
FileOutputStream fileOutputStream = new FileOutputStream(path);
byte[] buffer = new byte[1024];
int length;
while ((length = fileInputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, length);
FileOutputStream fileOutputStream= null;
try {
fileOutputStream = new FileOutputStream(path);
byte[] buffer = new byte[1024];
int length;
while ((length = fileInputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, length);
}
fileInputStream.close();
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null){
fileOutputStream.close();
}
if (fileInputStream != null){
fileInputStream.close();
}
}
fileInputStream.close();
fileOutputStream.close();
}
public void getFileFromLocal(String savePath, FileOutputStream outputStream) throws IOException {
@@ -80,22 +103,30 @@ public class LocalFileUtil {
if (!sourceFile.exists()) {
throw new IOException("Source file does not exist.");
}
FileInputStream inputStream = null;
try {
// 创建输入流
inputStream = new FileInputStream(sourceFile);
// 创建输入流
FileInputStream inputStream = new FileInputStream(sourceFile);
// 创建缓冲区
byte[] buffer = new byte[1024];
int bytesRead;
// 创建缓冲区
byte[] buffer = new byte[1024];
int bytesRead;
// 从输入流读取数据,并写入输出流
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null){
inputStream.close();
}
// 从输入流读取数据,并写入输出流
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
if (outputStream != null){
outputStream.close();
}
}
// 关闭输入流和输出流
inputStream.close();
outputStream.close();
}
public void downFile(String savePath, HttpServletResponse response) throws IOException {
@@ -89,7 +89,9 @@ public class ObsBootUtil {
throw new AdcDaBaseException("OBS下载文件失败");
}finally {
// 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
objectContent.close();
if (objectContent != null) {
objectContent.close();
}
response.getOutputStream().close();
}
@@ -112,8 +114,13 @@ public class ObsBootUtil {
throw new AdcDaBaseException("OBS下载文件失败");
}finally {
// 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
objectContent.close();
outputStream.close();
if (objectContent != null){
objectContent.close();
}
if (outputStream != null){
outputStream.close();
}
}
}