Strut架构中上传的中文文件名乱码问题?

WildGoose 2004-09-28 04:07:48
Action中的方法代码如下:

public ActionForward execute ( ActionMapping mapping ,
ActionForm form ,
HttpServletRequest request ,
HttpServletResponse response )
throws Exception
{
HttpSession session = request.getSession() ;
DynaActionForm df = ( DynaActionForm ) form ;
String fName = ( String ) df.get( "fname" ) ;
FormFile formFile = ( FormFile ) df.get( "upfile" ) ;
if ( fName.length() == 0 )
{
fName = StringUtil.cn( formFile.getFileName() ) ;
/*
不管怎么转,转成哪个字符集都是乱码
*/
}
String msg = "上传成功!" ;
if ( formFile.getFileData().length == 0 )
{
msg = "文件为空!" ;
}
else
{
Document doc = new Document() ;
doc.setName( fName ) ;
doc.setFilename( StringUtil.cn( formFile.getFileName() ) ) ;
doc.setExt( formFile.getContentType() ) ;
doc.setContent( formFile.getFileData() ) ;
doc.setType( "2" ) ;
doc.setUploadDatetime( new java.util.Date() ) ;
doc.setUploader( uc.getUser().getName() ) ;
doc.setSubType( nodeName ) ;
doc.setKind( kind ) ;
doc.setAppId( appId ) ;
DocMgt dm = new DocMgt() ;
dm.addDoc( doc ) ;
System.out.println( "finish save attachment" ) ;
}
request.setAttribute( "msg" , msg ) ;
return ( mapping.findForward( "filelist" ) ) ;
}

其中StringUtil.cn()方法是转换成GB2312字符集的,不过我试了好几种字符的转换都不行,请高手帮忙
...全文
286 点赞 收藏 22
写回复
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
WildGoose 2004-10-08
真可悲,这么久了还没有解决此问题
回复
WildGoose 2004-09-30
会不会是跟JBoss3.0.0-Tomcat4.0.3有关啊?
回复
WildGoose 2004-09-30
肯定存在啊
回复
miaoliujun 2004-09-30
你存在这个文件吗?
com.sdo.jmesis.util.SetEncodingFilter?
回复
WildGoose 2004-09-30
to winion :放在后面都不行,我一开始就是放在<taglib>后面的
楼上的兄台,如何加个过滤的servlet啊?
回复
WildGoose 2004-09-29
按你的方法我还没有试通,因为一加上<filter>的配置就访问不了网站啦
现在又得要解决这个问题了
回复
winion_c 2004-09-29
你是指传入的文件名就是乱码呢,还是输出信息是乱码?
按照我的方法,可以把网页传入的信息正确还原为汉字的。你的程序里输出了msg,要想看到msg的内容,输出时可能要添加 msg=new String(msg.getBytes(),"ISO8859-1");才行。
回复
WildGoose 2004-09-29
可是整个网站就只有这个地方是乱码,其它地方都不会出现乱码的情况啊
回复
QQzyb123456 2004-09-29
老大可以采用UTF-8编码过滤转化为UTF-8形式,下载时就可以恢复中文。
回复
WildGoose 2004-09-29
就是一加上<filter>的配置就访问不了网站了
回复
WildGoose 2004-09-29
我的Web.xml的全部内容如下:
<web-app>

<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.sdo.jmesis.util.SetEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- Standard Action Servlet Configuration (with debugging) -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>


<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>48000</session-timeout>
</session-config>

