FileUpload文件名编码 [英] FileUpload filename encoding

查看:212
本文介绍了FileUpload文件名编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一段时间了,因为我正在敲打这个:多部分/混合内容。

It's been quite a while since I'm banging my head against this: multipart/mixed content.

  @RequestPart(name="view") CoolView,
  @RequestPart(name="files") Part [] files

同样使用spring(因为CommonsMultipartResolver失败也没关系):

Also using spring's (it does not matter because CommonsMultipartResolver fails too) :

  StandardServletMultipartResolver

现在问题是,当上传具有US_ASCII字符之外的某些名称的文件时,服务器会将它们转换为奇怪的东西。奇怪的是,我的意思是它将它们转换为ISO_8859_1,我想我已经设想了UTF-8编码可以想象到的地方。

Now the thing is that when uploading files that have some names outside US_ASCII characters, the server is converting them into something weird. And by weird I mean it converts them to ISO_8859_1, and I think I've set the UTF-8 encoding everywhere imaginable.


  • -Dfile.encoding = UTF-8

  • LANG =en_US.UTF-8 LC_ALL =en_US.UTF-8

  • URIEncoding = UTF-8(在server.xml tomcat中)

  • CharacterEncodingFilter是第一个过滤器被接走

  • -Dfile.encoding=UTF-8
  • LANG="en_US.UTF-8" LC_ALL="en_US.UTF-8"
  • URIEncoding = UTF-8 (in server.xml tomcat)
  • CharacterEncodingFilter is the first filter that is being picked up
  • 有趣的是,只有当我在天蓝色的linux机器上尝试这个时才会发生这种情况,本地一切都很好。

    It's interesting that this only happens when I try this on the azure linux machine, locally everything is just fine.

    例如我使用curl发送一些要上传的文件:

    For example I'm using curl to send some files to be uploaded :

      curl -X POST -F "files=@Définition fonctionnalités.pdf" 
    

    在控制器I中我试图查看实际从Content-Disposition文件名中捕获的名称。

    And in Controller I'm trying to see the name that is actually being captured from Content-Disposition filename.

      Définition fonctionnalités.pdf 
    

    此时我会接受任何建议。谢谢你

    At this point I would accept any suggestion what-so-ever. thank u

    推荐答案

    根据你的描述,字符串Définitiononctionnalités.pdf 可以通过以下代码转换为正确的字符串。

    According to your description, the string Définition fonctionnalités.pdf can be converted into the correct string by the below code.

    String str = new String("Définition fonctionnalités.pdf".getBytes("ISO-8859-1"), "UTF-8");
    System.out.println(str); // Output is "Définition fonctionnalités.pdf"
    

    根据我的经验,它似乎是由如果开发环境在Windows上,则为Java源文件的字符编码格式。

    Per my experience, it seems to be caused by the Character Encoding format of your Java source file if your development environment is on Windows.

    因此,对于这种情况,解决方案是使用支持的文本编辑器 UTF-8 编译以确保Java源文件的编码格式在编译之前。

    So for the case, the solution is using the text editor supported UTF-8 Encoding to be sure the encoding format of Java source files before compiling them.

    如果您使用的是Eclipse IDE,则可以设置您项目的编码。步骤如下。

    If you are using Eclipse IDE, you can set the encoding for your project. The steps as below.


    1. 选择您的项目以右键单击以选择属性或直接输入组合键 Alt + Enter

    2. 在<$ c $中设置当前项目的文本文件编码c>资源标签,请参见下文。

    1. Select your project to right click to select the Properties or directly enter the combine keys Alt+Enter.
    2. Set up the text file encoding for the current project in the Resource tab, please see below.

    注意:如果为现有项目设置文本文件编码,则可能需要手动修复这些不正确的编码字符串。

    Note: If you set the text file encoding for the existing project, you may need to repair these incorrect encoding string manually。

    希望有所帮助。

    更新:请尝试在下面进行一些配置。

    Update: Please try to do some configurations below.


    1. 配置 server.xml 连接文件以启用 URIEncoding UTF-8

    <Connector port="80" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8"/> 
    


  • 为Java webapp添加全局过滤器。

  • Adding a global filter for your Java webapp.

    为您的项目配置 web.xml 文件

    <filter>
        <filter-name>charsetFilter</filter-name>
        <filter-class>com.XXXX.xxxx.CharsetFilter</filter-class>
        <init-param>
            <param-name>requestEncoding</param-name>
           <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>charsetFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    

    创建过滤器类 CharsetFilter

    public class CharsetFilter implements Filter {
        private String encoding;
        private Logger log = Logger.getLogger(this.getClass());
    
        public void init(FilterConfig config) throws ServletException {
            encoding = config.getInitParameter("requestEncoding");
            if (encoding == null) encoding = "UTF-8";
        }
    
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain next)
                throws IOException, ServletException {
            HttpServletRequest req = (HttpServletRequest) request;
            request.setCharacterEncoding(encoding);
            next.doFilter(request, response);
        }
    
        public void destroy() {
        }
    }
    




    1. 收到上传文件时,将文件名字符串的字节转换为 ISO-8859-1 进入 UTF-8 字符串,如上所述。

    1. When receiving the uploaded file, converting the bytes of the file name string with ISO-8859-1 into a UTF-8 string, as I said above.

    String originFN = ....;
    String fileName = new String(originFN.getBytes("ISO-8859-1"), "UTF-8");
    


  • 这篇关于FileUpload文件名编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