|
@@ -1,5 +1,6 @@
|
|
package com.ruoyi.file.controller;
|
|
package com.ruoyi.file.controller;
|
|
|
|
|
|
|
|
+import cn.hutool.extra.spring.SpringUtil;
|
|
import com.obs.services.ObsClient;
|
|
import com.obs.services.ObsClient;
|
|
import com.obs.services.model.DeleteObjectResult;
|
|
import com.obs.services.model.DeleteObjectResult;
|
|
import com.obs.services.model.ObsObject;
|
|
import com.obs.services.model.ObsObject;
|
|
@@ -9,14 +10,17 @@ import com.ruoyi.common.core.utils.StringUtils;
|
|
import com.ruoyi.common.core.utils.file.FileUtils;
|
|
import com.ruoyi.common.core.utils.file.FileUtils;
|
|
import com.ruoyi.file.config.ObsConfig;
|
|
import com.ruoyi.file.config.ObsConfig;
|
|
import com.ruoyi.file.service.ISysFileService;
|
|
import com.ruoyi.file.service.ISysFileService;
|
|
|
|
+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.lang3.ObjectUtils;
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
|
-import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
import javax.annotation.PostConstruct;
|
|
@@ -40,8 +44,6 @@ public class SysFileController
|
|
private ISysFileService sysFileService;
|
|
private ISysFileService sysFileService;
|
|
@Autowired
|
|
@Autowired
|
|
private ObsConfig obsConfig;
|
|
private ObsConfig obsConfig;
|
|
- @Value("${file.path}")
|
|
|
|
- private String localFilePath;
|
|
|
|
|
|
|
|
// private ObsClient obsClient;
|
|
// private ObsClient obsClient;
|
|
|
|
|
|
@@ -54,10 +56,8 @@ public class SysFileController
|
|
private void loadingObsConfig() {
|
|
private void loadingObsConfig() {
|
|
obsConfig.setAK("PFUCYWN1PHHNIZAS9E9L");
|
|
obsConfig.setAK("PFUCYWN1PHHNIZAS9E9L");
|
|
obsConfig.setSK("4g3bggtamms8p8dcC1dnha1d9dUmyXgy8gqMH8dU");
|
|
obsConfig.setSK("4g3bggtamms8p8dcC1dnha1d9dUmyXgy8gqMH8dU");
|
|
- obsConfig.setBUCKET_NAME("obsylfwxt");
|
|
|
|
|
|
+ obsConfig.setBUCKET_NAME("obsjmgcxt");
|
|
obsConfig.setENDPOINT("14.125.0.146");
|
|
obsConfig.setENDPOINT("14.125.0.146");
|
|
-// obsConfig.setPARENT_DIR(localFilePath);
|
|
|
|
-// obsClient = new ObsClient(obsConfig.getAK(), obsConfig.getSK(), obsConfig.getENDPOINT());
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -66,27 +66,61 @@ public class SysFileController
|
|
@PostMapping("upload")
|
|
@PostMapping("upload")
|
|
public R<SysFile> upload(MultipartFile file)
|
|
public R<SysFile> upload(MultipartFile file)
|
|
{
|
|
{
|
|
- try
|
|
|
|
- {
|
|
|
|
- // 上传并返回访问地址
|
|
|
|
- String url = sysFileService.uploadFile(file);
|
|
|
|
|
|
+ if (!StringUtils.equals(SpringUtil.getActiveProfile(), "prod")) {
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ // 上传并返回访问地址
|
|
|
|
+ String url = sysFileService.uploadFile(file);
|
|
|
|
+ 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());
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
SysFile sysFile = new SysFile();
|
|
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());
|
|
|
|
|
|
+ ObsClient obsClient = null;
|
|
|
|
+ String objectKey = FileUploadUtils.extractFilename(file);
|
|
|
|
+ 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(), "mz/" + objectKey, file.getInputStream());
|
|
|
|
+ sysFile.setName(putObjectResult.getObjectKey());
|
|
|
|
+ sysFile.setUrl(putObjectResult.getObjectUrl());
|
|
|
|
+ sysFile.setStream(file.getInputStream());
|
|
|
|
+ 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("commonUpload")
|
|
@PostMapping("commonUpload")
|
|
- public R<SysFile> commonUpload(@RequestParam("file") MultipartFile file, @RequestParam("classification") String classification, @RequestParam("objectKey") String objectKey)
|
|
|
|
|
|
+ public R<SysFile> commonUpload(@RequestParam("file") MultipartFile file)
|
|
{
|
|
{
|
|
SysFile sysFile = new SysFile();
|
|
SysFile sysFile = new SysFile();
|
|
ObsClient obsClient = null;
|
|
ObsClient obsClient = null;
|
|
|
|
+ String objectKey = FileUploadUtils.extractFilename(file);
|
|
if (StringUtils.isNotEmpty(objectKey)) {
|
|
if (StringUtils.isNotEmpty(objectKey)) {
|
|
if (StringUtils.equals(objectKey.substring(0, 1), "/")) {
|
|
if (StringUtils.equals(objectKey.substring(0, 1), "/")) {
|
|
objectKey = objectKey.substring(1);
|
|
objectKey = objectKey.substring(1);
|
|
@@ -99,6 +133,7 @@ public class SysFileController
|
|
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);
|
|
@@ -124,14 +159,10 @@ public class SysFileController
|
|
int index = fileName.lastIndexOf(".");
|
|
int index = fileName.lastIndexOf(".");
|
|
char[] ch = fileName.toCharArray();
|
|
char[] ch = fileName.toCharArray();
|
|
String lastString = String.copyValueOf(ch, index + 1, ch.length - index - 1);
|
|
String lastString = String.copyValueOf(ch, index + 1, ch.length - index - 1);
|
|
-// resp.setHeader("Content-Type","application/octet-stream");
|
|
|
|
-// resp.setContentType("application/octet-stream");
|
|
|
|
resp.setCharacterEncoding("utf-8");
|
|
resp.setCharacterEncoding("utf-8");
|
|
if (lastString.equals("jpg") || lastString.equals("jpeg") || lastString.equals("png") || lastString.equals("gif") || lastString.equals("bmp")) {
|
|
if (lastString.equals("jpg") || lastString.equals("jpeg") || lastString.equals("png") || lastString.equals("gif") || lastString.equals("bmp")) {
|
|
resp.setContentType("image/jpeg");
|
|
resp.setContentType("image/jpeg");
|
|
resp.setContentType("image/jpg");
|
|
resp.setContentType("image/jpg");
|
|
-// resp.setHeader("Content-Type","image/jpeg");
|
|
|
|
-// resp.setHeader("content-disposition","inline; filename="+new String(fileName.getBytes(),"utf-8"));
|
|
|
|
} else {
|
|
} else {
|
|
resp.setHeader("content-disposition", "attachment; filename=" + new String(fileName.getBytes(), "utf-8"));
|
|
resp.setHeader("content-disposition", "attachment; filename=" + new String(fileName.getBytes(), "utf-8"));
|
|
}
|
|
}
|
|
@@ -140,46 +171,6 @@ public class SysFileController
|
|
log.error("取得文件失败==》", e);
|
|
log.error("取得文件失败==》", e);
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
-// response.setContentType("application/octet-stream");
|
|
|
|
-// response.setCharacterEncoding("utf-8");
|
|
|
|
-// SysFile sysFile = new SysFile();
|
|
|
|
-// ObsClient obsClient = null;
|
|
|
|
-// InputStream inputStream = null;
|
|
|
|
-// try {
|
|
|
|
-// obsClient = new ObsClient(obsConfig.getAK(), obsConfig.getSK(), obsConfig.getENDPOINT());
|
|
|
|
-// ObsObject obsObject = obsClient.getObject(obsConfig.getBUCKET_NAME(), objectKey, null);
|
|
|
|
-// if (ObjectUtils.isNotEmpty(obsObject)) {
|
|
|
|
-// sysFile.setName(objectKey);
|
|
|
|
-// sysFile.setUrl(obsConfig.getPARENT_DIR() + objectKey);
|
|
|
|
-//// sysFile.setStream(obsObject.getObjectContent());
|
|
|
|
-// }
|
|
|
|
-// ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
|
-// inputStream = obsObject.getObjectContent();
|
|
|
|
-// byte[] b;
|
|
|
|
-// int len;
|
|
|
|
-// b = new byte[1024];
|
|
|
|
-// while ((len = inputStream.read(b)) != -1) {
|
|
|
|
-// byteArrayOutputStream.write(b, 0, len);
|
|
|
|
-// }
|
|
|
|
-// byteArrayOutputStream.writeTo(response.getOutputStream());
|
|
|
|
-//// response.setContentType("image/jpeg;charset=GB2312");
|
|
|
|
-// } catch (Exception e) {
|
|
|
|
-// log.error("取得文件预览失败", e);
|
|
|
|
-// } finally {
|
|
|
|
-// if (ObjectUtils.isNotEmpty(obsClient)) {
|
|
|
|
-// try {
|
|
|
|
-// // 关闭OBS连接
|
|
|
|
-// obsClient.close();
|
|
|
|
-// if (ObjectUtils.isNotEmpty(inputStream)) {
|
|
|
|
-// inputStream.close();
|
|
|
|
-// }
|
|
|
|
-// } catch (IOException e) {
|
|
|
|
-// log.error("OBS关闭连接报错!===============" + e.getMessage());
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
public void convertOutputStream2(HttpServletResponse response, String bucketName, String objectKey) throws IOException {
|
|
public void convertOutputStream2(HttpServletResponse response, String bucketName, String objectKey) throws IOException {
|
|
@@ -217,11 +208,6 @@ public class SysFileController
|
|
DeleteObjectResult res = obsClient.deleteObject(obsConfig.getBUCKET_NAME(), objectKey);
|
|
DeleteObjectResult res = obsClient.deleteObject(obsConfig.getBUCKET_NAME(), objectKey);
|
|
System.out.println(res);
|
|
System.out.println(res);
|
|
if (ObjectUtils.isNotEmpty(res)) {
|
|
if (ObjectUtils.isNotEmpty(res)) {
|
|
-// if (res.isDeleteMarker()) {
|
|
|
|
-// return R.ok("文件删除成功");
|
|
|
|
-// } else {
|
|
|
|
-// return R.fail("找到了该文件但是文件删除失败");
|
|
|
|
-// }
|
|
|
|
return R.ok("文件删除成功");
|
|
return R.ok("文件删除成功");
|
|
} else {
|
|
} else {
|
|
return R.fail("删除文件失败未找到该文件");
|
|
return R.fail("删除文件失败未找到该文件");
|
|
@@ -296,14 +282,6 @@ public class SysFileController
|
|
}else{
|
|
}else{
|
|
return R.fail("上传文件失败");
|
|
return R.fail("上传文件失败");
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
- // 上传并返回访问地址
|
|
|
|
-// String url = sysFileService.uploadFile(file);
|
|
|
|
-// SysFile sysFile = new SysFile();
|
|
|
|
-// sysFile.setName(FileUtils.getName(url));
|
|
|
|
-// sysFile.setUrl(url);
|
|
|
|
-// return R.ok(sysFile);
|
|
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
catch (Exception e)
|
|
{
|
|
{
|