<mime-mapping id="MimeMapping_1">
<extension>html</extension>
<mime-type>text/html</mime-type>
</mime-mapping>
<mime-mapping id="MimeMapping_2">
<extension>doc</extension>
<mime-type>application/msword</mime-type>
</mime-mapping>
<mime-mapping id="MimeMapping_3">
<extension>htm</extension>
<mime-type>text/html</mime-type>
</mime-mapping>
<mime-mapping id="MimeMapping_4">
<extension>gif</extension>
<mime-type>image/gif</mime-type>
</mime-mapping>
<mime-mapping id="MimeMapping_5">
<extension>jpeg</extension>
<mime-type>image/jpeg</mime-type>
</mime-mapping>
<mime-mapping id="MimeMapping_6">
<extension>jpg</extension>
<mime-type>image/jpeg</mime-type>
</mime-mapping>
<mime-mapping id="MimeMapping_7">
<extension>pdf</extension>
<mime-type>application/pdf</mime-type>
</mime-mapping>
<mime-mapping id="MimeMapping_8">
<extension>zip</extension>
<mime-type>application/zip</mime-type>
</mime-mapping>
<mime-mapping id="MimeMapping_9">
<extension>class</extension>
<mime-type>application/x-java-vm</mime-type>
</mime-mapping>
<mime-mapping id="MimeMapping_10">
<extension>jar</extension>
<mime-type>application/x-java-archive</mime-type>
</mime-mapping>
<mime-mapping id="MimeMapping_11">
<extension>ser</extension>
<mime-type>application/x-java-serialized</mime-type>
</mime-mapping>
<mime-mapping id="MimeMapping_12">
<extension>exe</extension>
<mime-type>application/octet-stream</mime-type>
</mime-mapping>
<mime-mapping id="MimeMapping_13">
<extension>hdml</extension>
<mime-type>text/x-hdml</mime-type>
</mime-mapping>
<mime-mapping id="MimeMapping_14">
<extension>bmp</extension>
<mime-type>image/bmp</mime-type>
</mime-mapping>
<mime-mapping id="MimeMapping_15">
<extension>wml</extension>
<mime-type>text/vnd.wap.wml</mime-type>
</mime-mapping>
<mime-mapping id="MimeMapping_16">
<extension>wmls</extension>
<mime-type>text/vnd.wap.wmlscript</mime-type>
</mime-mapping>
<mime-mapping id="MimeMapping_17">
<extension>wmlc</extension>
<mime-type>application/vnd.wap.wmlc</mime-type>
</mime-mapping>
<mime-mapping id="MimeMapping_18">
<extension>wmlsc</extension>
<mime-type>application/vnd.wap.wmlscript</mime-type>
</mime-mapping>
<mime-mapping id="MimeMapping_19">
<extension>wbmp</extension>
<mime-type>image/vnd.wap.wbmp</mime-type>
</mime-mapping>

<!-- The Usual Welcome File List -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>


<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>

</web-app>
回复
WildGoose 2004-09-29
to zhengenyang(恩阳) :兄台,我已经按你的说法做了,虽然没有抛异常了,可是也访问不了网站啊,网页的错误信息如下:
Apache Tomcat/4.0.3 - HTTP Status 404 - /jmesis

--------------------------------------------------------------------------------

type Status report

message /jmesis

description The requested resource (/jmesis) is not available.

--------------------------------------------------------------------------------
回复
zhengenyang 2004-09-29
将winion_c(winion)说的filter加在
<display-name>***</display-name>和
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
之间,不能加在其它地方,要遵循dtd的定义的
回复
WildGoose 2004-09-29
to 小刀会: 兄台你的方法又该如何做呢?如何先对文件名进行编码啊?我的页面操作流程是:选择文件,然后上传.HTML代码如下:
<input type="file" name="upfile" value="浏览" class=button>
<input type="button" name="Submit22" value="上传" class=button onclick="javascript:upfileto()">
回复
WildGoose 2004-09-29
to winion :兄台,我按你的方法加了,可是抛异常了:
08:45:32,408 INFO [STDOUT] PARSE error at line 148 column -1
08:45:32,408 INFO [STDOUT] org.xml.sax.SAXParseException: 组件“web-app”在此不
允许“filter”。
08:45:32,408 ERROR [Engine] ContextConfig[/jmesis] Parse error in application we
b.xml
org.xml.sax.SAXParseException: 组件“web-app”在此不允许“filter”。
at org.apache.crimson.parser.Parser2.error(Parser2.java:3160)
at org.apache.crimson.parser.ValidatingParser$ChildrenValidator.consume(
ValidatingParser.java:349)
at org.apache.crimson.parser.Parser2.maybeElement(Parser2.java:1317)
at org.apache.crimson.parser.Parser2.content(Parser2.java:1779)
at org.apache.crimson.parser.Parser2.maybeElement(Parser2.java:1507)
at org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:500)
at org.apache.crimson.parser.Parser2.parse(Parser2.java:305)
at org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:442)

