|
@@ -24,6 +24,7 @@ import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.ssl.SSLContexts;
|
|
import org.apache.http.ssl.SSLContexts;
|
|
|
|
+import org.springframework.util.StreamUtils;
|
|
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
|
|
@@ -145,7 +146,7 @@ public class FileSECUtils {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
- public static InputStream decodeFileForSEC(long fileSize,InputStream inputStream){
|
|
|
|
|
|
+ public static InputStream decodeFileForSEC(int fileSize,InputStream inputStream){
|
|
Map<String, String> mapentity = CommonHelperUtils.getCommonParams("SEC");
|
|
Map<String, String> mapentity = CommonHelperUtils.getCommonParams("SEC");
|
|
if(mapentity == null){
|
|
if(mapentity == null){
|
|
return null;
|
|
return null;
|
|
@@ -276,59 +277,77 @@ public class FileSECUtils {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public static InputStream processFileWithSEC(String path,InputStream inputStream) {
|
|
|
|
- File tempFile = null;
|
|
|
|
- try {
|
|
|
|
- logger.info("--------------SEC附件解密 processFileWithSEC1 ----------------");
|
|
|
|
- String filename = getFileNameWithoutExtension(path);
|
|
|
|
- String filetype = getFileType(filename);
|
|
|
|
- /**
|
|
|
|
- * 判断是否是媒体文件,如果是媒体文件,则不进行处理
|
|
|
|
- */
|
|
|
|
- boolean isMedia = isMediaFile(filetype);
|
|
|
|
- if(isMedia){
|
|
|
|
- return inputStream;
|
|
|
|
- }
|
|
|
|
- String filepath = System.getProperty("java.io.tmpdir") + "/" + filename;
|
|
|
|
- // 1. 将 InputStream 写入本地临时文件
|
|
|
|
- tempFile = File.createTempFile(filepath,"."+filetype);
|
|
|
|
- logger.info("--------------SEC附件临时路径 "+filepath,"."+filetype+" ----------------");
|
|
|
|
- tempFile.deleteOnExit(); // 确保 JVM 退出时删除临时文件
|
|
|
|
|
|
+ public static InputStream processFileWithSEC(String path, InputStream inputStream) {
|
|
|
|
+// InputStream tmpInputStream = inputStream;
|
|
|
|
|
|
- try (FileOutputStream fos = new FileOutputStream(tempFile)) {
|
|
|
|
- byte[] buffer = new byte[1024];
|
|
|
|
- int bytesRead;
|
|
|
|
- while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
|
- fos.write(buffer, 0, bytesRead);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- //返回临时文件输入流,不能使用的文件流
|
|
|
|
- InputStream tmpInputStream = new FileInputStream(tempFile);
|
|
|
|
|
|
|
|
|
|
+//// File tempFile = null;
|
|
|
|
+ try {
|
|
|
|
+// logger.info("--------------SEC附件解密 processFileWithSEC1 ----------------");
|
|
|
|
+//// String filename = getFileNameWithoutExtension(path);
|
|
|
|
+//// String filetype = getFileType(filename);
|
|
|
|
+//// /**
|
|
|
|
+//// * 判断是否是媒体文件,如果是媒体文件,则不进行处理
|
|
|
|
+//// */
|
|
|
|
+//// boolean isMedia = isMediaFile(filetype);
|
|
|
|
+//// if(isMedia){
|
|
|
|
+//// return inputStream;
|
|
|
|
+//// }
|
|
|
|
+//// String filepath = System.getProperty("java.io.tmpdir") + "/" + filename;
|
|
|
|
+//// // 1. 将 InputStream 写入本地临时文件
|
|
|
|
+//// tempFile = File.createTempFile(filepath,"."+filetype);
|
|
|
|
+//// logger.info("--------------SEC附件临时路径 "+filepath,"."+filetype+" ----------------");
|
|
|
|
+//// tempFile.deleteOnExit(); // 确保 JVM 退出时删除临时文件
|
|
|
|
+////
|
|
|
|
+//// try (FileOutputStream fos = new FileOutputStream(tempFile)) {
|
|
|
|
+//// byte[] buffer = new byte[1024];
|
|
|
|
+//// int bytesRead;
|
|
|
|
+//// while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
|
+//// fos.write(buffer, 0, bytesRead);
|
|
|
|
+//// }
|
|
|
|
+//// }
|
|
|
|
+//// //返回临时文件输入流,不能使用的文件流
|
|
|
|
+//
|
|
|
|
+// if(true){
|
|
|
|
+// return inputStream;
|
|
|
|
+// }
|
|
|
|
+//
|
|
|
|
+//
|
|
|
|
+// InputStream tmpInputStream = inputStream;//new FileInputStream(tempFile);
|
|
|
|
+//
|
|
|
|
+//
|
|
|
|
+//
|
|
// 2. 调用是否加密接口方法
|
|
// 2. 调用是否加密接口方法
|
|
- int isEncrypted = checkFileIsEncryptionRest(new FileInputStream(tempFile));
|
|
|
|
|
|
+ int isEncrypted = checkFileIsEncryptionRest(inputStream);
|
|
if (isEncrypted == -1) {
|
|
if (isEncrypted == -1) {
|
|
logger.info("--------------SEC附件检查,文件加密状态检查失败----------------");
|
|
logger.info("--------------SEC附件检查,文件加密状态检查失败----------------");
|
|
- return tmpInputStream;
|
|
|
|
|
|
+ return inputStream;
|
|
} else if (isEncrypted == 0) {
|
|
} else if (isEncrypted == 0) {
|
|
logger.info("--------------SEC附件检查,是明文无需解密----------------");
|
|
logger.info("--------------SEC附件检查,是明文无需解密----------------");
|
|
- return tmpInputStream;
|
|
|
|
|
|
+ return inputStream;
|
|
}
|
|
}
|
|
|
|
+ int fileSize = 0;
|
|
|
|
+
|
|
|
|
+ byte[] bytes = StreamUtils.copyToByteArray(inputStream);
|
|
|
|
+ fileSize = inputStream.read(bytes);
|
|
|
|
+
|
|
|
|
+ logger.info("--------------SEC附件解密,文件大小:" + fileSize + "----------------");
|
|
|
|
|
|
// 3. 获取文件大小
|
|
// 3. 获取文件大小
|
|
- long fileSize = tempFile.length();
|
|
|
|
|
|
+// int fileSize = tempFile.length();
|
|
|
|
|
|
// 4. 调用解密方法
|
|
// 4. 调用解密方法
|
|
- InputStream decryptedInputStream = decodeFileForSEC(fileSize, new FileInputStream(tempFile));
|
|
|
|
|
|
+ InputStream decryptedInputStream = decodeFileForSEC(fileSize, inputStream);
|
|
if (decryptedInputStream == null) {
|
|
if (decryptedInputStream == null) {
|
|
logger.info("--------------SEC附件解密,文件解密失败----------------");
|
|
logger.info("--------------SEC附件解密,文件解密失败----------------");
|
|
- return tmpInputStream;
|
|
|
|
|
|
+ return inputStream;
|
|
}
|
|
}
|
|
|
|
|
|
- // 5. 删除临时文件
|
|
|
|
- if (!tempFile.delete()) {
|
|
|
|
- logger.info("--------------SEC附件解密,无法删除临时文件----------------");
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+// // 5. 删除临时文件
|
|
|
|
+// if (!tempFile.delete()) {
|
|
|
|
+// logger.info("--------------SEC附件解密,无法删除临时文件----------------");
|
|
|
|
+// }
|
|
|
|
|
|
// 返回解密后的输入流
|
|
// 返回解密后的输入流
|
|
return decryptedInputStream;
|
|
return decryptedInputStream;
|