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

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

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