at org.xml.sax.helpers.XMLReaderAdapter.parse(XMLReaderAdapter.java:223)

at javax.xml.parsers.SAXParser.parse(SAXParser.java:314)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:89)
at org.apache.catalina.util.xml.XmlMapper.readXml(XmlMapper.java:275)
at org.apache.catalina.startup.ContextConfig.applicationConfig(ContextCo
nfig.java:256)
at org.apache.catalina.startup.ContextConfig.start(ContextConfig.java:82
4)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfi
g.java:224)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(Lifecycl
eSupport.java:155)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1131)

at org.apache.catalina.core.StandardContext.start(StandardContext.java:3
345)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:78
5)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:454)

at org.jboss.web.catalina.EmbeddedCatalinaServiceSX.createWebContext(Emb
eddedCatalinaServiceSX.java:321)
at org.jboss.web.catalina.EmbeddedCatalinaServiceSX.performDeploy(Embedd
edCatalinaServiceSX.java:238)
at org.jboss.web.AbstractWebContainer.start(AbstractWebContainer.java:30
0)
at org.jboss.deployment.MainDeployer.start(MainDeployer.java:678)
at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:513)
at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:481)
at sun.reflect.GeneratedMethodAccessor8.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.jboss.mx.capability.ReflectedMBeanDispatcher.invoke(ReflectedMBea
nDispatcher.java:284)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:491)
at org.jboss.util.jmx.MBeanProxy.invoke(MBeanProxy.java:174)
at $Proxy4.deploy(Unknown Source)
at org.jboss.deployment.scanner.URLDeploymentScanner.deploy(URLDeploymen
tScanner.java:405)
at org.jboss.deployment.scanner.URLDeploymentScanner.scan(URLDeploymentS
canner.java:515)
at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.
loop(AbstractDeploymentScanner.java:202)
at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.
run(AbstractDeploymentScanner.java:191)
08:45:32,468 INFO [Engine] ContextConfig[/jmesis]: Occurred at line 148 column
-1
08:45:32,468 INFO [Engine] ContextConfig[/jmesis]: Marking this application una
vailable due to previous error(s)
08:45:34,361 INFO [EmbeddedCatalinaServiceSX] Using Java2 parent classloader de
legation: true
08:45:34,361 INFO [Engine] StandardContext[/jmesis]: Context startup failed due
to previous errors
08:45:34,431 INFO [MainDeployer] Successfully completed deployment of package:

这该怎么解决啊?我用的是JBOSS+TOMCAT做服务器的
回复
kingbug 2004-09-29
加个过滤的strvlet
回复
winion_c 2004-09-29
把<filter>放在最后试试,就是放在<taglib>的后面。我的配置是放在后面的。
回复
WildGoose 2004-09-29
兄台,改成action后都还是不行啊,反正一加上了<filter>的配置就访问不了网站了
回复
WildGoose 2004-09-29
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.sdo.jmesis.util.SetEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
楼上的兄台,是不是将上面中的<url-pattern>/*</url-pattern>改成:<servlet-name>action</servlet-name>啊?
回复
zhengenyang 2004-09-29
改成
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
表示对所有的action进行过滤编码
filter是解决struts乱码问题的根本方法
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告