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

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

parent de20005a
......@@ -191,7 +191,7 @@ public class SoftwareCopyRightScheduler {
JSONObject jsonObject = JSONObject.parseObject(obj.toString());
JSONArray jsonArray = jsonObject.getJSONArray("result");
if (jsonArray != null) {
String url = createSourceCodeDocument(jsonArray);
String url = createSourceCodeDocument(jsonArray, "源代码");
recordEntity.setSourceCode(url);
isChange = true;
}
......@@ -212,7 +212,7 @@ public class SoftwareCopyRightScheduler {
JSONObject jsonObject = JSONObject.parseObject(obj.toString());
String result = jsonObject.getString("result");
if (result != null) {
String url = createSimpleDocument(result);
String url = createSimpleDocument(result, "电脑使用说明书");
recordEntity.setPcOperatingManual(url);
isChange = true;
}
......@@ -234,7 +234,7 @@ public class SoftwareCopyRightScheduler {
String result = jsonObject.getString("result");
if (result != null) {
String url = createSimpleDocument(result);
String url = createSimpleDocument(result, "手机使用说明书");
recordEntity.setPhOperatingManual(url);
isChange = true;
}
......@@ -324,20 +324,20 @@ public class SoftwareCopyRightScheduler {
@Resource
private MD2WordService md2WordService;
private String createSourceCodeDocument(JSONArray jsonArray) throws Exception {
private String createSourceCodeDocument(JSONArray jsonArray, String fileName) throws Exception {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < jsonArray.size(); i++) {
String content = jsonArray.getString(i);
stringBuilder.append(content);
}
return createSimpleDocument(stringBuilder.toString());
return createSimpleDocument(stringBuilder.toString(), fileName);
}
private String createSimpleDocument(String content) throws Exception {
return md2wordAndUploadURL(content);
private String createSimpleDocument(String content, String fileName) throws Exception {
return md2wordAndUploadURL(content, fileName);
}
private String md2wordAndUploadURL(String mdContent) throws Exception {
private String md2wordAndUploadURL(String mdContent, String fileName) throws Exception {
File file = null;
FileOutputStream fileOutputStream = null;
FileInputStream fileInputStream = null;
......@@ -351,7 +351,7 @@ public class SoftwareCopyRightScheduler {
byte[] bytes = new byte[fileInputStream.available()];
fileInputStream.read(bytes);
String mdURL = uploadDocument(bytes, file.getName());
String mdURL = uploadDocument(bytes, fileName);
Md2WordResponse response = new Md2WordResponse();
response.setFilePath(mdURL);
Md2WordResult md2WordResult = md2WordService.md2Word(response);
......@@ -369,10 +369,10 @@ public class SoftwareCopyRightScheduler {
private String uploadDocument(byte[] documentBytes, String fileName) throws Exception {
File tempFile = null;
try {
tempFile = File.createTempFile(fileName, DOCX_EXTENSION);
tempFile = File.createTempFile(UUIDUtils.getUUID8(), DOCX_EXTENSION);
FileUtil.writeBytes(documentBytes, tempFile);
try (FileInputStream fileInputStream = new FileInputStream(tempFile)) {
return bosConfigService.upload(fileInputStream, DOCX_FILE_TYPE, "");
return bosConfigService.upload(fileInputStream, DOCX_FILE_TYPE, "", fileName);
}
} finally {
if (tempFile != null && tempFile.exists()) {
......
......@@ -7,12 +7,14 @@ import java.io.InputStream;
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 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;
......
......@@ -37,7 +37,8 @@ public class BosConfigServiceImpl implements BosConfigService {
@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 END_POINT = fmxParamConfigService.getParam("bos.endPoint");
String FILE_NAME = fileName + "." + fileType;
......@@ -79,7 +80,8 @@ public class BosConfigServiceImpl implements BosConfigService {
}
@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 END_POINT = fmxParamConfigService.getParam("bos.endPoint");
String FILE_NAME = createFileName(fileType);
......@@ -89,7 +91,7 @@ public class BosConfigServiceImpl implements BosConfigService {
meta.setContentType(contentType);
// 设置内容被下载时的名称。
if (StringUtils.isNoneBlank(FILE_NAME)) {
meta.setContentDisposition("attachment; filename=" + FILE_NAME);
meta.setContentDisposition("attachment; filename=" + (fileName + "." + fileType));
}
// 设置内容被下载时的编码格式。
meta.setContentEncoding(StandardCharsets.UTF_8.displayName());
......@@ -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
public String uploadImageByBase64(String base64) throws IOException {
......
......@@ -73,7 +73,7 @@ public class ContentReportTest {
FileOutputStream ostream = new FileOutputStream(file);
poifs.writeFilesystem(ostream);
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();
ostream.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