国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

jspsmartupload

2024-09-05 00:19:22
字體:
來源:轉載
供稿:網友
  • 網站運營seo文章大全
  • 提供全面的站長運營經驗及seo技術!
  • 一、安裝篇

      jspsmartupload是由www.jspsmart.com網站開發的一個可免費使用的全功能的文件上傳下載組件,適于嵌入執行上傳下載操作的jsp文件中。該組件有以下幾個特點:

    1、使用簡單。在jsp文件中僅僅書寫三五行java代碼就可以搞定文件的上傳或下載,方便。

    2、能全程控制上傳。利用jspsmartupload組件提供的對象及其操作方法,可以獲得全部上傳文件的信息(包括文件名,大小,類型,擴展名,文件數據等),方便存取。

    3、能對上傳的文件在大小、類型等方面做出限制。如此可以濾掉不符合要求的文件。

    4、下載靈活。僅寫兩行代碼,就能把web服務器變成文件服務器。不管文件在web服務器的目錄下或在其它任何目錄下,都可以利用jspsmartupload進行下載。

    5、能將文件上傳到數據庫中,也能將數據庫中的數據下載下來。這種功能針對的是mysql數據庫,因為不具有通用性,所以本文不準備舉例介紹這種用法。

      jspsmartupload組件可以從www.jspsmart.com網站上自由下載,壓縮包的名字是jspsmartupload.zip。下載后,用winzip或winrar將其解壓到tomcat的webapps目錄下(本文以tomcat服務器為例進行介紹)。解壓后,將webapps/jspsmartupload目錄下的子目錄web-inf名字改為全大寫的web-inf,這樣一改jspsmartupload類才能使用。因為tomcat對文件名大小寫敏感,它要求web應用程序相關的類所在目錄為web-inf,且必須是大寫。接著重新啟動tomcat,這樣就可以在jsp文件中使用jspsmartupload組件了。

      注意,按上述方法安裝后,只有webapps/jspsmartupload目錄下的程序可以使用jspsmartupload組件,如果想讓tomcat服務器的所有web應用程序都能用它,必須做如下工作:

    1.進入命令行狀態,將目錄切換到tomcat的webapps/jspsmartupload/web-inf目錄下。

    2.運行jar打包命令:jar cvf jspsmartupload.jar com

    (也可以打開資源管理器,切換到當前目錄,用winzip將com目錄下的所有文件壓縮成jspsmartupload.zip,然后將jspsmartupload.zip換名為jspsmartupload.jar文件即可。)

    3.將jspsmartupload.jar拷貝到tomcat的shared/lib目錄下。


    二、相關類說明篇

    ㈠ file類

      這個類包裝了一個上傳文件的所有信息。通過它,可以得到上傳文件的文件名、文件大小、擴展名、文件數據等信息。

      file類主要提供以下方法:

    1、saveas作用:將文件換名另存。

    原型:

    public void saveas(java.lang.string destfilepathname)



    public void saveas(java.lang.string destfilepathname, int optionsaveas)

    其中,destfilepathname是另存的文件名,optionsaveas是另存的選項,該選項有三個值,分別是save_physical,save_virtual,save_auto。save_physical表明以操作系統的根目錄為文件根目錄另存文件,save_virtual表明以web應用程序的根目錄為文件根目錄另存文件,saveas_auto則表示讓組件決定,當web應用程序的根目錄存在另存文件的目錄時,它會選擇save_virtual,否則會選擇save_physical。

    例如,saveas("/upload/sample.zip",save_physical)執行后若web服務器安裝在c盤,則另存的文件名實際是c:/upload/sample.zip。而saveas("/upload/sample.zip",save_virtual)執行后若web應用程序的根目錄是webapps/jspsmartupload,則另存的文件名實際是webapps/jspsmartupload/upload/sample.zip。saveas("/upload/sample.zip",save_auto)執行時若web應用程序根目錄下存在upload目錄,則其效果同saveas("/upload/sample.zip",save_virtual),否則同saveas("/upload/sample.zip",save_physical)。

    建議:對于web程序的開發來說,最好使用save_virtual,以便移植。

    2、ismissing

    作用:這個方法用于判斷用戶是否選擇了文件,也即對應的表單項是否有值。選擇了文件時,它返回false。未選文件時,它返回true。

    原型:public boolean ismissing()

    3、getfieldname

    作用:取html表單中對應于此上傳文件的表單項的名字。

    原型:public string getfieldname()

    4、getfilename

    作用:取文件名(不含目錄信息)

    原型:public string getfilename()

    5、getfilepathname

    作用:取文件全名(帶目錄)

    原型:public string getfilepathname

    6、getfileext

    作用:取文件擴展名(后綴)

    原型:public string getfileext()

    7、getsize

    作用:取文件長度(以字節計)

    原型:public int getsize()

    8、getbinarydata

    作用:取文件數據中指定位移處的一個字節,用于檢測文件等處理。

    原型:public byte getbinarydata(int index)。其中,index表示位移,其值在0到getsize()-1之間。

    ㈡ files類

      這個類表示所有上傳文件的集合,通過它可以得到上傳文件的數目、大小等信息。有以下方法:

    1、getcount

    作用:取得上傳文件的數目。

    原型:public int getcount()

    2、getfile

    作用:取得指定位移處的文件對象file(這是com.jspsmart.upload.file,不是java.io.file,注意區分)。

    原型:public file getfile(int index)。其中,index為指定位移,其值在0到getcount()-1之間。

    3、getsize

    作用:取得上傳文件的總長度,可用于限制一次性上傳的數據量大小。

    原型:public long getsize()

    4、getcollection

    作用:將所有上傳文件對象以collection的形式返回,以便其它應用程序引用,瀏覽上傳文件信息。

    原型:public collection getcollection()

    5、getenumeration

    作用:將所有上傳文件對象以enumeration(枚舉)的形式返回,以便其它應用程序瀏覽上傳文件信息。

    原型:public enumeration getenumeration()

    ㈢ request類

      這個類的功能等同于jsp內置的對象request。只所以提供這個類,是因為對于文件上傳表單,通過request對象無法獲得表單項的值,必須通過jspsmartupload組件提供的request對象來獲取。該類提供如下方法:

    1、getparameter

    作用:獲取指定參數之值。當參數不存在時,返回值為null。

    原型:public string getparameter(string name)。其中,name為參數的名字。

    2、getparametervalues

    作用:當一個參數可以有多個值時,用此方法來取其值。它返回的是一個字符串數組。當參數不存在時,返回值為null。

    原型:public string[] getparametervalues(string name)。其中,name為參數的名字。

    3、getparameternames

    作用:取得request對象中所有參數的名字,用于遍歷所有參數。它返回的是一個枚舉型的對象。

    原型:public enumeration getparameternames()

    ㈣ smartupload類這個類完成上傳下載工作。

    a.上傳與下載共用的方法:

    只有一個:initialize。

    作用:執行上傳下載的初始化工作,必須第一個執行。

    原型:有多個,主要使用下面這個:

    public final void initialize(javax.servlet.jsp.pagecontext pagecontext)

    其中,pagecontext為jsp頁面內置對象(頁面上下文)。

    b.上傳文件使用的方法:

    1、upload

    作用:上傳文件數據。對于上傳操作,第一步執行initialize方法,第二步就要執行這個方法。

    原型:public void upload()

    2、save

    作用:將全部上傳文件保存到指定目錄下,并返回保存的文件個數。

    原型:public int save(string destpathname)

    和public int save(string destpathname,int option)

    其中,destpathname為文件保存目錄,option為保存選項,它有三個值,分別是save_physical,save_virtual和save_auto。(同file類的saveas方法的選項之值類似)save_physical指示組件將文件保存到以操作系統根目錄為文件根目錄的目錄下,save_virtual指示組件將文件保存到以web應用程序根目錄為文件根目錄的目錄下,而save_auto則表示由組件自動選擇。

    注:save(destpathname)作用等同于save(destpathname,save_auto)。

    3、getsize

    作用:取上傳文件數據的總長度

    原型:public int getsize()

    4、getfiles

    作用:取全部上傳文件,以files對象形式返回,可以利用files類的操作方法來獲得上傳文件的數目等信息。

    原型:public files getfiles()

    5、getrequest

    作用:取得request對象,以便由此對象獲得上傳表單參數之值。

    原型:public request getrequest()

    6、setallowedfileslist

    作用:設定允許上傳帶有指定擴展名的文件,當上傳過程中有文件名不允許時,組件將拋出異常。

    原型:public void setallowedfileslist(string allowedfileslist)

    其中,allowedfileslist為允許上傳的文件擴展名列表,各個擴展名之間以逗號分隔。如果想允許上傳那些沒有擴展名的文件,可以用兩個逗號表示。例如:setallowedfileslist("doc,txt,,")將允許上傳帶doc和txt擴展名的文件以及沒有擴展名的文件。

    7、setdeniedfileslist

    作用:用于限制上傳那些帶有指定擴展名的文件。若有文件擴展名被限制,則上傳時組件將拋出異常。

    原型:public void setdeniedfileslist(string deniedfileslist)

    其中,deniedfileslist為禁止上傳的文件擴展名列表,各個擴展名之間以逗號分隔。如果想禁止上傳那些沒有擴展名的文件,可以用兩個逗號來表示。例如:setdeniedfileslist("exe,bat,,")將禁止上傳帶exe和bat擴展名的文件以及沒有擴展名的文件。

    8、setmaxfilesize

    作用:設定每個文件允許上傳的最大長度。

    原型:public void setmaxfilesize(long maxfilesize)

    其中,maxfilesize為為每個文件允許上傳的最大長度,當文件超出此長度時,將不被上傳。

    9、settotalmaxfilesize

    作用:設定允許上傳的文件的總長度,用于限制一次性上傳的數據量大小。

    原型:public void settotalmaxfilesize(long totalmaxfilesize)

    其中,totalmaxfilesize為允許上傳的文件的總長度。

    c.下載文件常用的方法

    1、setcontentdisposition

    作用:將數據追加到mime文件頭的content-disposition域。jspsmartupload組件會在返回下載的信息時自動填寫mime文件頭的content-disposition域,如果用戶需要添加額外信息,請用此方法。

    原型:public void setcontentdisposition(string contentdisposition)

    其中,contentdisposition為要添加的數據。如果contentdisposition為null,則組件將自動添加"attachment;",以表明將下載的文件作為附件,結果是ie瀏覽器將會提示另存文件,而不是自動打開這個文件(ie瀏覽器一般根據下載的文件擴展名決定執行什么操作,擴展名為doc的將用word程序打開,擴展名為pdf的將用acrobat程序打開,等等)。

    2、downloadfile

    作用:下載文件。

    原型:共有以下三個原型可用,第一個最常用,后兩個用于特殊情況下的文件下載(如更改內容類型,更改另存的文件名)。

    ① public void downloadfile(string sourcefilepathname)

    其中,sourcefilepathname為要下載的文件名(帶目錄的文件全名)

    ② public void downloadfile(string sourcefilepathname,string contenttype)

    其中,sourcefilepathname為要下載的文件名(帶目錄的文件全名),contenttype為內容類型(mime格式的文件類型信息,可被瀏覽器識別)。

    ③ public void downloadfile(string sourcefilepathname,string contenttype,string destfilename)

    其中,sourcefilepathname為要下載的文件名(帶目錄的文件全名),contenttype為內容類型(mime格式的文件類型信息,可被瀏覽器識別),destfilename為下載后默認的另存文件名。

    三、文件上傳篇

    ㈠ 表單要求

    對于上傳文件的form表單,有兩個要求:

    1、method應用post,即method="post"。

    2、增加屬性:enctype="multipart/form-data"

    下面是一個用于上傳文件的form表單的例子:



    <form method="post" enctype="multipart/form-data"
    action="/jspsmartupload/upload.jsp">
    <input type="file" name="myfile">
    <input type="submit">
    </form>



    ㈡ 上傳的例子

    1、上傳頁面upload.html

    本頁面提供表單,讓用戶選擇要上傳的文件,點擊"上傳"按鈕執行上傳操作。

    頁面源碼如下:

    <!--
    文件名:upload.html
    作 者:縱橫軟件制作中心雨亦奇([email protected])
    -->
    <!doctype html public "-//w3c//dtd html 4.01 transitional//en">
    <html>
    <head>
    <title>文件上傳</title>
    <meta http-equiv="content-type" content="text/html; charset=gb2312">
    </head>

    <body>
    <p> </p>
    <p align="center">上傳文件選擇</p>
    <form method="post" action="jsp/do_upload.jsp"
    enctype="multipart/form-data">
    <input type="hidden" name="test" value="good">
    <table width="75%" border="1" align="center">
    <tr>
    <td><div align="center">1、
    <input type="file" name="file1" size="30">
    </div></td>
    </tr>
    <tr>
    <td><div align="center">2、
    <input type="file" name="file2" size="30">
    </div></td>
    </tr>
    <tr>
    <td><div align="center">3、
    <input type="file" name="file3" size="30">
    </div></td>
    </tr>
    <tr>
    <td><div align="center">4、
    <input type="file" name="file4" size="30">
    </div></td>
    </tr>
    <tr>
    <td><div align="center">
    <input type="submit" name="submit" value="上傳它!">
    </div></td>
    </tr>
    </table>
    </form>
    </body>
    </html>



    2、上傳處理頁面do_upload.jsp

    本頁面執行文件上傳操作。頁面源碼中詳細介紹了上傳方法的用法,在此不贅述了。

    頁面源碼如下:

    <%--
    文件名:do_upload.jsp
    作 者:縱橫軟件制作中心雨亦奇([email protected])
    --%>
    <%@ page contenttype="text/html; charset=gb2312" language="java"
    import="java.util.*,com.jspsmart.upload.*" errorpage="" %>
    <html>
    <head>
    <title>文件上傳處理頁面</title>
    <meta http-equiv="content-type" content="text/html; charset=gb2312">
    </head>

    <body>
    <%
    // 新建一個smartupload對象
    smartupload su = new smartupload();
    // 上傳初始化
    su.initialize(pagecontext);
    // 設定上傳限制
    // 1.限制每個上傳文件的最大長度。
    // su.setmaxfilesize(10000);
    // 2.限制總上傳數據的長度。
    // su.settotalmaxfilesize(20000);
    // 3.設定允許上傳的文件(通過擴展名限制),僅允許doc,txt文件。
    // su.setallowedfileslist("doc,txt");
    // 4.設定禁止上傳的文件(通過擴展名限制),禁止上傳帶有exe,bat,
    jsp,htm,html擴展名的文件和沒有擴展名的文件。
    // su.setdeniedfileslist("exe,bat,jsp,htm,html,,");
    // 上傳文件
    su.upload();
    // 將上傳文件全部保存到指定目錄
    int count = su.save("/upload");
    out.println(count+"個文件上傳成功!<br>");

    // 利用request對象獲取參數之值
    out.println("test="+su.getrequest().getparameter("test")
    +"<br><br>");

    // 逐一提取上傳文件信息,同時可保存文件。
    for (int i=0;i<su.getfiles().getcount();i++)
    {
    com.jspsmart.upload.file file = su.getfiles().getfile(i);

    // 若文件不存在則繼續
    if (file.ismissing()) continue;

    // 顯示當前文件信息
    out.println("<table border=1>");
    out.println("<tr><td>表單項名(fieldname)</td><td>"
    + file.getfieldname() + "</td></tr>");
    out.println("<tr><td>文件長度(size)</td><td>" +
    file.getsize() + "</td></tr>");
    out.println("<tr><td>文件名(filename)</td><td>"
    + file.getfilename() + "</td></tr>");
    out.println("<tr><td>文件擴展名(fileext)</td><td>"
    + file.getfileext() + "</td></tr>");
    out.println("<tr><td>文件全名(filepathname)</td><td>"
    + file.getfilepathname() + "</td></tr>");
    out.println("</table><br>");

    // 將文件另存
    // file.saveas("/upload/" + myfile.getfilename());
    // 另存到以web應用程序的根目錄為文件根目錄的目錄下
    // file.saveas("/upload/" + myfile.getfilename(),
    su.save_virtual);
    // 另存到操作系統的根目錄為文件根目錄的目錄下
    // file.saveas("c://temp//" + myfile.getfilename(),
    su.save_physical);

    }
    %>
    </body>
    </html>



    四、文件下載篇

    1、下載鏈接頁面download.html

    頁面源碼如下:

    <!--
    文件名:download.html
    作 者:縱橫軟件制作中心雨亦奇([email protected])
    -->
    <!doctype html public "-//w3c//dtd html 4.01 transitional//en">
    <html>
    <head>
    <title>下載</title>
    <meta http-equiv="content-type" content="text/html; charset=gb2312">
    </head>
    <body>
    <a href="/jsp/do_download.jsp">點擊下載</a>
    </body>
    </html>



    2、下載處理頁面do_download.jsp do_download.jsp展示了如何利用jspsmartupload組件來下載文件,從下面的源碼中就可以看到,下載何其簡單。

    源碼如下:

    <%@ page contenttype="text/html;charset=gb2312"
    import="com.jspsmart.upload.*" %><%
    // 新建一個smartupload對象
    smartupload su = new smartupload();
    // 初始化
    su.initialize(pagecontext);
    // 設定contentdisposition為null以禁止瀏覽器自動打開文件,
    //保證點擊鏈接后是下載文件。若不設定,則下載的文件擴展名為
    //doc時,瀏覽器將自動用word打開它。擴展名為pdf時,
    //瀏覽器將用acrobat打開。
    su.setcontentdisposition(null);
    // 下載文件
    su.downloadfile("/upload/如何賺取我的第一桶金.doc");
    %>



    注意,執行下載的頁面,在java腳本范圍外(即<% ... %>之外),不要包含html代碼、空格、回車或換行等字符,有的話將不能正確下載。不信的話,可以在上述源碼中%><%之間加入一個換行符,再下載一下,保證出錯。因為它影響了返回給瀏覽器的數據流,導致解析出錯。

    3、如何下載中文文件

    jspsmartupload雖然能下載文件,但對中文支持不足。若下載的文件名中有漢字,則瀏覽器在提示另存的文件名時,顯示的是一堆亂碼,很掃人興。上面的例子就是這樣。(這個問題也是眾多下載組件所存在的問題,很少有人解決,搜索不到相關資料,可嘆!)

    為了給jspsmartupload組件增加下載中文文件的支持,我對該組件進行了研究,發現對返回給瀏覽器的另存文件名進行utf-8編碼后,瀏覽器便能正確顯示中文名字了。這是一個令人高興的發現。于是我對jspsmartupload組件的smartupload類做了升級處理,增加了toutf8string這個方法,改動部分源碼如下:

    public void downloadfile(string s, string s1, string s2, int i)
    throws servletexception, ioexception, smartuploadexception
    {
    if(s == null)
    throw new illegalargumentexception("file '" + s +
    "' not found (1040).");
    if(s.equals(""))
    throw new illegalargumentexception("file '" + s +
    "' not found (1040).");
    if(!isvirtual(s) && m_denyphysicalpath)
    throw new securityexception("physical path is
    denied (1035).");
    if(isvirtual(s))
    s = m_application.getrealpath(s);
    java.io.file file = new java.io.file(s);
    fileinputstream fileinputstream = new fileinputstream(file);
    long l = file.length();
    boolean flag = false;
    int k = 0;
    byte abyte0[] = new byte[i];
    if(s1 == null)
    m_response.setcontenttype("application/x-msdownload");
    else
    if(s1.length() == 0)
    m_response.setcontenttype("application/x-msdownload");
    else
    m_response.setcontenttype(s1);
    m_response.setcontentlength((int)l);
    m_contentdisposition = m_contentdisposition != null ?
    m_contentdisposition : "attachment;";
    if(s2 == null)
    m_response.setheader("content-disposition",
    m_contentdisposition + " filename=" +
    toutf8string(getfilename(s)));
    else
    if(s2.length() == 0)
    m_response.setheader("content-disposition",
    m_contentdisposition);
    else
    m_response.setheader("content-disposition",
    m_contentdisposition + " filename=" + toutf8string(s2));
    while((long)k < l)
    {
    int j = fileinputstream.read(abyte0, 0, i);
    k += j;
    m_response.getoutputstream().write(abyte0, 0, j);
    }
    fileinputstream.close();
    }

    /**
    * 將文件名中的漢字轉為utf8編碼的串,以便下載時能正確顯示另存的文件名.
    * 縱橫軟件制作中心雨亦奇2003.08.01
    * @param s 原文件名
    * @return 重新編碼后的文件名
    */
    public static string toutf8string(string s) {
    stringbuffer sb = new stringbuffer();
    for (int i=0;i<s.length();i++) {
    char c = s.charat(i);
    if (c >= 0 && c <= 255) {
    sb.append(c);
    } else {
    byte[] b;
    try {
    b = character.tostring(c).getbytes("utf-8");
    } catch (exception ex) {
    system.out.println(ex);
    b = new byte[0];
    }
    for (int j = 0; j < b.length; j++) {
    int k = b[j];
    if (k < 0) k += 256;
    sb.append("%" + integer.tohexstring(k).
    touppercase());460) {this.resized=true; this.width=460;}">
    }
    }
    }
    return sb.tostring();
    }



    注意源碼中粗體部分,原jspsmartupload組件對返回的文件未作任何處理,現在做了編碼的轉換工作,將文件名轉換為utf-8形式的編碼形式。utf-8編碼對英文未作任何處理,對中文則需要轉換為%xx的形式。toutf8string方法中,直接利用java語言提供的編碼轉換方法獲得漢字字符的utf-8編碼,之后將其轉換為%xx的形式。

    將源碼編譯后打包成jspsmartupload.jar,拷貝到tomcat的shared/lib目錄下(可為所有web應用程序所共享),然后重啟tomcat服務器就可以正常下載含有中文名字的文件了。另,toutf8string方法也可用于轉換含有中文的超級鏈接,以保證鏈接的有效,因為有的web服務器不支持中文鏈接。

    小結:jspsmartupload組件是應用jsp進行b/s程序開發過程中經常使用的上傳下載組件,它使用簡單,方便。現在我又為其加上了下載中文名字的文件的支持,真個是如虎添翼,必將贏得更多開發者的青睞。
    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 辽中县| 土默特左旗| 廉江市| 绍兴县| 博客| 师宗县| 双峰县| 高碑店市| 平江县| 凭祥市| 达尔| 柘荣县| 枣阳市| 确山县| 淮南市| 昭苏县| 北流市| 连云港市| 大姚县| 兴国县| 沾益县| 宜良县| 台湾省| 清苑县| 葫芦岛市| 淮南市| 绍兴市| 五寨县| 阳原县| 乌恰县| 阜宁县| 汾阳市| 蓝田县| 增城市| 宿州市| 四会市| 长汀县| 绥中县| 西城区| 九龙城区| 疏勒县|