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

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

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

    heting

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      40 隨筆 :: 9 文章 :: 45 評論 :: 0 Trackbacks

     

     

    FileAction

    package com.action;
    import org.apache.struts.action.*;
    import javax.servlet.http.*;
    import com.actionForm.FileActionForm;
    import org.apache.struts.actions.DispatchAction;
    import java.util.Date;
    import java.text.*;
    import org.apache.struts.upload.FormFile;
    import java.io.*;
    import java.net.URLEncoder;
    import com.dao.*;

    public class FileAction extends DispatchAction {

        
    private JDBConnection connection =new JDBConnection();
    //以下方法實(shí)現(xiàn)文件的上傳
        public ActionForward upLoadFile(ActionMapping mapping, ActionForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response) 
    throws
                Exception 
    {
        ActionForward forward
    =null;
            Date date 
    = new Date();
            FileActionForm fileActionForm 
    = (FileActionForm) form;
            
    //FormFile用于指定存取文件的類型
            FormFile file = fileActionForm.getFile(); //獲取當(dāng)前的文件
          
    // 獲得系統(tǒng)的絕對路徑 String dir = servlet.getServletContext().getRealPath("/image");
            
    //我上傳的文件沒有放在服務(wù)器上。而是存在D:D:\\loadfile\\temp\\
            String dir="D:\\loadfile\\temp\\";
            
    int i = 0;
       String type 
    = file.getFileName();
       
    while(i!=-1){
       
    //找到上傳文件的類型的位置,這個地方的是'.'
        i = type.indexOf(".");
       
    /* System.out.println(i);*/
        
    /*截取上傳文件的后綴名,此時得到了文件的類型*/
        type 
    = type.substring(i+1);
       }

    // 限制上傳類型為jpg,txt,rar;
       if (!type.equals("jpg"&& !type.equals("txt")&& !type.equals("bmp"))
       
      
    {//當(dāng)上傳的類型不為上述類型時,跳轉(zhuǎn)到錯誤頁面。
        forward=mapping.findForward("error");
       }

       
    else
       
    {  
    //    將上傳時間加入文件名(這個地方的是毫秒數(shù))   
         String times = String.valueOf(date.getTime());
       
    //組合成 time.type
             String fname = times + "." + type;
           
    //InInputStream是用以從特定的資源讀取字節(jié)的方法。
              InputStream streamIn = file.getInputStream();    //創(chuàng)建讀取用戶上傳文件的對象
              
    //得到是字節(jié)數(shù),即byte,我們可以直接用file.getFileSize(),也可以在創(chuàng)建讀取對象時用streamIn.available();
             
    // int ok=streamIn.available();           
              int ok=file.getFileSize();
              String strFee 
    = null;
             
    //這個地方是處理上傳的為M單位計算時,下一個是以kb,在下一個是byte;
              
              
    if(ok>=1024*1024)
              
    {
              
    float ok1=(((float)ok)/1024f/1024f); 
               DecimalFormat myformat1 
    = new DecimalFormat("0.00");         
              strFee 
    = myformat1.format(ok1)+"M";
                     System.out.println(strFee
    +"M");
              }

              
    else if(ok>1024 && ok<=1024*1024)
              
    {
                 
    double ok2=((double)ok)/1024;
                 DecimalFormat myformat2
    =new DecimalFormat("0.00");
                strFee 
    = myformat2.format(ok2)+"kb";
                     System.out.println(strFee
    +"kb");
              }

              
    else if(ok<1024)
              
    {
              System.out.println(
    "aaaaaaaaa");
               strFee
    =String.valueOf(ok)+"byte";
               System.out.println(strFee);
               
              }

              System.out.println( streamIn.available()
    +"文件大小byte");
              
    //這個是io包下的上傳文件類
              File uploadFile = new File(dir);   //指定上傳文件的位置
              if (!uploadFile.exists() || uploadFile == null//判斷指定路徑dir是否存在,不存在則創(chuàng)建路徑
                  uploadFile.mkdirs();
              }

              
    //上傳的路徑+文件名
              String path = uploadFile.getPath() + "\\" + fname;
           
    //OutputStream用于向某個目標(biāo)寫入字節(jié)的抽象類,這個地方寫入目標(biāo)是path,通過輸出流FileOutputStream去寫
              OutputStream streamOut = new FileOutputStream(path);
              
    int bytesRead = 0;
              
    byte[] buffer = new byte[8192];
              
    //將數(shù)據(jù)讀入byte數(shù)組的一部分,其中讀入字節(jié)數(shù)的最大值是8192,讀入的字節(jié)將存儲到,buffer[0]到buffer[0+8190-1]的部分中
              
    //streamIn.read方法返回的是實(shí)際讀取字節(jié)數(shù)目.如果讀到末尾則返回-1.如果bytesRead返回為0則表示沒有讀取任何字節(jié)。
              while ((bytesRead = streamIn.read(buffer, 08192)) != -1{
              
    //寫入buffer數(shù)組的一部分,從buf[0]開始寫入并寫入bytesRead個字節(jié),這個write方法將發(fā)生阻塞直至字節(jié)寫入完成。
                  streamOut.write(buffer, 0, bytesRead);
              }

            
    // 關(guān)閉輸出輸入流,銷毀File流。
              streamOut.close();
              streamIn.close();
              file.destroy();    
              String paths
    =path;
              System.out.println(paths);
             String fileName 
    = Chinese.toChinese(fileActionForm.getFileName()); //獲取文件的名稱
            
    //String fileSize = String.valueOf(file.getFileSize());
             String fileDate = DateFormat.getDateInstance().format(date);
             String sql 
    = "insert into tb_file values('" + fileName + "','" +
             strFee 
    + "','" + fileDate + "','" + paths + "')";
             connection.executeUpdate(sql);
             connection.closeConnection();
             forward
    =mapping.findForward("upLoadFileResult");
       }

            
    return forward;
        }

        
    //實(shí)現(xiàn)文件的下載
        public ActionForward downFile(ActionMapping mapping, ActionForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response) 
    throws
                Exception 
    {
            String path 
    = request.getParameter("path");
            System.out.println(path
    +"111");
            BufferedInputStream bis 
    = null;
            BufferedOutputStream bos 
    = null;
            OutputStream fos 
    = null;
            InputStream fis 
    = null;
            
          
    //如果是從服務(wù)器上取就用這個獲得系統(tǒng)的絕對路徑方法。 String filepath = servlet.getServletContext().getRealPath("/" + path);
            String filepath=path;
            System.out.println(
    "文件路徑"+filepath);
            File uploadFile 
    = new File(filepath);
            fis 
    = new FileInputStream(uploadFile);
            bis 
    = new BufferedInputStream(fis);
            fos 
    = response.getOutputStream();
            bos 
    = new BufferedOutputStream(fos);
            
    //這個就就是彈出下載對話框的關(guān)鍵代碼
            response.setHeader("Content-disposition",
                               
    "attachment;filename=" +
                               URLEncoder.encode(path, 
    "utf-8"));
            
    int bytesRead = 0;
            
    //這個地方的同上傳的一樣。我就不多說了,都是用輸入流進(jìn)行先讀,然后用輸出流去寫,唯一不同的是我用的是緩沖輸入輸出流
            byte[] buffer = new byte[8192];
            
    while ((bytesRead = bis.read(buffer, 08192)) != -1{
                bos.write(buffer, 
    0, bytesRead);
            }

            bos.flush();
            fis.close();
            bis.close();
            fos.close();
            bos.close();
            
    return null;
        }


    }



    FileActionForm
    package com.actionForm;

    import org.apache.struts.action.*;
    import org.apache.struts.upload.*;

    public class FileActionForm extends ActionForm {
        
    private String fileName;//上傳文件的名稱
        private String fileSize;//上傳文件的大小
        private String filePath;//上傳文件到服務(wù)器的路徑
        private String fileDate;//上傳文件的日期
        private FormFile file;//上傳文件

        
    public String getFileName() {
            
    return fileName;
        }


        
    public FormFile getFile() {
            
    return file;
        }


        
    public String getFileSize() {
            
    return fileSize;
        }


        
    public String getFilePath() {
            
    return filePath;
        }


        
    public String getFileDate() {
            
    return fileDate;
        }


        
    public void setFileName(String fileName) {
            
    this.fileName = fileName;
        }


        
    public void setFile(FormFile file) {
            
    this.file = file;
        }


        
    public void setFileSize(String fileSize) {
            
    this.fileSize = fileSize;
        }


        
    public void setFilePath(String filePath) {
            
    this.filePath = filePath;
        }


        
    public void setFileDate(String fileDate) {
            
    this.fileDate = fileDate;
        }


    }



    index.jsp
    <table width="264" height="81" border="0" align="center" cellpadding="0" cellspacing="0">
                    
    <tr>
                      
    <td width="115" rowspan="4" align="center"><img src="<%=form.getFilePath()%>" width="100" height="100"></td>
                      
    <td width="133" align="center">圖片名稱:<%=form.getFileName()%></td>
                    
    </tr>
                    
    <tr align="center">
                      
    <td>圖片大?。?/span><%=form.getFileSize()%></td>
                    
    </tr>
                    
    <tr align="center">
                      
    <td>上傳日期:<%=form.getFileDate()%></td>
                    
    </tr>
                    
    <tr>
                      
    <td align="center"><href="fileAction.do?method=downFile&path=<%=form.getFilePath()%>" ><img src="priture/bottond.jpg"></a>


                      
    </td>
                    
    </tr>
                
    </table>

    <html:form action="fileAction.do?method=upLoadFile" enctype="multipart/form-data" onsubmit="return Mycheck()">
            
    <table height="52" border="0" align="center" cellpadding="0" cellspacing="0">
              
    <tr align="center">
                
    <td width="60" height="26">圖片名稱:</td>
                
    <td width="160"> <html:text property="fileName"/> </td>
                
    <td width="60">圖片路徑:</td>
                
    <td width="198"> <html:file property="file"/> </td>
              
    </tr>
              
    <tr align="right">
                
    <td height="26" colspan="4"> <html:submit>上傳</html:submit> </td>
              
    </tr>
            
    </table>
       
    </html:form>


    struts-config.xml  

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

    <struts-config>
    <form-beans>
        
    <form-bean name="fileActionForm" type="com.actionForm.FileActionForm" />
    </form-beans>
    <action-mappings>
        
    <action name="fileActionForm" parameter="method" path="/fileAction" scope="request" type="com.action.FileAction" validate="true">
            
    <forward name="upLoadFileResult" path="/result.jsp"/>
            
    <forward name="error" path="/fail.jsp"></forward>
        
    </action>
    </action-mappings>
    <message-resources parameter="ApplicationResources" />
    </struts-config>

    posted on 2009-03-04 10:36 賀挺 閱讀(21129) 評論(23)  編輯  收藏

    評論

    # re: struts1文件上傳和下載[未登錄] 2010-04-08 09:28 qq
    very perfit!!!  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載 2010-06-30 17:03 ai
    能給個源碼例子嗎  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載 2010-10-31 11:15 路過
    頂樓主!  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載 2011-06-29 14:51 naoskang
    頂樓主,非常好用  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載[未登錄] 2011-09-13 09:06 even
    思路一下就清晰了  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載 2011-12-19 20:33 asd
    上傳文件時??要求客戶去填寫???文件名????  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載 2011-12-19 20:34 asd
    如果不那樣寫的話。會亂碼OKlahoma???  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載[未登錄] 2012-01-06 16:58 chen
    非常感謝,轉(zhuǎn)了、、、、、、  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載 2012-03-06 11:10 安萬軍
    JDBConnection這個類是那個包的?  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載 2012-03-06 11:27 安萬軍
    @安萬軍
    看錯了!  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載 2012-06-30 21:41 lsc
    @安萬軍
    我沒看懂這個JDBConnection,有問題,能指點(diǎn)一下嗎  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載 2012-06-30 22:35 lsc
    @安萬軍
    是數(shù)據(jù)庫連接嗎?自己寫個類行不行?  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載 2012-07-31 09:33 sinianxue
    這里讓用戶填寫名稱和路徑不合理,一般用戶是不會同意這么做的  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載[未登錄] 2012-09-05 19:41 test
    這種方法上傳1m以上的doc文件會出現(xiàn)錯誤  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載[未登錄] 2012-11-30 22:35 aaa
    不能直接用  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載 2012-12-13 15:25 lvjun
    垃圾  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載 2013-03-21 11:48 000

    000
      回復(fù)  更多評論
      

    # re: struts1文件上傳和下載 2013-06-17 20:50 yangther
    asdasd  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載 2013-06-17 20:50 yangther
    Struts上傳  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載[未登錄] 2013-06-28 15:02 jBoy
    下載的是時候,沒有考慮過中文亂碼的問題。  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載 2013-12-12 14:48
    頂!?。。。。。。。。。。。。。。。?!  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載[未登錄] 2014-01-22 17:38 ggg
    ssgsg  回復(fù)  更多評論
      

    # re: struts1文件上傳和下載 2015-03-24 18:12 zuidaima
    java struts框架demo使用實(shí)例教程源代碼下載地址:http://zuidaima.com/share/kstruts-p1-s1.htm  回復(fù)  更多評論
      


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


    網(wǎng)站導(dǎo)航:
    博客園   IT新聞   Chat2DB   C++博客   博問  
     
    主站蜘蛛池模板: 国产小视频免费观看| 日日夜夜精品免费视频| 日韩免费视频在线观看| 亚洲精品你懂的在线观看| 激情亚洲一区国产精品| 午夜成人无码福利免费视频| 午夜影院免费观看| 可以免费观看一级毛片黄a| 亚洲人成在线影院| 免费大片av手机看片| 波多野结衣在线免费观看| 久久夜色精品国产亚洲av| 亚洲 欧洲 视频 伦小说| 三上悠亚电影全集免费| 无码国产精品一区二区免费I6| 中文字幕亚洲电影| 亚洲色偷偷综合亚洲AV伊人蜜桃| 国产色无码精品视频免费| 大学生高清一级毛片免费| 国产V亚洲V天堂A无码| 国产成人人综合亚洲欧美丁香花 | 女性无套免费网站在线看| 亚洲av无码av制服另类专区| 婷婷亚洲综合一区二区| 国产国产人免费视频成69堂| 精品亚洲综合久久中文字幕| 国产亚洲精品成人久久网站| 1000部拍拍拍18勿入免费视频下载| 亚洲男人在线无码视频| 亚洲欧美综合精品成人导航| 99re在线这里只有精品免费| 亚洲色图综合在线| 亚洲AV无码AV吞精久久| 成人午夜免费福利视频| 亚洲AV午夜成人片| 国产99视频精品免费视频76| 日本一区二区三区日本免费| 亚洲午夜精品在线| 精品一区二区三区无码免费视频| 精品亚洲成α人无码成α在线观看 | 99精品免费观看|