Commit 20660123 authored by 施翔轲's avatar 施翔轲

文件名解码防止上传中文名文件导致的文件名乱码

parent a3b5c1bc
......@@ -22,6 +22,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
......@@ -70,7 +71,7 @@ public class SysOssController extends BaseController {
@SaCheckPermission("system:oss:upload")
@Log(title = "OSS对象存储", businessType = BusinessType.INSERT)
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public R<Map<String, String>> upload(@RequestPart("file") MultipartFile file) {
public R<Map<String, String>> upload(@RequestPart("file") MultipartFile file) throws UnsupportedEncodingException {
if (ObjectUtil.isNull(file)) {
return R.fail("上传文件不能为空");
}
......@@ -90,7 +91,7 @@ public class SysOssController extends BaseController {
@SaCheckPermission("system:oss:download")
@GetMapping("/download/{ossId}")
public void download(@PathVariable Long ossId, HttpServletResponse response) throws IOException {
iSysOssService.download(ossId,response);
iSysOssService.download(ossId, response);
}
/**
......
......@@ -19,6 +19,7 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
......@@ -108,7 +109,7 @@ public class SysProfileController extends BaseController {
*/
@Log(title = "用户头像", businessType = BusinessType.UPDATE)
@PostMapping(value = "/avatar", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public R<Map<String, Object>> avatar(@RequestPart("avatarfile") MultipartFile avatarfile) {
public R<Map<String, Object>> avatar(@RequestPart("avatarfile") MultipartFile avatarfile) throws UnsupportedEncodingException {
Map<String, Object> ajax = new HashMap<>();
if (!avatarfile.isEmpty()) {
String extension = FileUtil.extName(avatarfile.getOriginalFilename());
......
......@@ -10,6 +10,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.List;
......@@ -26,7 +27,7 @@ public interface ISysOssService {
SysOssVo getById(Long ossId);
SysOssVo upload(MultipartFile file);
SysOssVo upload(MultipartFile file) throws UnsupportedEncodingException;
SysOssVo upload(File file);
......
......@@ -34,6 +34,8 @@ import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
......@@ -113,7 +115,7 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName());
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8");
OssClient storage = OssFactory.instance(sysOss.getService());
try(InputStream inputStream = storage.getObjectContent(sysOss.getUrl())) {
try (InputStream inputStream = storage.getObjectContent(sysOss.getUrl())) {
int available = inputStream.available();
IoUtil.copy(inputStream, response.getOutputStream(), available);
response.setContentLength(available);
......@@ -123,8 +125,10 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
}
@Override
public SysOssVo upload(MultipartFile file) {
public SysOssVo upload(MultipartFile file) throws UnsupportedEncodingException {
String originalfileName = file.getOriginalFilename();
//文件名解码防止上传中文名文件导致的文件名乱码
originalfileName = URLDecoder.decode(originalfileName,"UTF-8");
String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
OssClient storage = OssFactory.instance();
UploadResult uploadResult;
......
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