`

Struts2上传

阅读更多
Struts2上传下载:
Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的文件。
需要导入的包文件:commons-fileupload-1.1.1.jar和commons-io-1.1.jar
FileUpload.jsp:
   < s:form action ="fileUpload" method ="POST" enctype ="multipart/form-data" >
        < s:file name ="myFile" label ="Image File" />
        < s:textfield name ="caption" label ="Caption" />       
        < s:submit />
    </ s:form >
FileUploadAction :
private File myFile;
private String contentType;
private String fileName;
private String imageFileName;
private String caption;
private static void copy(File src, File dst) {
       try {
          InputStream in = null ;
          OutputStream out = null ;
           try {               
              in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
              out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
                byte [] buffer = new byte [BUFFER_SIZE];
                  while (in.read(buffer) > 0 ) {
                    out.write(buffer);
                }
             } finally {
                  if ( null != in) {
                    in.close();
                }
                  if ( null != out) {
                    out.close();
                }
            }
         } catch (Exception e) {
            e.printStackTrace();
        }
    }
   
      private static String getExtention(String fileName) {
         int pos = fileName.lastIndexOf( " . " );
         return fileName.substring(pos);
       }
      public String execute()     {       
        imageFileName = new Date().getTime() + getExtention(fileName);
        File imageFile = new File(ServletActionContext.getServletContext().getRealPath( " /UploadImages " ) + " / " + imageFileName);
        copy(myFile, imageFile);
         return SUCCESS;
       }
在FileUploadAction中我分别写了setMyFileContentType、setMyFileFileName、setMyFile和setCaption四个Setter方法,后两者很容易明白,分别对应FileUpload.jsp中的<s:file/>和<s:textfield/>标志。但是前两者并没有显式地与任何的页面标志绑定,那么它们的值又是从何而来的呢?其实,<s:file/>标志不仅仅是绑定到myFile,还有myFileContentType(上传文件的MIME类型)和myFileFileName(上传文件的文件名,该文件名不包括文件的路径)。因此,<s:file name="xxx" />对应Action类里面的xxx、xxxContentType和xxxFileName三个属性。
FileUploadAction作用是将浏览器上传的文件拷贝到WEB应用程序的UploadImages文件夹下,新文件的名称是由系统时间与上传文件的后缀组成,该名称将被赋给imageFileName属性,以便上传成功的跳转页面使用。
上传成功页面(ShowUpload.jsp):
  < div style ="padding: 3px; border: solid 1px #cccccc; text-align: center" >
        < img src ='UploadImages/<s:property value ="imageFileName" /> ' />
        < br />
        < s:property value ="caption" />
    </ div >
Struts.xml:
< action name ="fileUpload" class ="tutorial.FileUploadAction" >
            < interceptor-ref name ="fileUploadStack" />
            < result name ="success" > /ShowUpload.jsp </ result >
</ action >
web.xml:
< filter >
        < filter-name > struts-cleanup </ filter-name >
        < filter-class >
            org.apache.struts2.dispatcher.ActionContextCleanUp
        </ filter-class >
</ filter >
< filter-mapping >
        < filter-name > struts-cleanup </ filter-name >
        < url-pattern > /* </ url-pattern >
</ filter-mapping >   






分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics