Commit 97d0dd48 authored by alex yao's avatar alex yao

feat(storage): 扩展文件上传功能支持自定义文件名

parent de20005a
...@@ -191,7 +191,7 @@ public class SoftwareCopyRightScheduler { ...@@ -191,7 +191,7 @@ public class SoftwareCopyRightScheduler {
JSONObject jsonObject = JSONObject.parseObject(obj.toString()); JSONObject jsonObject = JSONObject.parseObject(obj.toString());
JSONArray jsonArray = jsonObject.getJSONArray("result"); JSONArray jsonArray = jsonObject.getJSONArray("result");
if (jsonArray != null) { if (jsonArray != null) {
String url = createSourceCodeDocument(jsonArray); String url = createSourceCodeDocument(jsonArray, "源代码");
recordEntity.setSourceCode(url); recordEntity.setSourceCode(url);
isChange = true; isChange = true;
} }
...@@ -212,7 +212,7 @@ public class SoftwareCopyRightScheduler { ...@@ -212,7 +212,7 @@ public class SoftwareCopyRightScheduler {
JSONObject jsonObject = JSONObject.parseObject(obj.toString()); JSONObject jsonObject = JSONObject.parseObject(obj.toString());
String result = jsonObject.getString("result"); String result = jsonObject.getString("result");
if (result != null) { if (result != null) {
String url = createSimpleDocument(result); String url = createSimpleDocument(result, "电脑使用说明书");
recordEntity.setPcOperatingManual(url); recordEntity.setPcOperatingManual(url);
isChange = true; isChange = true;
} }
...@@ -234,7 +234,7 @@ public class SoftwareCopyRightScheduler { ...@@ -234,7 +234,7 @@ public class SoftwareCopyRightScheduler {
String result = jsonObject.getString("result"); String result = jsonObject.getString("result");
if (result != null) { if (result != null) {
String url = createSimpleDocument(result); String url = createSimpleDocument(result, "手机使用说明书");
recordEntity.setPhOperatingManual(url); recordEntity.setPhOperatingManual(url);
isChange = true; isChange = true;
} }
...@@ -324,20 +324,20 @@ public class SoftwareCopyRightScheduler { ...@@ -324,20 +324,20 @@ public class SoftwareCopyRightScheduler {
@Resource @Resource
private MD2WordService md2WordService; private MD2WordService md2WordService;
private String createSourceCodeDocument(JSONArray jsonArray) throws Exception { private String createSourceCodeDocument(JSONArray jsonArray, String fileName) throws Exception {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < jsonArray.size(); i++) { for (int i = 0; i < jsonArray.size(); i++) {
String content = jsonArray.getString(i); String content = jsonArray.getString(i);
stringBuilder.append(content); stringBuilder.append(content);
} }
return createSimpleDocument(stringBuilder.toString()); return createSimpleDocument(stringBuilder.toString(), fileName);
} }
private String createSimpleDocument(String content) throws Exception { private String createSimpleDocument(String content, String fileName) throws Exception {
return md2wordAndUploadURL(content); return md2wordAndUploadURL(content, fileName);
} }
private String md2wordAndUploadURL(String mdContent) throws Exception { private String md2wordAndUploadURL(String mdContent, String fileName) throws Exception {
File file = null; File file = null;
FileOutputStream fileOutputStream = null; FileOutputStream fileOutputStream = null;
FileInputStream fileInputStream = null; FileInputStream fileInputStream = null;
...@@ -351,7 +351,7 @@ public class SoftwareCopyRightScheduler { ...@@ -351,7 +351,7 @@ public class SoftwareCopyRightScheduler {
byte[] bytes = new byte[fileInputStream.available()]; byte[] bytes = new byte[fileInputStream.available()];
fileInputStream.read(bytes); fileInputStream.read(bytes);
String mdURL = uploadDocument(bytes, file.getName()); String mdURL = uploadDocument(bytes, fileName);
Md2WordResponse response = new Md2WordResponse(); Md2WordResponse response = new Md2WordResponse();
response.setFilePath(mdURL); response.setFilePath(mdURL);
Md2WordResult md2WordResult = md2WordService.md2Word(response); Md2WordResult md2WordResult = md2WordService.md2Word(response);
...@@ -369,10 +369,10 @@ public class SoftwareCopyRightScheduler { ...@@ -369,10 +369,10 @@ public class SoftwareCopyRightScheduler {
private String uploadDocument(byte[] documentBytes, String fileName) throws Exception { private String uploadDocument(byte[] documentBytes, String fileName) throws Exception {
File tempFile = null; File tempFile = null;
try { try {
tempFile = File.createTempFile(fileName, DOCX_EXTENSION); tempFile = File.createTempFile(UUIDUtils.getUUID8(), DOCX_EXTENSION);
FileUtil.writeBytes(documentBytes, tempFile); FileUtil.writeBytes(documentBytes, tempFile);
try (FileInputStream fileInputStream = new FileInputStream(tempFile)) { try (FileInputStream fileInputStream = new FileInputStream(tempFile)) {
return bosConfigService.upload(fileInputStream, DOCX_FILE_TYPE, ""); return bosConfigService.upload(fileInputStream, DOCX_FILE_TYPE, "", fileName);
} }
} finally { } finally {
if (tempFile != null && tempFile.exists()) { if (tempFile != null && tempFile.exists()) {
......
...@@ -7,12 +7,14 @@ import java.io.InputStream; ...@@ -7,12 +7,14 @@ import java.io.InputStream;
public interface BosConfigService extends BaseService { public interface BosConfigService extends BaseService {
String uploadFileByByteArray2Oss(byte[] decodedByte, String uploadFolder, String fileName, String fileType) throws IOException; String uploadFileByByteArray2Oss(byte[] decodedByte, String uploadFolder, String fileName, String fileType)
throws IOException;
String uploadFileByByteArray2Oss(byte[] decodedByte, String fileName, String fileType) throws IOException; String uploadFileByByteArray2Oss(byte[] decodedByte, String fileName, String fileType) throws IOException;
String upload(InputStream inputStream, String fileType, String contentType) throws IOException; String upload(InputStream inputStream, String fileType, String contentType) throws IOException;
String upload(InputStream inputStream, String fileType, String contentType, String fileName) throws IOException;
String uploadImageByBase64(String base64) throws IOException; String uploadImageByBase64(String base64) throws IOException;
......
...@@ -37,7 +37,8 @@ public class BosConfigServiceImpl implements BosConfigService { ...@@ -37,7 +37,8 @@ public class BosConfigServiceImpl implements BosConfigService {
@Override @Override
public String uploadFileByByteArray2Oss(byte[] decodedByte, String uploadFolder, String fileName, String fileType) throws IOException { public String uploadFileByByteArray2Oss(byte[] decodedByte, String uploadFolder, String fileName, String fileType)
throws IOException {
String BUCKET_NAME = fmxParamConfigService.getParam("bos.bucketName"); String BUCKET_NAME = fmxParamConfigService.getParam("bos.bucketName");
String END_POINT = fmxParamConfigService.getParam("bos.endPoint"); String END_POINT = fmxParamConfigService.getParam("bos.endPoint");
String FILE_NAME = fileName + "." + fileType; String FILE_NAME = fileName + "." + fileType;
...@@ -79,7 +80,8 @@ public class BosConfigServiceImpl implements BosConfigService { ...@@ -79,7 +80,8 @@ public class BosConfigServiceImpl implements BosConfigService {
} }
@Override @Override
public String upload(InputStream inputStream, String fileType, String contentType) { public String upload(InputStream inputStream, String fileType, String contentType, String fileName)
throws IOException {
String BUCKET_NAME = fmxParamConfigService.getParam("bos.bucketName"); String BUCKET_NAME = fmxParamConfigService.getParam("bos.bucketName");
String END_POINT = fmxParamConfigService.getParam("bos.endPoint"); String END_POINT = fmxParamConfigService.getParam("bos.endPoint");
String FILE_NAME = createFileName(fileType); String FILE_NAME = createFileName(fileType);
...@@ -89,7 +91,7 @@ public class BosConfigServiceImpl implements BosConfigService { ...@@ -89,7 +91,7 @@ public class BosConfigServiceImpl implements BosConfigService {
meta.setContentType(contentType); meta.setContentType(contentType);
// 设置内容被下载时的名称。 // 设置内容被下载时的名称。
if (StringUtils.isNoneBlank(FILE_NAME)) { if (StringUtils.isNoneBlank(FILE_NAME)) {
meta.setContentDisposition("attachment; filename=" + FILE_NAME); meta.setContentDisposition("attachment; filename=" + (fileName + "." + fileType));
} }
// 设置内容被下载时的编码格式。 // 设置内容被下载时的编码格式。
meta.setContentEncoding(StandardCharsets.UTF_8.displayName()); meta.setContentEncoding(StandardCharsets.UTF_8.displayName());
...@@ -130,6 +132,11 @@ public class BosConfigServiceImpl implements BosConfigService { ...@@ -130,6 +132,11 @@ public class BosConfigServiceImpl implements BosConfigService {
} }
} }
@Override
public String upload(InputStream inputStream, String fileType, String contentType) throws IOException {
return upload(inputStream, fileType, contentType, createFileName(fileType));
}
@Override @Override
public String uploadImageByBase64(String base64) throws IOException { public String uploadImageByBase64(String base64) throws IOException {
......
...@@ -73,7 +73,7 @@ public class ContentReportTest { ...@@ -73,7 +73,7 @@ public class ContentReportTest {
FileOutputStream ostream = new FileOutputStream(file); FileOutputStream ostream = new FileOutputStream(file);
poifs.writeFilesystem(ostream); poifs.writeFilesystem(ostream);
FileInputStream fileInputStream = new FileInputStream(file); FileInputStream fileInputStream = new FileInputStream(file);
String upload = bosConfigService.upload(fileInputStream, "docx", "application/msword"); String upload = bosConfigService.upload(fileInputStream, "docx", "application/msword","111-222");
bais.close(); bais.close();
ostream.close(); ostream.close();
poifs.close(); poifs.close();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment