Struts 上传组件中文乱码问题

Struts上传实现很简单,不需要自己用什么common_fileupload包,struts中用一个FormFile的接口用到了common_fileupload包的东西。在StrutsForm中只要定义一个FormFile类型的属性就行了。


程序代码
protected FormFile file;
public FormFile getFile() {
return file;
}
public void setFile(FormFile theFile) {
this.file = theFile;
}


uploadFile.jsp文件:

程序代码
<html:form action="/uploadFile" enctype="multipart/form-data">
File Description : <html:text property="description"/>
<br />
Please select the file that you would like to upload: <br /><html:file property="file" /></p>
<html:submit/>
</html:form>


在jsp的Form定义里面 “enctype="multipart/form-data"”不能忘记,不然会出现一个“BeanUtils.populate”属性赋值的错误。


程序代码
UploadFileForm uploadFileForm = (UploadFileForm) form;
String desciption = uploadFileForm.getDescription();

FormFile file = uploadFileForm.getFile();
String fileName = file.getFileName();
String contentType = file.getContentType();
String size = (file.getFileSize() + " bytes");
String filePath = null;
try {
stream = file.getInputStream();// ームホトシ�カチネ�
filePath = getServlet().getServletContext().getRealPath("/upload");
OutputStream bos = new FileOutputStream(filePath + "/"+ file.getFileName());
System.out.println(filePath+"\\"+file.getFileName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
stream.close();
} catch (Exception e) {
// do some thing
}
info = "The file uploaded to the path:\""+filePath+"\"";
// log the success infomation
file.destroy();


这样文件上传基本就可以成功了,但是存在中文乱码问题。

在Action的excute方法里面设置

程序代码
String encoding = "gb2312";
request.setCharacterEncoding(encoding);
response.setContentType("text/html;charset=" + encoding + "");

但是利用form的get方法取出来的值仍然是乱码,后来知道了,Struts从request里利用BeanUtils.populate把值赋给form的时候已经是乱码的,excute方法里面设置是没用的,所以要在赋值之前进行编码转换,所以下面两种方法才是有效的:

1.在filter里面对request,response的编码进行设定
2.在RequestProcessor.process()里进行编码设定

方法一:filter过滤

程序代码
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
String encoding = "gb2312";
request.setCharacterEncoding(encoding);
response.setContentType("text/html;charset=" + encoding + "");

filterChain.doFilter(request,response);
}

web.xml中

程序代码
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>xxx.xxxx.xxxx.EncodingFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>


方法二:自己写一个RequestProcessor,

程序代码
[size=large]public class EncodingProcessor extends RequestProcessor {
public void process(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {

String encoding = "gb2312";
request.setCharacterEncoding(encoding);
response.setContentType("text/html;charset=" + encoding + "");
super.process(request, response);
}
}[/size]

并在struts-config.xml里设定:

程序代码
<controller processorClass="xxx.xxxx.xxxx.EncodingProcessor" />

出自 http://www.lurenjia.net/article.asp?id=184
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值