Jsf 2.2 Upload与Apache Commons Upload中的文件名字符编码 [英] Filename characters encoding in Jsf 2.2 Upload vs. Apache Commons Upload

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

问题描述

Wildfly 8,Omnifaces 2.2,Primefaces 5.2,JSF 2.2.11(Mojarra)

Wildfly 8, Omnifaces 2.2, Primefaces 5.2, JSF 2.2.11 (Mojarra)

我正在使用Ominifaces CharacterEncodingFilter来确保文件名在服务器上正确编码.奇怪的是,如果Primefaces使用Jsf内部上传,则不会对文件名进行编码.并且如果Primefaces对Appache Commons使用较旧的方法也可以.

I am using Ominifaces CharacterEncodingFilter to ensure that file names are encoded correctly on server. Oddly if Primefaces using Jsf internal upload, the file name are not encoded. And if Primefaces use older approach with Appache Commons it is ok.

示例:Hällo.jpg"变为HÃllo.jpg"

Example: 'Hällo.jpg' becomes 'Hällo.jpg'

Web.xml配置: Apache解决方案(正确):

Web.xml configurations: Apache solution (correct):

<filter>
         <filter-name>PrimeFaces FileUpload Filter</filter-name>
         <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
   </filter>
   <filter-mapping>
         <filter-name>PrimeFaces FileUpload Filter</filter-name>
         <servlet-name>Faces Servlet</servlet-name>
   </filter-mapping>
   <context-param>
         <param-name>primefaces.UPLOADER</param-name>
         <param-value>commons</param-value>
   </context-param>

<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.omnifaces.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Jsf上载(未编码字符).其他参数将被删除.

Jsf Upload (characters are not encoded). Other parameters are removed.

<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.omnifaces.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

编辑:由于回答,这是服务器错误.我尝试配置服务器:

EDIT: due to answer this is a server bug. I've tried to configure the server:

<jboss-web>
 <default-encoding>UTF-8</default-encoding>
</jboss-web>

<servlet-container name="default" default-encoding="UTF-8">

但这没有帮助.

推荐答案

我复制了它,WildFly根本没有考虑multipart/form-data请求的请求请求主体编码.您确实必须在服务器端进行配置(就像对GET请求所做的一样).

I reproduced it, WildFly isn't at all considering request request body encoding for multipart/form-data requests. You really have to configure it in server end (like as you would do for GET requests).

打开/standalone/configuration/standalone.xml,查看以下行

<servlet-container name="default">

将其更改为

<servlet-container name="default" default-encoding="UTF-8">

,然后重新启动.这至少在WildFly 10.0.0上对我有用.我创建了问题 WFLY-6226 ,让它首先考虑请求主体编码,因此无需在此上编辑standalone.xml.

and restart. This at least worked for me on WildFly 10.0.0. I created issue WFLY-6226 to let it consider request body encoding first so there's no need to edit standalone.xml on that.

不幸的是,在WildFly 8.x(我测试了8.2.1)中,该功能仍然无法使用,因为它根本没有考虑上述设置.最好的选择是继续使用Apache Commons FileUpload,直到可以升级WildFly.

In WildFly 8.x (I tested 8.2.1) this unfortunately still won't work as it does not at all consider the above setting. Your best bet is to keep using Apache Commons FileUpload until you can upgrade WildFly.

如果您确实要保留本机上传,则可以考虑使用ISO-8859-1将破损的文件名显式解码为字节,然后使用UTF-8重新编码.

If you really want to keep native upload, then you could consider to explicitly decode the broken filename to bytes using ISO-8859-1 and then re-encode it using UTF-8.

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

但是,它很脆弱且不可移植,因为当部署到这种编码问题不明显的服务器上时,它会破裂.因此,您确实需要记住在升级/迁移时还原该解决方法.

This is however brittle and not portable as it would break when deployed to a server where this encoding problem doesn't manifest. So you really need to remember to revert the workaround when upgrading/migrating.

这篇关于Jsf 2.2 Upload与Apache Commons Upload中的文件名字符编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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