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

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

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

    jspmartLoad文件上傳下載

    Posted on 2008-10-02 23:16 H2O 閱讀(487) 評論(0)  編輯  收藏 所屬分類: java

     

    此例子是基于jspsmartupload組件的,jspsmartupload是一個不錯的上傳下載組件,但對中文支持不足。若下載的文件名中有漢字,則瀏覽器在提示另存的文件名時,顯示的是一堆亂碼,讓人看了很不舒服,為此,有人專門修改此組件,做了編碼的轉換工作,將文件名轉換為UTF-8形式的編碼形式。我用的是網上修改過的,已經可以支持中文,相信你也可以找到,如果需要,可以聯系我,我會在第一時間發給你!

    在網上找了很多相關資料,自己也添加了一些js代碼,基本實現了動態添加刪除多文件上傳的功能,如果想要做得更完美,或者把文件上傳下載信息存儲到數據庫等,那就自己去完善了,以下是所有的源代碼:

    (文件下載出于安全考慮是按流的方式來進行的,而不是直接給出文件下載路徑地址,所以像迅雷等下載工具是不能下載的)

    首先當然是上傳下載的頁面了,upfile.jsp

    <%@ page contentType="text/html;charset=GBK"%>
    <html>
    <head>
       
    <title>File Upload</title>
    <script type="text/javascript">
             function addFile()
            {
                var upFile = '
    <input type="file" name="file1"><br>';
                document .getElementById ("files").insertAdjacentHTML("beforeEnd",upFile);
            }
            function deleteFile()
            {
                var file = document .getElementById ("files").lastChild;
                if(file == null)
                    return;
                document .getElementById ("files").removeChild(file);
                
                file = document .getElementById ("files").lastChild;   //移除換行符
    <br>所以要移兩次
                document .getElementById ("files").removeChild(file); //如果在表格里面不加
    <br>就自動換行的,可以去掉,自己把握
            }
    </script>
    </head>
    <body>
    <h3>基于jspsmart upload組件的文件上傳下載</h3>
    <form action="servlet/ServletUpload" method="post" enctype="multipart/form-data">
       選擇文件:
    <div id="files"><input type="file" name="file1"><br></div>
         
    <input type="submit" value="上傳">
         
    <input type="button" value="增加文件" onclick="addFile()"> 
         
    <input type="button" value="刪除文件" onclick="deleteFile()">
         
    <input type="reset" value="重置">
       
    </form>
       
    <br>
       
    <form action="servlet/ServletDownload" method="post">
         下載文件的名稱:
         
    <input type="text" name="downloadFileName" size="20" maxlength="80">
        
    <input type="submit" value="下載">
       
    </form>
    </body>
    </html>
    然后是實現上傳和下載的兩個servlet類

    上傳文件ServletUpload類:

    package servlet;

    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import com.jspsmart.upload.SmartUpload;
    import com.jspsmart.upload.SmartUploadException;

    public class ServletUpload extends HttpServlet {

    private ServletConfig config;

    public ServletUpload() {
       super();
    }

    public void destroy() {
       super.destroy(); // Just puts "destroy" string in log
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException { 
       int count = 0; //記錄文件上傳總個數
       SmartUpload mySmartUpload = new SmartUpload();
       mySmartUpload.initialize(config,request, response);
       try {
        //mySmartUpload.setAllowedFilesList("rar,htm,html,jar");//設置允許上傳的文件
        mySmartUpload.setDeniedFilesList("exe,jsp,asp");//禁止上傳的文件
        mySmartUpload.setDenyPhysicalPath(true); //拒絕物理路徑
        mySmartUpload.setMaxFileSize(5000000);//設置允許上傳文件最大為50000bytes
        mySmartUpload.setTotalMaxFileSize(50000000);//一次上傳文件大小最多不超過5000000bytes
        mySmartUpload.upload();
        for(int i=0;i
    <mySmartUpload.getFiles().getCount();i++){
         com.jspsmart.upload.File myFile 
    = mySmartUpload.getFiles().getFile(i);
         
    String fileName = myFile.getFileName();
         
    System.out.println("文件名:"+fileName);
        }
        count 
    = mySmartUpload.save("/upload");
        
    System.out.println(count+"文件已上傳");
       } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
       doGet(request, response);
    }

    public void init(ServletConfig config) throws ServletException {
       this.config 
    = config;
    }
    }

    下載文件ServletDownload類:

    package servlet;

    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import com.jspsmart.upload.SmartUpload;
    import com.jspsmart.upload.SmartUploadException;

    public class ServletDownload extends HttpServlet {

    private ServletConfig config;

    public ServletDownload() {
       super();
    }

    public void destroy() {
       super.destroy(); // Just puts "destroy" string in log
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
       String temp_fileName 
    = request.getParameter("downloadFileName");
       
    if(temp_fileName == null || temp_fileName == "")
        return ;
       byte[] temp_t 
    = temp_fileName.getBytes("ISO8859_1");
       
    String fileName = new String(temp_t, "GBK");
       SmartUpload mySmartUpload 
    = new SmartUpload();
       mySmartUpload.initialize(config, request, response);
       mySmartUpload.setContentDisposition(null);
       /*
       * 原型:public void setContentDisposition(String contentDisposition)
       * 其中,contentDisposition為要添加的數據。
       * 如果contentDisposition為null,則組件將自動添加"attachment;",
       * 以表明將下載的文件作為附件,結果是IE瀏覽器將會提示另存文件,而不是自動打開這個文件
       * (IE瀏覽器一般根據下載的文件擴展名決定執行什么操作,擴展名為doc的將用word程序打開,
       * 擴展名為pdf的將用acrobat程序打開,等等)。
       */
       try {
        mySmartUpload.downloadFile("/upload/"+fileName);
       
       } catch (SmartUploadException e) {
        e.printStackTrace();
       }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
       doGet(request, response);
    }

    public void init(ServletConfig config) throws ServletException {
       this.config 
    = config;
    }
    }

    以下是web.xml配置文件:

    <?xml version
    ="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
    xmlns
    ="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation
    ="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    >
    <servlet>
        
    <description>This is the description of my J2EE component</description>
        
    <display-name>This is the display name of my J2EE component</display-name>
        
    <servlet-name>ServletUpload</servlet-name>
        
    <servlet-class>servlet.ServletUpload</servlet-class>
    </servlet>
    <servlet>
        
    <description>This is the description of my J2EE component</description>
        
    <display-name>This is the display name of my J2EE component</display-name>
        
    <servlet-name>ServletDownload</servlet-name>
        
    <servlet-class>servlet.ServletDownload</servlet-class>
    </servlet>


    <servlet-mapping>
        
    <servlet-name>ServletUpload</servlet-name>
        
    <url-pattern>/servlet/ServletUpload</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        
    <servlet-name>ServletDownload</servlet-name>
        
    <url-pattern>/servlet/ServletDownload</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    </web-app>

    要實現無刷新上傳其實很簡單。只要利用iframe即可,只要把target設置為隱藏的iframe將就可以了。
     <iframe name='hidden_frame' id="hidden_frame" style='display:none'></iframe>

    posts - 0, comments - 21, trackbacks - 0, articles - 101

    Copyright © H2O

    主站蜘蛛池模板: 在线日韩日本国产亚洲| 成人啪精品视频免费网站| 全亚洲最新黄色特级网站| 亚洲AV无码专区在线观看成人 | 深夜久久AAAAA级毛片免费看| 精品久久久久久久免费加勒比| 亚洲欧美国产国产综合一区| 日韩在线视频免费看| 99亚洲乱人伦aⅴ精品| 日本免费v片一二三区| 视频一区在线免费观看| 亚洲日韩国产精品乱| 爱丫爱丫影院在线观看免费| 一级做a爰片久久毛片免费陪 | 亚洲人成网站在线观看青青| 人成电影网在线观看免费| 国产成人亚洲综合无码| 99精品视频在线观看免费| 亚洲第一视频网站| 亚洲视频在线免费播放| 亚洲AV永久无码天堂影院| 免费人成无码大片在线观看| 9久热精品免费观看视频| 亚洲视频在线免费看| 狼友av永久网站免费观看| 日韩少妇内射免费播放| 亚洲av无码成人黄网站在线观看 | 久久国产精品一区免费下载| 亚洲无限乱码一二三四区| 国产无遮挡吃胸膜奶免费看视频| 免费人成网站永久| 亚洲精品高清国产一久久| 毛片免费全部免费观看| 福利免费在线观看| 亚洲一区精品视频在线| 亚洲一区二区三区无码影院| 99精品视频在线观看免费播放| 亚洲夂夂婷婷色拍WW47| 亚洲真人无码永久在线| 成熟女人牲交片免费观看视频 | 亚洲日韩国产精品乱-久|