Springmvc 上传文件 ,文件名中文乱码
Springmvc 上传文件 ,文件名中文乱码
鱼中鱼 发表于1年前
Springmvc 上传文件 ,文件名中文乱码
  • 发表于 1年前
  • 阅读 442
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

Springmvc 上传文件 ,文件名中文乱码。

spring 编码过滤器,tomcat,请求页面的编码都是utf-8,但接收到的中文文件名还是变成乱码了。

最后怀疑是 CommonsMultipartResolver编码问题,用以下方式查看后,都是utf-8
    public class MyMultipartResolver extends CommonsMultipartResolver { 
        private static final Log log = LogFactory.getLog(MyMultipartResolver.class);  
      
        //fileItem.getContentType() >request.getCharacterEncoding()> FileUpload().getHeaderEncoding > WebUtils.DEFAULT_CHARACTER_ENCODING  
        //表单元素的编码规则:fileItem.getContentType() >  determineEncoding(request) = request.getCharacterEncoding()> FileUpload().getHeaderEncoding > WebUtils.DEFAULT_CHARACTER_ENCODING  
        @Override  
        protected String determineEncoding(HttpServletRequest request) {  
            String encoding = request.getCharacterEncoding();  
            log.debug(" request.getCharacterEncoding --->" + encoding );  
            if (encoding == null) {  
                encoding = getDefaultEncoding();  
                log.debug(" getDefaultEncoding --->" + encoding );  
            }  
            return encoding ;   
        }  
      
        // CUSTOM  Encoding  
        @Override  
        protected String getDefaultEncoding() {  
            String encoding = getFileUpload().getHeaderEncoding();  
            log.debug(" getFileUpload().getHeaderEncoding --->" + encoding );  
            if (encoding == null) {  
                encoding = WebUtils.DEFAULT_CHARACTER_ENCODING;  
                log.debug(" WebUtils.DEFAULT_CHARACTER_ENCODING --->" + encoding );  
            }  
            return encoding;  
        }  
          
        @SuppressWarnings("unchecked")  
        protected MultipartParsingResult parseRequest(HttpServletRequest request) throws MultipartException {  
            String encoding = determineEncoding(request);  
            log.debug(" determineEncoding --->" + encoding );  
            FileUpload fileUpload = prepareFileUpload(encoding);  
            try {  
                List<FileItem> fileItems = ((ServletFileUpload) fileUpload).parseRequest(request);  
                return parseFileItems(fileItems, encoding);  
            }  
            catch (FileUploadBase.SizeLimitExceededException ex) {  
                throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex);  
            }  
            catch (FileUploadException ex) {  
                throw new MultipartException("Could not parse multipart servlet request", ex);  
            }  
        }  
      
           
        protected FileUpload prepareFileUpload(String encoding) {  
            FileUpload fileUpload = getFileUpload();  
            FileUpload actualFileUpload = fileUpload;  
      
            // Use new temporary FileUpload instance if the request specifies  
            // its own encoding that does not match the default encoding.  
            if (encoding != null && !encoding.equals(fileUpload.getHeaderEncoding())) {  
                actualFileUpload = newFileUpload(getFileItemFactory());  
                actualFileUpload.setSizeMax(fileUpload.getSizeMax());  
                actualFileUpload.setHeaderEncoding(encoding);  
            }  
            log.debug(" actualFileUpload  HeaderEncoding--->" + actualFileUpload.getHeaderEncoding() );  
            return actualFileUpload;  
        }  
      
      
    }  

最终看到了这个:

HttpEntity entity = MultipartEntityBuilder.create()

            .setMode(HttpMultipartMode.RFC6532)//BROWSER_COMPATIBLE自定义charset,RFC6532=utf-8,STRICT=iso-8859-1

            .addTextBody("name", testFile, textUtf8)//中文文件名还是需要传utf-8编码

            .addBinaryBody("file"new File(testFile))

            .build();

只有用这个HttpMultipartMode.RFC6532上传才是对的。

共有 人打赏支持
粉丝 13
博文 21
码字总数 6902
×
鱼中鱼
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: