<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Cyh的博客

    Email:kissyan4916@163.com
    posts - 26, comments - 19, trackbacks - 0, articles - 220
         關(guān)于如何在項(xiàng)目中添加struts2這里就不在多說了,struts2上傳下載所需要的jar包大家可以去www.apache.org進(jìn)行下載,在Jakarta下的選擇
     Commons,然后下載FileUpload 和 IO這兩個(gè)jar包。解壓后拷貝到你的項(xiàng)目lib文件夾下就OK了。

         下面直接開始struts2的上傳:

         首先創(chuàng)建一個(gè)文件上傳的Action,以下注釋部分為批量上傳時(shí)的代碼,大同小異!
        
    public class UploadAction extends ActionSupport {
      
       。。。省略了 getter 和 setter方法
      
       
    private String title;
        
    //單個(gè)文件 
        private File upload;
        
        
        
    // 多個(gè)文件
        
    // private String[] upload;
        
    //保存上傳文件的文件夾名字,可在配置文件中動(dòng)態(tài)指定
       
        
    private String savePath;
        
        
    private String uploadContentType;
       
        
    private String uploadFileName;

        
    public String getSavePath() {
             String s 
    =  ServletActionContext.getRequest().getRealPath(savePath);
             System.out.println(s);
             
    return s;
        }

        @Override
        
    public String execute() throws Exception {
            System.out.println(
    "進(jìn)入 execute方法");
            
    //查看上傳文件的大小
            System.out.println("所上傳的文件的大小為"+getUpload().length());
            
            
    //查看上傳文件的名字
            System.out.println("UploadFileName:"+getUploadFileName());
            
            
    //指定上傳文件到哪個(gè)文件夾
            FileOutputStream fos = new FileOutputStream(savePath+"\\"
                    
    +getUploadFileName());
            
    //取得一個(gè)將要上傳的文件的一個(gè)輸入流
            FileInputStream fis = new FileInputStream(getUpload());
            
            
    //當(dāng)圖片需要存儲(chǔ)進(jìn)數(shù)據(jù)庫時(shí),可以將圖片的連接存儲(chǔ)進(jìn)數(shù)據(jù)庫,譬如:
            
    //path路徑是需要讀進(jìn)數(shù)據(jù)庫滴,所以這里不能使用 getSavePath(),因?yàn)檫@樣返回的是絕對(duì)路徑。
            String path = savePath+"\\"+getUploadFileName();
            
            
    byte[] buffer = new byte[1024];
            
            
    int len = 0 ;
            
            
    while((len = fis.read(buffer))>0) {
                fos.write(buffer, 
    0, len);
            }
            System.out.println(
    "文件已上傳");
            
            
    return SUCCESS;
        }

        
    //完成多個(gè)文件的上傳
    //    public String execute() throws Exception {
    //
    //        
    //        //查看上傳文件的名字
    //        System.out.println("UploadFileName:"+getUploadFileName());
    //        
    //        //指定上傳文件到哪個(gè)文件夾
    //        FileOutputStream fos = new FileOutputStream(savePath+"\\"
    //                +getUploadFileName());
    //        
    //        String[] ss = getUpload();
    //        for(int i=0;i<ss.length;i++){
    //            //取得一個(gè)將要上傳的文件的一個(gè)輸入流
    //        FileInputStream fis = new FileInputStream(ss[i]);
    //        
    //        //當(dāng)圖片需要存儲(chǔ)進(jìn)數(shù)據(jù)庫時(shí),可以將圖片的連接存儲(chǔ)進(jìn)數(shù)據(jù)庫,譬如:
    //        //path路徑是需要讀進(jìn)數(shù)據(jù)庫滴
    //        String path = savePath+"\\"+getUploadFileName();
    //        
    //        byte[] buffer = new byte[1024];
    //        
    //        int len = 0 ;
    //        
    //        while((len = fis.read(buffer))>0) {
    //            fos.write(buffer, 0, len);
    //        }    

    //        return SUCCESS ; 
    //    }
      

        
    }

    文件上傳的頁面index.jsp
    <body>
          
    <s:fielderror/>
          
    <s:form action="ryanUpload" enctype="multipart/form-data">
            
    <s:textfield label="title" name="title"/>
                
    <!-- 單個(gè)文件上傳 -->
             
    <s:file name="upload" label="文件1"/>     
            
    <!-- 多個(gè)文件上傳 
           
            
    <s:file name="upload" label="文件1"/>
            
    <s:file name="upload" label="文件2"/>
            
    <s:file name="upload" label="文件3"/>

          
    -->
          
    <s:submit/>
          
    </s:form>
      
    </body>
    在來看看struts.xml中的配置,
    <struts>
         
    <constant name="struts.i18n.encoding" value="GBK"/>
         
         
    <package name="ryan" extends="struts-default">
         
            
    <action name="ryanUpload" class="com.UploadAction">
                
    <result name="success">/succ.jsp</result>
              
                
    <result name="input">/index.jsp</result>
               
                
    <!-- 動(dòng)態(tài)指定上傳文件的目錄 -->
                 
    <param name="savePath">/upload</param>   <!--  在此處可以動(dòng)態(tài)的指定服務(wù)器上存儲(chǔ)圖片的文件夾 --> 
                
                
    <interceptor-ref name="fileUpload">
                   
    <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
                   
    <param name="maximumSize">3000000000</param>  <!--  這里的單位為字節(jié)  -->
                
    </interceptor-ref>
                
                
    <interceptor-ref name="defaultStack"/>
            
    </action>
          
             
            
        
    </package>

    </struts>

    struts2下載(參考了beansoft的文章):
       
       
      一般在項(xiàng)目中所用到下載都是直接根據(jù)數(shù)據(jù)庫讀出的地址來進(jìn)行下載,譬如有下面一個(gè)鏈接:
     
       <!-- 假設(shè)有這樣一條從數(shù)據(jù)庫中讀出的數(shù)據(jù) ,它的存儲(chǔ)路徑為 downLoad/ryan.jsp ,文件名字為ryan.jsp
                    很明顯這是一張圖片,那么我們點(diǎn)擊下載時(shí)就會(huì)根據(jù)path來進(jìn)行下載
                                                         
    -->

        <s:url action="downLoad" id="down">
          <s:param name="inputPath">downLoad/ryan.jsp</s:param>
          <s:param name="fileName">ryan.jsp</s:param>
        </s:url>
       
        <s:a href="%{down}">downLoad</s:a>


    來看看我們的struts.xml中的配置

    <struts>
      
    <package  name="technicalInformationAction"   extends="Action">
       
        
    <default-action-ref name="download"/>
     

         
    <action name="downLoad" class="com.DownLoadAction">
                   
                    <!--這里的返回值并不是字符串,而是一個(gè)流類型-->  
                    
    <result name="success" type="stream">
                    
                    
    <param name="contentType">application/octet-stream;charset=ISO8859-1</param>
                    
                    
    <param name="inputName">inputStream</param>
                 
           
                     <!--這里需要將文件名進(jìn)行轉(zhuǎn)碼,轉(zhuǎn)碼后將值賦予
    downloadFileName, 對(duì)應(yīng)文中的getDownloadFileName()-->        
                    
    <param name="contentDisposition">attachment;filename="${downloadFileName}"</param> 
                   
                    
    <param name="bufferSize">4096</param>
                    
                    
    </result> 
         
    </action> 
      
      
    </package>
    </struts>

    下載文件的DownLoadAction:

    public class DownLoadAction implements Action{

        
    private String fileName;// 初始的通過param指定的文件名屬性

        
    private String inputPath;// 指定要被下載的文件路徑
        
        String username 
    = "chenyanhu";

        
    public InputStream getInputStream() throws Exception {

        
    // 通過 ServletContext,也就是application 來讀取數(shù)據(jù)
       
        
    return ServletActionContext.getServletContext().getResourceAsStream("/"+inputPath);

        }
        
         /**
          *   execute中代碼的含義是為了防止他人惡意的下載項(xiàng)目的WEB-INF/web.xml文件,譬如在瀏覽器中輸入
          *  http://......./downLoad.action?inputPath=WEB-INF/web.xml  這樣將下載下的文件后綴名改為xml就可以查看你的項(xiàng)目的web.xml了

          *  execute中代碼的意思是將文件存儲(chǔ)的真實(shí)目錄與地址欄的路徑進(jìn)行對(duì)比。
    發(fā)現(xiàn)企圖下載不在/download下的文件,就顯示內(nèi)容為空
          */ 
       

    public
     String execute()
            
            
    // 文件下載目錄路徑

            String downloadDir = ServletActionContext.getServletContext().getRealPath("/technicalInformationDirectory"+"/"+username);

            
    // 文件下載路徑

            String downloadFile 
    = ServletActionContext.getServletContext().getRealPath("/"+inputPath);

            java.io.File file 
    = new java.io.File(downloadFile);
       try{
            downloadFile 
    = file.getCanonicalPath();// 真實(shí)文件路徑,去掉里面的..等信息

            
    if(!downloadFile.startsWith(downloadDir)) {

            
    return null;

            }
       }catch (IOException io) {
            // TODO: handle exception
            System.out.println("IO異常!!路徑發(fā)生錯(cuò)誤!");
            io.printStackTrace();
        }catch (SecurityException se) {
            // TODO: handle exception
            System.out.println("安全性異常!!");
            se.printStackTrace();
        }catch (NullPointerException  npe ) {
            System.out.println("空指針異常");
            npe.printStackTrace();
        }catch (Exception e) {
            System.out.println("出現(xiàn)異常了!");
            e.printStackTrace();
        }
            
    return SUCCESS;     
            

        }



        
    // 提供轉(zhuǎn)換編碼后的供下載用的文件名 

        
    public String getDownloadFileName() {

        String downFileName 
    = fileName;

        
    try {

        downFileName 
    = new String(downFileName.getBytes(), "ISO8859-1");

        } 
    catch (UnsupportedEncodingException e) {

        e.printStackTrace();

        }

        
    return downFileName;

        }
      


        }
       


    關(guān)于中文轉(zhuǎn)碼的問題:
        
          我在項(xiàng)目中遇到了一個(gè)問題,那就是下載文件的時(shí)候,不能對(duì)含有中文名字的文件進(jìn)行下載,老是出現(xiàn)亂碼,但是數(shù)據(jù)庫中
      
        是沒有亂碼的,今天花了比較長的時(shí)間去解決這個(gè)問題,不過現(xiàn)在我已經(jīng)把這個(gè)問題解決, 就是需要修改tomcat的server.xml文件  :
     
                  <Connector port="8641" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443" URIEncoding="utf-8"/>
      
        加上紅色部分就OK了!!




                                                                                                           --    學(xué)海無涯
            

    Feedback

    # re: sturts2的上傳和下載以及中文轉(zhuǎn)碼問題  回復(fù)  更多評(píng)論   

    2009-09-05 09:21 by aasd
    sdf
    主站蜘蛛池模板: 四虎永久免费影院| 国产在线观看片a免费观看 | 午夜成年女人毛片免费观看| h片在线免费观看| 97免费人妻在线视频| 久久综合九色综合97免费下载 | 亚洲中文字幕无码亚洲成A人片| 亚洲日韩中文字幕天堂不卡| 亚洲特级aaaaaa毛片| 亚洲性猛交xx乱| tom影院亚洲国产一区二区| 久久亚洲精品专区蓝色区| 亚洲高清视频在线| 久久精品国产亚洲AV未满十八| 羞羞视频免费网站含羞草| jizz免费观看视频| 最好免费观看高清在线| 99ee6热久久免费精品6| 曰曰鲁夜夜免费播放视频 | 67194成手机免费观看| 国产精彩免费视频| 性xxxx视频播放免费| 免费A级毛片无码A| 亚洲色爱图小说专区| 777亚洲精品乱码久久久久久| 亚洲一区电影在线观看| 亚洲国产精品成人午夜在线观看| 国产亚洲综合视频| 热久久这里是精品6免费观看| 最近2019中文字幕免费直播| 免费毛片在线看片免费丝瓜视频| 国产免费久久精品| 亚洲精品字幕在线观看| 亚洲特级aaaaaa毛片| 国产亚洲视频在线播放大全| 国产一级在线免费观看| h视频在线免费看| 四虎永久成人免费| 亚洲短视频男人的影院| 亚洲人AV在线无码影院观看| 又大又硬又粗又黄的视频免费看|