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

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

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