struts文件上传以及中文乱码问题
生活随笔
收集整理的這篇文章主要介紹了
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,
程序代碼
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);
????}
}
并在struts-config.xml里設定:
程序代碼
<controller processorClass="xxx.xxxx.xxxx.EncodingProcessor" />
程序代碼
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,
程序代碼
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);
????}
}
并在struts-config.xml里設定:
程序代碼
<controller processorClass="xxx.xxxx.xxxx.EncodingProcessor" />
轉載于:https://blog.51cto.com/zhouhua/156308
總結
以上是生活随笔為你收集整理的struts文件上传以及中文乱码问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无法停止‘通用卷’设备的解决方法
- 下一篇: JPA EntityManager详解