xhchc

          危波帆墻,笑談只在桃花上;與誰共尚,風吹萬里浪; 相依相偎,不做黃泉想;莫惆悵,碧波潮生,一蕭自狂放……

           

          Struts2.0 文件上傳與下載全解析(轉)

          struts的上傳封裝的已經非常完美了,首先我們來看一下頁面

              <s:form action="saveDocument.action" method="post" enctype ="multipart/form-data">

                                      
          <td height="32" class="heder">
                                          上傳檔案 :
                                      
          </td>                
                                      
          <td align="left" bgcolor="#FFFFFF" class="main2">
                                          
          <s:file name="documentFile" />
                                      
          </td>

                                      
          <td align="center">
                                          
          <input type="submit" value="保  存" class="button" onclick="return nextsubmit();"/>
                                      
          </td>

              
          </s:form>

           
          主要關注的就是 <s:file name="documentFile" />    enctype ="multipart/form-data"

          在action中,我們來看

              private String documentFileContentType;
              
          private String documentFileFileName;
              
          private File documentFile;

              
          public String getDocumentFileContentType() {
                  
          return documentFileContentType;
              }


              
          public void setDocumentFileContentType(String documentFileContentType) {
                  
          this.documentFileContentType = documentFileContentType;
              }


              
          public String getDocumentFileFileName() {
                  
          return documentFileFileName;
              }


              
          public void setDocumentFileFileName(String documentFileFileName) {
                  
          this.documentFileFileName = documentFileFileName;
              }


              
          public File getDocumentFile() {
                  
          return documentFile;
              }


              
          public void setDocumentFile(File documentFile) {
                  
          this.documentFile = documentFile;
              }


              
          private void copy(File src, File dst) {     
                  InputStream in 
          = null;
                  OutputStream out 
          = null;
                  
          try{                
                      in 
          = new BufferedInputStream( new FileInputStream(src));
                      out 
          = new BufferedOutputStream( new FileOutputStream(dst)); 
                      
                      
          byte [] buffer = new byte [1024];    
                      
          while (in.read(buffer) > 0 )    
                          out.write(buffer);      
                      in.close();
                      out.close(); 
                  }
          catch (Exception e) {    
                      e.printStackTrace();    
                  }
               
                      
              }



              
          public String save(){

                  
          if(!documentFileFileName.equals("")){
                      
                  
                      String folder 
          = ServletActionContext.getServletContext()
                                          .getRealPath(
          "/archives");
                      
                      File rootDir 
          = new File(folder);
                      
                      
          if(!rootDir.exists())
                          rootDir.mkdirs();
                      
                      String fileEx 
          = documentFileFileName.substring(
                                          documentFileFileName.indexOf(
          "."), 
                                          documentFileFileName.length());
                      
                      String fileRealName 
          = documentFileFileName.substring(0, documentFileFileName.indexOf(".")) + String.valueOf(new Date().getTime())+fileEx;
                      
                      String fileName 
          = folder + "\" + fileRealName;
              
                      
                      copy(documentFile,
          new File(fileName));
                      

                  }

                          

                      
                  
          return "success";
              }

          documentFileContentType;   documentFileFileName;  documentFile;  上傳后這三個東西會自動注入進來,根據要求對文件名更改下,保存下

          好了,接著我們要提供下載,看看struts是怎么做的,網上關于這方面資料很少,就一個家伙把官方的showcase翻譯下,我再完整的走一遍流程

          在頁面中

          <s:url id="url" action="download"> <s:param name="inputPath">/archives/<s:property value="loc" /></s:param>
          </s:url>
          <s:a href="%{url}">下載</s:a>

          在action中

          import java.io.InputStream;
          import java.io.UnsupportedEncodingException;

          import org.apache.struts2.ServletActionContext;

          import com.opensymphony.xwork2.Action;
          import com.opensymphony.xwork2.ActionContext;



          public class FileDownloadAction implements Action {

              
              
          private String inputPath;
              
          public void setInputPath(String value) throws UnsupportedEncodingException {
                  inputPath 
          =  new String(value.getBytes("ISO-8859-1"),"UTF-8");
                  System.out.println();
              }


              
          public InputStream getInputStream() throws Exception {
                  
          return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
              }


              
          public String execute() throws Exception {
                  String fileName 
          = inputPath.substring(inputPath.lastIndexOf("/")+1, inputPath.length());
                  ServletActionContext.getResponse().setHeader(
          "Content-Disposition""attachment; filename="+new String(fileName.getBytes("gb2312"),"iso-8859-1"));

                  
          return SUCCESS;
              }


              
              

          }

          相應的XML配置

                  <action name="download" class="FileDownloadAction">
                      
          <result name="success" type="stream">
                          
          <param name="inputName">inputStream</param>
                          
          <param name="bufferSize">4096</param>
                      
          </result>
                  
          </action>

          這里要注意,在action中 inputPath =  new String(value.getBytes("ISO-8859-1"),"UTF-8");  需要轉換下
          另外在
          setHeader("Content-Disposition""attachment; filename="+new String(fileName.getBytes("gb2312"),"iso-8859-1"));
          這一步也是非常重要的。
          注意:第一個轉換,
          "ISO-8859-1"————"UTF-8"  UTF-8是根據你自己的編碼來處理
          第二個轉換,
          "gb2312"————"iso-8859-1"  你就不要改變了,不管你是什么編碼,都這么處理就是了,只要你的客戶用的是中文的操作系統,呵呵

          大家在官方例子showcase里看到的是這樣的

                  <action name="download" class="org.apache.struts2.showcase.filedownload.FileDownloadAction">
                      
          <param name="inputPath">/images/struts.gif</param>
                      
          <result name="success" type="stream">
                          
          <param name="contentType">image/gif</param>
                          
          <param name="inputName">inputStream</param>
                          
          <param name="contentDisposition">filename="struts.gif"</param>
                          
          <param name="bufferSize">4096</param>
                      
          </result>
                  
          </action>


          可以看到 inputPath 我們已經寫在了jsp的URL中了,contentType 這個東西也是大家比較惱火的,因為對于圖片、zip、rar、doc、word、txt都是不同的,我這里做了個實驗,干脆不要了,讓系統自己去判斷,發現可行,呵呵,可能struts會自動判斷,contentDisposition 我們也寫在action的response中了,剩下的2個inputname和bufferSize就讓它放著吧,反正不用改變,好了,經過上述的改變,終于符合業務需求了,呵呵

          posted on 2009-05-26 14:21 chu 閱讀(462) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆檔案

          我的鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 长乐市| 隆昌县| 苍山县| 高淳县| 正蓝旗| 灵宝市| 日土县| 溆浦县| 陆川县| 玉林市| 宜川县| 什邡市| 武夷山市| 松江区| 普定县| 郸城县| 高青县| 永宁县| 道孚县| 江阴市| 通城县| 新营市| 清新县| 枣强县| 钦州市| 敦煌市| 葫芦岛市| 龙州县| 西和县| 伊宁市| 财经| 安吉县| 九龙县| 富平县| 宜君县| 灵台县| 老河口市| 垫江县| 郯城县| 濉溪县| 壶关县|