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

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


uploadFile.jsp文件:
code.gif 程序代码
<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”属性赋值的错误。


code.gif 程序代码
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方法里面设置
code.gif 程序代码
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过滤
code.gif 程序代码
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中

code.gif 程序代码
<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,
code.gif 程序代码
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里设定:
code.gif 程序代码
<controller processorClass="xxx.xxxx.xxxx.EncodingProcessor" />