大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> JSP专区 -> jspSmartUpload上传下载全攻略

jspSmartUpload上传下载全攻略

时间: 2021-07-31 作者:daque

一、安置篇   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是另存的选项,该选项有三个值,辨别是saveas_physical,saveas_virtual,saveas_auto。saveas_physical表白以操纵体例的根目次为文献根目次另存文献,saveas_virtual表白以web运用步调的根目次为文献根目次另存文献,saveas_auto则表白让组件确定,当web运用步调的根目次生存另存文献的目次时,它会采用saveas_virtual,要不会采用saveas_physical。 比方,saveas("/upload/sample.zip",saveas_physical)实行后若web效劳器安置在c盘,则另存的文献名本质是c:\upload\sample.zip。而saveas("/upload/sample.zip",saveas_virtual)实行后若web运用步调的根目次是webapps/jspsmartupload,则另存的文献名本质是webapps/jspsmartupload/upload/sample.zip。saveas("/upload/sample.zip",saveas_auto)实行时若web运用步调根目次下生存upload目次,则其功效同saveas("/upload/sample.zip",saveas_virtual),要不同saveas("/upload/sample.zip",saveas_physical)。 倡导:对于web步调的开拓来说,最佳运用saveas_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作 者:纵横软硬件创造重心雨亦奇(zhsoft88@sohu.com)--><!doctype html public "-//w3c//dtd html 4.01 transitional//en"><html><head><title>文献上传</title><meta http-equiv="content-type" content="text/html; charset=utf8"></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作 者:纵横软硬件创造重心雨亦奇(zhsoft88@sohu.com)--%><%@ page contenttype="text/html; charset=utf8" language="java" import="java.util.*,com.jspsmart.upload.*" errorpage="" %><html><head><title>文献上传处置页面</title><meta http-equiv="content-type" content="text/html; charset=utf8"></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作 者:纵横软硬件创造重心雨亦奇(zhsoft88@sohu.com)--><!doctype html public "-//w3c//dtd html 4.01 transitional//en"><html><head><title>载入</title><meta http-equiv="content-type" content="text/html; charset=utf8"></head><body><a href="jsp/do_download.jsp">点击载入</a></body></html>2、载入处置页面do_download.jsp do_download.jsp展现了怎样运用jspsmartupload组件来载入文献,从底下的源码中就不妨看到,载入何其大略。 源码如次: <%@ page contenttype="text/html;charset=utf8" 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");elseif(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)));elseif(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());} }}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步调开拓进程中常常运用的上传载入组件,它运用大略,简单。此刻我又为其加上了载入华文名字的文献的扶助,真个是锦上添花,必将博得更多开拓者的喜爱。

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.