Commit 52a58a33 authored by alex yao's avatar alex yao

fix(bos): 解决文件名中文编码问题

parent 33e42427
...@@ -374,7 +374,7 @@ public class SoftwareCopyRightScheduler { ...@@ -374,7 +374,7 @@ public class SoftwareCopyRightScheduler {
tempFile = File.createTempFile(UUIDUtils.getUUID8(), 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, "", fileName); return bosConfigService.upload(fileInputStream, DOCX_FILE_TYPE, "");
} }
} finally { } finally {
if (tempFile != null && tempFile.exists()) { if (tempFile != null && tempFile.exists()) {
......
...@@ -14,6 +14,7 @@ import com.baidubce.services.bos.model.ObjectMetadata; ...@@ -14,6 +14,7 @@ import com.baidubce.services.bos.model.ObjectMetadata;
import com.baidubce.services.bos.model.PutObjectRequest; import com.baidubce.services.bos.model.PutObjectRequest;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.DateUtils; import org.apache.http.client.utils.DateUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
...@@ -23,6 +24,7 @@ import java.io.IOException; ...@@ -23,6 +24,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Base64; import java.util.Base64;
import java.util.Date; import java.util.Date;
...@@ -91,7 +93,9 @@ public class BosConfigServiceImpl implements BosConfigService { ...@@ -91,7 +93,9 @@ 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=" + fileName + "." + fileType); String downloadName = fileName + "." + fileType;
String encoded = URLEncoder.encode(downloadName, StandardCharsets.UTF_8.name());
meta.setContentDisposition("attachment; filename=" + encoded);
} }
// 设置内容被下载时的编码格式。 // 设置内容被下载时的编码格式。
meta.setContentEncoding(StandardCharsets.UTF_8.displayName()); meta.setContentEncoding(StandardCharsets.UTF_8.displayName());
......
...@@ -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","111-222"); String upload = bosConfigService.upload(fileInputStream, "docx", "application/msword","中文测试");
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