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

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

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

    heting

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      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();
    //以下方法實現文件的上傳
        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(); //獲取當前的文件
          
    // 獲得系統的絕對路徑 String dir = servlet.getServletContext().getRealPath("/image");
            
    //我上傳的文件沒有放在服務器上。而是存在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"))
       
      
    {//當上傳的類型不為上述類型時,跳轉到錯誤頁面。
        forward=mapping.findForward("error");
       }

       
    else
       
    {  
    //    將上傳時間加入文件名(這個地方的是毫秒數)   
         String times = String.valueOf(date.getTime());
       
    //組合成 time.type
             String fname = times + "." + type;
           
    //InInputStream是用以從特定的資源讀取字節的方法。
              InputStream streamIn = file.getInputStream();    //創建讀取用戶上傳文件的對象
              
    //得到是字節數,即byte,我們可以直接用file.getFileSize(),也可以在創建讀取對象時用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是否存在,不存在則創建路徑
                  uploadFile.mkdirs();
              }

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

            
    // 關閉輸出輸入流,銷毀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;
        }

        
    //實現文件的下載
        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;
            
          
    //如果是從服務器上取就用這個獲得系統的絕對路徑方法。 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);
            
    //這個就就是彈出下載對話框的關鍵代碼
            response.setHeader("Content-disposition",
                               
    "attachment;filename=" +
                               URLEncoder.encode(path, 
    "utf-8"));
            
    int bytesRead = 0;
            
    //這個地方的同上傳的一樣。我就不多說了,都是用輸入流進行先讀,然后用輸出流去寫,唯一不同的是我用的是緩沖輸入輸出流
            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;//上傳文件到服務器的路徑
        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>圖片大小:<%=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!!!  回復  更多評論
      

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    000
      回復  更多評論
      

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

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

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

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

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

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


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


    網站導航:
     
    主站蜘蛛池模板: 亚洲中文字幕无码久久2017| 久久久亚洲欧洲日产国码农村| 深夜福利在线免费观看| 久久久久亚洲精品无码网址 | 久草福利资源网站免费| 亚洲国产精品久久网午夜| 国产成人精品男人免费| 黄色网站软件app在线观看免费 | 在线免费观看污网站| 久久久WWW免费人成精品| 亚洲欧洲免费视频| 四虎永久精品免费观看| 久久久久久夜精品精品免费啦| 亚洲欧美第一成人网站7777| 国产亚洲精品国产| 卡一卡二卡三在线入口免费| 特级做A爰片毛片免费看无码| 亚洲欧美综合精品成人导航| 亚洲AV无码成人精品区蜜桃| 日本牲交大片免费观看| 美女在线视频观看影院免费天天看 | 国产乱子影视频上线免费观看| 久操视频免费观看| 香港经典a毛片免费观看看| 亚洲日韩图片专区第1页| 亚洲国产精品一区二区第四页| 日本人的色道免费网站| 美女巨胸喷奶水视频www免费| 亚洲中文无码永久免费| 久久久久亚洲Av片无码v| 亚洲国产婷婷香蕉久久久久久| 亚洲免费黄色网址| 中文字幕乱码免费看电影| 国产精品亚洲а∨无码播放不卡 | 国产伦精品一区二区免费| 77777亚洲午夜久久多喷| 亚洲日本一区二区| 亚洲国产精品乱码一区二区 | 亚洲av无码成h人动漫无遮挡| 免费国产成人午夜电影| 最近最好的中文字幕2019免费 |