|
@@ -13,6 +13,7 @@ import com.ruoyi.file.service.ISysFileService;
|
|
import com.ruoyi.file.utils.FileUploadUtils;
|
|
import com.ruoyi.file.utils.FileUploadUtils;
|
|
import com.ruoyi.file.utils.HuaWeiObsUtil;
|
|
import com.ruoyi.file.utils.HuaWeiObsUtil;
|
|
import com.ruoyi.system.api.domain.SysFile;
|
|
import com.ruoyi.system.api.domain.SysFile;
|
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -25,10 +26,9 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
import javax.annotation.PostConstruct;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
|
-import java.io.IOException;
|
|
|
|
-import java.io.InputStream;
|
|
|
|
|
|
+import java.io.*;
|
|
import java.net.URLDecoder;
|
|
import java.net.URLDecoder;
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 文件请求处理
|
|
* 文件请求处理
|
|
@@ -64,20 +64,16 @@ 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")) {
|
|
- try
|
|
|
|
- {
|
|
|
|
|
|
+ try {
|
|
// 上传并返回访问地址
|
|
// 上传并返回访问地址
|
|
String url = sysFileService.uploadFile(file);
|
|
String url = sysFileService.uploadFile(file);
|
|
SysFile sysFile = new SysFile();
|
|
SysFile sysFile = new SysFile();
|
|
sysFile.setName(FileUtils.getName(url));
|
|
sysFile.setName(FileUtils.getName(url));
|
|
sysFile.setUrl(url);
|
|
sysFile.setUrl(url);
|
|
return R.ok(sysFile);
|
|
return R.ok(sysFile);
|
|
- }
|
|
|
|
- catch (Exception e)
|
|
|
|
- {
|
|
|
|
|
|
+ } catch (Exception e) {
|
|
log.error("上传文件失败", e);
|
|
log.error("上传文件失败", e);
|
|
return R.fail(e.getMessage());
|
|
return R.fail(e.getMessage());
|
|
}
|
|
}
|
|
@@ -90,14 +86,13 @@ public class SysFileController
|
|
objectKey = objectKey.substring(1);
|
|
objectKey = objectKey.substring(1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- try
|
|
|
|
- {
|
|
|
|
|
|
+ try {
|
|
// 上传并返回访问地址
|
|
// 上传并返回访问地址
|
|
obsClient = new ObsClient(obsConfig.getAK(), obsConfig.getSK(), obsConfig.getENDPOINT());
|
|
obsClient = new ObsClient(obsConfig.getAK(), obsConfig.getSK(), obsConfig.getENDPOINT());
|
|
- PutObjectResult putObjectResult = obsClient.putObject(obsConfig.getBUCKET_NAME(), "mz/" + objectKey, file.getInputStream());
|
|
|
|
|
|
+ PutObjectResult putObjectResult = obsClient.putObject(obsConfig.getBUCKET_NAME(),
|
|
|
|
+ "mz/" + objectKey, file.getInputStream());
|
|
sysFile.setName(putObjectResult.getObjectKey());
|
|
sysFile.setName(putObjectResult.getObjectKey());
|
|
sysFile.setUrl(putObjectResult.getObjectUrl());
|
|
sysFile.setUrl(putObjectResult.getObjectUrl());
|
|
- sysFile.setStream(file.getInputStream());
|
|
|
|
return R.ok(sysFile);
|
|
return R.ok(sysFile);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("上传文件失败", e);
|
|
log.error("上传文件失败", e);
|
|
@@ -115,6 +110,22 @@ public class SysFileController
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private static ByteArrayOutputStream cloneInputStream(InputStream input) {
|
|
|
|
+ try {
|
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
|
+ int len;
|
|
|
|
+ while ((len = input.read(buffer)) > -1) {
|
|
|
|
+ baos.write(buffer, 0, len);
|
|
|
|
+ }
|
|
|
|
+ baos.flush();
|
|
|
|
+ return baos;
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
@PostMapping("commonUpload")
|
|
@PostMapping("commonUpload")
|
|
public R<SysFile> commonUpload(@RequestParam("file") MultipartFile file)
|
|
public R<SysFile> commonUpload(@RequestParam("file") MultipartFile file)
|
|
{
|
|
{
|
|
@@ -126,14 +137,25 @@ public class SysFileController
|
|
objectKey = objectKey.substring(1);
|
|
objectKey = objectKey.substring(1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ InputStream stream1 = null;
|
|
|
|
+ InputStream stream2 = null;
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- // 上传并返回访问地址
|
|
|
|
- obsClient = new ObsClient(obsConfig.getAK(), obsConfig.getSK(), obsConfig.getENDPOINT());
|
|
|
|
- PutObjectResult putObjectResult = obsClient.putObject(obsConfig.getBUCKET_NAME(), "mz/" + objectKey, file.getInputStream());
|
|
|
|
- sysFile.setName(putObjectResult.getObjectKey());
|
|
|
|
- sysFile.setUrl(putObjectResult.getObjectUrl());
|
|
|
|
- sysFile.setStream(file.getInputStream());
|
|
|
|
|
|
+ // 流分两份
|
|
|
|
+ ByteArrayOutputStream baos = cloneInputStream(file.getInputStream());
|
|
|
|
+ if (ObjectUtils.isNotEmpty(baos)) {
|
|
|
|
+ // 上传并返回访问地址
|
|
|
|
+ obsClient = new ObsClient(obsConfig.getAK(), obsConfig.getSK(), obsConfig.getENDPOINT());
|
|
|
|
+ // 打开两个新的输入流
|
|
|
|
+ stream1 = new ByteArrayInputStream(baos.toByteArray());
|
|
|
|
+ stream2 = new ByteArrayInputStream(baos.toByteArray());
|
|
|
|
+ PutObjectResult putObjectResult = obsClient.putObject(obsConfig.getBUCKET_NAME(), "mz/" + objectKey, stream1);
|
|
|
|
+ StringWriter writer = new StringWriter();
|
|
|
|
+ IOUtils.copy(stream2, writer, StandardCharsets.UTF_8.name());
|
|
|
|
+ sysFile.setStream(writer.toString());
|
|
|
|
+ sysFile.setName(putObjectResult.getObjectKey());
|
|
|
|
+ sysFile.setUrl(putObjectResult.getObjectUrl());
|
|
|
|
+ }
|
|
return R.ok(sysFile);
|
|
return R.ok(sysFile);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("上传文件失败", e);
|
|
log.error("上传文件失败", e);
|
|
@@ -147,6 +169,20 @@ public class SysFileController
|
|
log.error("OBS关闭连接报错!===============" + e.getMessage());
|
|
log.error("OBS关闭连接报错!===============" + e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ if (ObjectUtils.isNotEmpty(stream1)) {
|
|
|
|
+ try {
|
|
|
|
+ stream1.close();
|
|
|
|
+ } catch (IOException e1) {
|
|
|
|
+ log.error("OBS用流关闭失败=>" + e1.getMessage());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (ObjectUtils.isNotEmpty(stream2)) {
|
|
|
|
+ try {
|
|
|
|
+ stream2.close();
|
|
|
|
+ } catch (IOException e2) {
|
|
|
|
+ log.error("前端用流关闭失败=>" + e2.getMessage());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|