<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) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): java

     

    此例子是基于jspsmartupload組件的,jspsmartupload是一個(gè)不錯(cuò)的上傳下載組件,但對(duì)中文支持不足。若下載的文件名中有漢字,則瀏覽器在提示另存的文件名時(shí),顯示的是一堆亂碼,讓人看了很不舒服,為此,有人專門(mén)修改此組件,做了編碼的轉(zhuǎn)換工作,將文件名轉(zhuǎn)換為UTF-8形式的編碼形式。我用的是網(wǎng)上修改過(guò)的,已經(jīng)可以支持中文,相信你也可以找到,如果需要,可以聯(lián)系我,我會(huì)在第一時(shí)間發(fā)給你!

    在網(wǎng)上找了很多相關(guān)資料,自己也添加了一些js代碼,基本實(shí)現(xiàn)了動(dòng)態(tài)添加刪除多文件上傳的功能,如果想要做得更完美,或者把文件上傳下載信息存儲(chǔ)到數(shù)據(jù)庫(kù)等,那就自己去完善了,以下是所有的源代碼:

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

    首先當(dāng)然是上傳下載的頁(yè)面了,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>就自動(dòng)換行的,可以去掉,自己把握
            }
    </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>
    然后是實(shí)現(xiàn)上傳和下載的兩個(gè)servlet類(lèi)

    上傳文件ServletUpload類(lèi):

    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; //記錄文件上傳總個(gè)數(shù)
       SmartUpload mySmartUpload = new SmartUpload();
       mySmartUpload.initialize(config,request, response);
       try {
        //mySmartUpload.setAllowedFilesList("rar,htm,html,jar");//設(shè)置允許上傳的文件
        mySmartUpload.setDeniedFilesList("exe,jsp,asp");//禁止上傳的文件
        mySmartUpload.setDenyPhysicalPath(true); //拒絕物理路徑
        mySmartUpload.setMaxFileSize(5000000);//設(shè)置允許上傳文件最大為50000bytes
        mySmartUpload.setTotalMaxFileSize(50000000);//一次上傳文件大小最多不超過(guò)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類(lèi):

    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為要添加的數(shù)據(jù)。
       * 如果contentDisposition為null,則組件將自動(dòng)添加"attachment;",
       * 以表明將下載的文件作為附件,結(jié)果是IE瀏覽器將會(huì)提示另存文件,而不是自動(dòng)打開(kāi)這個(gè)文件
       * (IE瀏覽器一般根據(jù)下載的文件擴(kuò)展名決定執(zhí)行什么操作,擴(kuò)展名為doc的將用word程序打開(kāi),
       * 擴(kuò)展名為pdf的將用acrobat程序打開(kāi),等等)。
       */
       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>

    要實(shí)現(xiàn)無(wú)刷新上傳其實(shí)很簡(jiǎn)單。只要利用iframe即可,只要把target設(shè)置為隱藏的iframe將就可以了。
     <iframe name='hidden_frame' id="hidden_frame" style='display:none'></iframe>

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

    Copyright © H2O

    主站蜘蛛池模板: 亚洲三级中文字幕| 美女视频黄频a免费| 手机看黄av免费网址| 国产成人精品亚洲2020| 成人毛片18女人毛片免费| 欧美亚洲国产SUV| 免费人成视频x8x8入口| 国产精品久久久久久亚洲影视| 成人超污免费网站在线看| 日韩色视频一区二区三区亚洲| 又粗又硬又黄又爽的免费视频 | 国产精品二区三区免费播放心| 免费在线观看一区| 亚洲国产日韩在线视频| 无码国产精品一区二区免费 | 国产偷国产偷亚洲高清人| 亚洲精品偷拍视频免费观看| 三上悠亚电影全集免费 | 中文字幕无线码中文字幕免费| 亚洲AV色吊丝无码| 亚洲日本乱码在线观看| 国产国产人免费人成免费视频| 67pao强力打造国产免费| 国产日韩久久免费影院| 亚洲欧美黑人猛交群| 亚洲视频免费在线播放| 中文字幕亚洲乱码熟女一区二区 | 国产精品亚洲av色欲三区| 亚洲欧洲日韩综合| 久久99国产亚洲高清观看首页| 成人免费淫片在线费观看| 四虎在线最新永久免费| 国产线视频精品免费观看视频| 色偷偷噜噜噜亚洲男人| 中文字幕无码亚洲欧洲日韩| 亚洲黄色免费电影| 国产AV无码专区亚洲AV男同| 亚洲午夜精品第一区二区8050| 国产成人精品男人免费| 国产免费av片在线看| 麻豆视频免费播放|