|
@@ -75,20 +75,6 @@ public class SysFileController
|
|
@PostMapping("upload")
|
|
@PostMapping("upload")
|
|
public R<SysFile> upload(MultipartFile file) {
|
|
public R<SysFile> upload(MultipartFile file) {
|
|
if (!StringUtils.equals(SpringUtil.getActiveProfile(), "prod")) {
|
|
if (!StringUtils.equals(SpringUtil.getActiveProfile(), "prod")) {
|
|
- if (StringUtils.equals(SpringUtil.getActiveProfile(), "gljt") || StringUtils.equals(SpringUtil.getActiveProfile(), "dev")) {
|
|
|
|
- try {
|
|
|
|
- // 上传并返回访问地址
|
|
|
|
- MultipartFile shift = ShiftCryptoUtils.encryptMultipartFile(file);
|
|
|
|
- String url = sysFileService.uploadFile(shift);
|
|
|
|
- SysFile sysFile = new SysFile();
|
|
|
|
- sysFile.setName(FileUtils.getName(url));
|
|
|
|
- sysFile.setUrl(url);
|
|
|
|
- return R.ok(sysFile);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- log.error("上传文件失败", e);
|
|
|
|
- return R.fail(e.getMessage());
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
try {
|
|
try {
|
|
// 上传并返回访问地址
|
|
// 上传并返回访问地址
|
|
String url = sysFileService.uploadFile(file);
|
|
String url = sysFileService.uploadFile(file);
|
|
@@ -135,6 +121,61 @@ public class SysFileController
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @PostMapping("gljtUpload")
|
|
|
|
+ public R<SysFile> gljtUpload(MultipartFile file) {
|
|
|
|
+ MultipartFile shift;
|
|
|
|
+ try {
|
|
|
|
+ shift = ShiftCryptoUtils.encryptMultipartFile(file);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ return R.fail(e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ if (!StringUtils.equals(SpringUtil.getActiveProfile(), "prod")) {
|
|
|
|
+ try {
|
|
|
|
+ // 上传并返回访问地址
|
|
|
|
+ String url = sysFileService.uploadFile(shift);
|
|
|
|
+ SysFile sysFile = new SysFile();
|
|
|
|
+ sysFile.setName(FileUtils.getName(url));
|
|
|
|
+ sysFile.setUrl(replaceFirstOccurrence(url, "uploadPath", "gljtUploadPath"));
|
|
|
|
+ return R.ok(sysFile);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("上传文件失败", e);
|
|
|
|
+ return R.fail(e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ SysFile sysFile = new SysFile();
|
|
|
|
+ ObsClient obsClient = null;
|
|
|
|
+ String objectKey = FileUploadUtils.extractFilename(shift);
|
|
|
|
+ if (StringUtils.isNotEmpty(objectKey)) {
|
|
|
|
+ if (StringUtils.equals(objectKey.substring(0, 1), "/")) {
|
|
|
|
+ objectKey = objectKey.substring(1);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ // 上传并返回访问地址
|
|
|
|
+ obsClient = new ObsClient(obsConfig.getAK(), obsConfig.getSK(), obsConfig.getENDPOINT());
|
|
|
|
+ PutObjectResult putObjectResult = obsClient.putObject(obsConfig.getBUCKET_NAME(),
|
|
|
|
+ "mzylfwobs/uploadPath/" + objectKey, shift.getInputStream());
|
|
|
|
+ String[] arr = putObjectResult.getObjectKey().split("/");
|
|
|
|
+ String url = putObjectResult.getObjectKey().replaceAll("mzylfwobs", "");
|
|
|
|
+ sysFile.setName(arr[arr.length - 1]);
|
|
|
|
+ sysFile.setUrl(replaceFirstOccurrence(url, "uploadPath", "gljtUploadPath"));
|
|
|
|
+ return R.ok(sysFile);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("上传文件失败", e);
|
|
|
|
+ return R.fail(e.getMessage());
|
|
|
|
+ } finally {
|
|
|
|
+ if (ObjectUtils.isNotEmpty(obsClient)) {
|
|
|
|
+ try {
|
|
|
|
+ // 关闭OBS连接
|
|
|
|
+ obsClient.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ log.error("OBS关闭连接报错!===============" + e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
@PostMapping("uploadMul")
|
|
@PostMapping("uploadMul")
|
|
public R<SysFile> uploadMul(@RequestBody JSONObject fileStr) {
|
|
public R<SysFile> uploadMul(@RequestBody JSONObject fileStr) {
|
|
JSONArray fileArray = new JSONArray();
|
|
JSONArray fileArray = new JSONArray();
|
|
@@ -342,6 +383,32 @@ public class SysFileController
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @GetMapping("/gljtUploadPath/{year}/{month}/{day}/{fileName}")
|
|
|
|
+ public void gljtUploadPath(@PathVariable("year") String year, @PathVariable("month") String month,
|
|
|
|
+ @PathVariable("day") String day, @PathVariable("fileName") String fileName, HttpServletResponse resp) {
|
|
|
|
+ if (!StringUtils.equals(SpringUtil.getActiveProfile(), "prod")) {
|
|
|
|
+ try {
|
|
|
|
+ fileName = URLDecoder.decode(fileName, "utf-8");
|
|
|
|
+ String filePath = localFilePath + "/" + year + "/" + month + "/" + day + "/" + fileName;
|
|
|
|
+ System.out.println("本地路径<<<<======>>>>" + filePath);
|
|
|
|
+ buildResponse(resp, fileName);
|
|
|
|
+ gljtConvertOutputStream(resp, filePath);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("取得本地文件失败==》", e);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ try {
|
|
|
|
+ fileName = URLDecoder.decode(fileName, "utf-8");
|
|
|
|
+ String objectKey = "mzylfwobs/uploadPath/" + year + "/" + month + "/" + day + "/" + fileName;
|
|
|
|
+ System.out.println("本地路径<<<<======>>>>" + objectKey);
|
|
|
|
+ buildResponse(resp, fileName);
|
|
|
|
+ gljtConvertOutputStreamObs(resp, obsConfig.getBUCKET_NAME(), objectKey);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("取得OBS文件失败==》", e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
@GetMapping("/uploadPath/{year}/{month}/{fileName}")
|
|
@GetMapping("/uploadPath/{year}/{month}/{fileName}")
|
|
public void commonPreview(@PathVariable("year") String year, @PathVariable("month") String month,
|
|
public void commonPreview(@PathVariable("year") String year, @PathVariable("month") String month,
|
|
@PathVariable("fileName") String fileName, HttpServletResponse resp) {
|
|
@PathVariable("fileName") String fileName, HttpServletResponse resp) {
|
|
@@ -427,11 +494,34 @@ public class SysFileController
|
|
File file = new File(filePath);
|
|
File file = new File(filePath);
|
|
FileInputStream fileInputStream = new FileInputStream(file);
|
|
FileInputStream fileInputStream = new FileInputStream(file);
|
|
inputStream = new BufferedInputStream(fileInputStream);
|
|
inputStream = new BufferedInputStream(fileInputStream);
|
|
- if (StringUtils.equals(SpringUtil.getActiveProfile(), "gljt") || StringUtils.equals(SpringUtil.getActiveProfile(), "dev")) {
|
|
|
|
- byte[] in = IOUtils.toByteArray(inputStream);
|
|
|
|
- byte[] up = ShiftCryptoUtils.shiftDecrypt(in);
|
|
|
|
- inputStream = new ByteArrayInputStream(up);
|
|
|
|
|
|
+ byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
|
+ int len;
|
|
|
|
+ b = new byte[1024];
|
|
|
|
+ while ((len = inputStream.read(b)) != -1) {
|
|
|
|
+ byteArrayOutputStream.write(b, 0, len);
|
|
}
|
|
}
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ assert inputStream != null;
|
|
|
|
+ inputStream.close();
|
|
|
|
+ }
|
|
|
|
+ assert byteArrayOutputStream != null;
|
|
|
|
+ byteArrayOutputStream.writeTo(response.getOutputStream());
|
|
|
|
+ byteArrayOutputStream.close();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void gljtConvertOutputStream(HttpServletResponse response, String filePath) throws IOException {
|
|
|
|
+ ByteArrayOutputStream byteArrayOutputStream = null;
|
|
|
|
+ InputStream inputStream = null;
|
|
|
|
+ byte[] b;
|
|
|
|
+ try {
|
|
|
|
+ File file = new File(filePath);
|
|
|
|
+ FileInputStream fileInputStream = new FileInputStream(file);
|
|
|
|
+ inputStream = new BufferedInputStream(fileInputStream);
|
|
|
|
+ byte[] in = IOUtils.toByteArray(inputStream);
|
|
|
|
+ byte[] up = ShiftCryptoUtils.shiftDecrypt(in);
|
|
|
|
+ inputStream = new ByteArrayInputStream(up);
|
|
byteArrayOutputStream = new ByteArrayOutputStream();
|
|
byteArrayOutputStream = new ByteArrayOutputStream();
|
|
int len;
|
|
int len;
|
|
b = new byte[1024];
|
|
b = new byte[1024];
|
|
@@ -474,6 +564,34 @@ public class SysFileController
|
|
byteArrayOutputStream.close();
|
|
byteArrayOutputStream.close();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public void gljtConvertOutputStreamObs(HttpServletResponse response, String bucketName, String objectKey) throws IOException {
|
|
|
|
+ ObsClient obsClient = new ObsClient(obsConfig.getAK(), obsConfig.getSK(), obsConfig.getENDPOINT());
|
|
|
|
+ ObsObject obsObject = obsClient.getObject(bucketName,objectKey, null);
|
|
|
|
+ ByteArrayOutputStream byteArrayOutputStream = null;
|
|
|
|
+ InputStream inputStream = null;
|
|
|
|
+ byte[] b;
|
|
|
|
+ try {
|
|
|
|
+ inputStream = obsObject.getObjectContent();
|
|
|
|
+ byte[] in = IOUtils.toByteArray(inputStream);
|
|
|
|
+ byte[] up = ShiftCryptoUtils.shiftDecrypt(in);
|
|
|
|
+ inputStream = new ByteArrayInputStream(up);
|
|
|
|
+ byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
|
+ int len;
|
|
|
|
+ b = new byte[1024];
|
|
|
|
+ while ((len = inputStream.read(b)) != -1) {
|
|
|
|
+ byteArrayOutputStream.write(b, 0, len);
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ assert inputStream != null;
|
|
|
|
+ inputStream.close();
|
|
|
|
+ }
|
|
|
|
+ assert byteArrayOutputStream != null;
|
|
|
|
+ byteArrayOutputStream.writeTo(response.getOutputStream());
|
|
|
|
+ byteArrayOutputStream.close();
|
|
|
|
+ }
|
|
|
|
+
|
|
public void buildResponse(HttpServletResponse response, String fileName) throws UnsupportedEncodingException {
|
|
public void buildResponse(HttpServletResponse response, String fileName) throws UnsupportedEncodingException {
|
|
int index = fileName.lastIndexOf(".");
|
|
int index = fileName.lastIndexOf(".");
|
|
char[] ch = fileName.toCharArray();
|
|
char[] ch = fileName.toCharArray();
|
|
@@ -581,4 +699,22 @@ public class SysFileController
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 替换字符串中第一次出现的指定子串
|
|
|
|
+ * @param original 原始字符串
|
|
|
|
+ * @param target 需要替换的目标子串
|
|
|
|
+ * @param replacement 替换内容
|
|
|
|
+ * @return 替换后的新字符串
|
|
|
|
+ */
|
|
|
|
+ public static String replaceFirstOccurrence(String original, String target, String replacement) {
|
|
|
|
+ int index = original.indexOf(target);
|
|
|
|
+ if (index == -1) {
|
|
|
|
+ // 未找到目标子串时返回原始字符串
|
|
|
|
+ return original;
|
|
|
|
+ }
|
|
|
|
+ // 拼接新字符串:前半部分 + 替换内容 + 剩余部分
|
|
|
|
+ return original.substring(0, index)
|
|
|
|
+ + replacement
|
|
|
|
+ + original.substring(index + target.length());
|
|
|
|
+ }
|
|
}
|
|
}
|