9 JSP 文件上傳下載_第1頁(yè)
9 JSP 文件上傳下載_第2頁(yè)
9 JSP 文件上傳下載_第3頁(yè)
9 JSP 文件上傳下載_第4頁(yè)
9 JSP 文件上傳下載_第5頁(yè)
已閱讀5頁(yè),還剩9頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、myeclipse 6 實(shí)戰(zhàn)開發(fā)講解視頻入門 10 jsp 文件上傳下載2007-12-2本視頻講解了如何使用最新版本開源的apache commons fileupload 來上傳文件以及如何編寫文件下載代碼. 視頻部分代碼屏幕出現(xiàn)閃爍, 錯(cuò)位, 不便之處請(qǐng)參考本文中的源碼和文檔中綠色部分的注釋:/ set factory constraintsfactory.setsizethreshold(yourmaxmemorysize); / 設(shè)置最多只允許在內(nèi)存中存儲(chǔ)的數(shù)據(jù),單位:字節(jié)factory.setrepository(yourtempdirectory); / 設(shè)置一旦文件大小超過ge

2、tsizethreshold()的值時(shí)數(shù)據(jù)存放在硬盤的目錄(默認(rèn)可以不用設(shè)置)/ create a new file upload handlerservletfileupload upload = new servletfileupload(factory);/ set overall request size constraint/ 設(shè)置允許用戶上傳文件大小,單位:字節(jié)upload.setsizemax(yourmaxrequestsize);友情提示: 下載微軟網(wǎng)盤文件時(shí)關(guān)閉下載工具, 否則你將得到錯(cuò)誤的文件, 雙擊 exe 會(huì)出來 dos 窗口. 正確操作是點(diǎn)擊文件名后能看到顯示下載鏈

3、接和文件大小等信息. 代碼: http:/cid- 132 kb 視頻: http:/cid- 16分31秒 6.0 mb 內(nèi)容包括: 1. apache commons fileupload 項(xiàng)目介紹 2. 下載并增加必要的類庫(kù) 3. 編寫文件上傳表單 html 4. 編寫文件上傳處理 jsp 5. 編寫文件下載jsp 6. 發(fā)布并測(cè)試 視頻截圖: 代碼: upload.htm login: password: 附件: upload.jsp 0) filename = filename.substring(filename.lastindexof(/) + 1, filename.lengt

4、h(); return filename; return ; %相關(guān)資料: 下載地址 /fileupload/ /io/ 用法文檔: /fileupload/using.html using fileuploadfileupload can be used in a number of different ways, depending upon the requirements of your application. in the simplest

5、 case, you will call a single method to parse the servlet request, and then process the list of items as they apply to your application. at the other end of the scale, you might decide to customize fileupload to take full control of the way in which individual items are stored; for example, you migh

6、t decide to stream the content into a database. here, we will describe the basic principles of fileupload, and illustrate some of the simpler - and most common - usage patterns. customization of fileupload is described elsewhere. fileupload depends on commons io, so make sure you have the version me

7、ntioned on the dependencies page in your classpath before continuing. how it worksa file upload request comprises an ordered list of items that are encoded according to rfc 1867, form-based file upload in html. fileupload can parse such a request and provide your application with a list of the indiv

8、idual uploaded items. each such item implements the fileitem interface, regardless of its underlying implementation. this page describes the traditional api of the commons fileupload library. the traditional api is a convenient approach. however, for ultimate performance, you might prefer the faster

9、 streaming api. each file item has a number of properties that might be of interest for your application. for example, every item has a name and a content type, and can provide an inputstream to access its data. on the other hand, you may need to process items differently, depending upon whether the

10、 item is a regular form field - that is, the data came from an ordinary text box or similar html field - or an uploaded file. the fileitem interface provides the methods to make such a determination, and to access the data in the most appropriate manner. fileupload creates new file items using a fil

11、eitemfactory. this is what gives fileupload most of its flexibility. the factory has ultimate control over how each item is created. the factory implementation that currently ships with fileupload stores the items data in memory or on disk, depending on the size of the item (i.e. bytes of data). how

12、ever, this behavior can be customized to suit your application. servlets and portletsstarting with version 1.1, fileupload supports file upload requests in both servlet and portlet environments. the usage is almost identical in the two environments, so the remainder of this document refers only to t

13、he servlet environment. if you are building a portlet application, the following are the two distinctions you should make as you read this document: where you see references to the servletfileupload class, substitute the portletfileupload class. where you see references to the httpservletrequest cla

14、ss, substitute the actionrequest class. parsing the requestbefore you can work with the uploaded items, of course, you need to parse the request itself. ensuring that the request is actually a file upload request is straightforward, but fileupload makes it simplicity itself, by providing a static me

15、thod to do just that. / check that we have a file upload requestboolean ismultipart = servletfileupload.ismultipartcontent(request);now we are ready to parse the request into its constituent items. the simplest casethe simplest usage scenario is the following: uploaded items should be retained in me

16、mory as long as they are reasonably small. larger items should be written to a temporary file on disk. very large upload requests should not be permitted. the built-in defaults for the maximum size of an item to be retained in memory, the maximum permitted size of an upload request, and the location

17、 of temporary files are acceptable. handling a request in this scenario couldnt be much simpler: / create a factory for disk-based file itemsfileitemfactory factory = new diskfileitemfactory();/ create a new file upload handlerservletfileupload upload = new servletfileupload(factory);/ parse the req

18、uestlist /* fileitem */ items = upload.parserequest(request);thats all thats needed. really! the result of the parse is a list of file items, each of which implements the fileitem interface. processing these items is discussed below. exercising more controlif your usage scenario is close to the simp

19、lest case, described above, but you need a little more control, you can easily customize the behavior of the upload handler or the file item factory or both. the following example shows several configuration options: / create a factory for disk-based file itemsdiskfileitemfactory factory = new diskf

20、ileitemfactory();/ set factory constraintsfactory.setsizethreshold(yourmaxmemorysize); / 設(shè)置最多只允許在內(nèi)存中存儲(chǔ)的數(shù)據(jù),單位:字節(jié)factory.setrepository(yourtempdirectory); / 設(shè)置一旦文件大小超過getsizethreshold()的值時(shí)數(shù)據(jù)存放在硬盤的目錄(默認(rèn)可以不用設(shè)置)/ create a new file upload handlerservletfileupload upload = new servletfileupload(factory);/

21、set overall request size constraint/ 設(shè)置允許用戶上傳文件大小,單位:字節(jié)upload.setsizemax(yourmaxrequestsize);/ parse the requestlist /* fileitem */ items = upload.parserequest(request); of course, each of the configuration methods is independent of the others, but if you want to configure the factory all at once, y

22、ou can do that with an alternative constructor, like this: / create a factory for disk-based file itemsdiskfileitemfactory factory = new diskfileitemfactory( yourmaxmemorysize, yourtempdirectory);should you need further control over the parsing of the request, such as storing the items elsewhere - f

23、or example, in a database - you will need to look into customizing fileupload. processing the uploaded itemsonce the parse has completed, you will have a list of file items that you need to process. in most cases, you will want to handle file uploads differently from regular form fields, so you migh

24、t process the list like this: / process the uploaded itemsiterator iter = items.iterator();while (iter.hasnext() fileitem item = (fileitem) iter.next(); if (item.isformfield() processformfield(item); else processuploadedfile(item); for a regular form field, you will most likely be interested only in

25、 the name of the item, and its string value. as you might expect, accessing these is very simple. / process a regular form fieldif (item.isformfield() string name = item.getfieldname(); string value = item.getstring(); .for a file upload, there are several different things you might want to know bef

26、ore you process the content. here is an example of some of the methods you might be interested in. / process a file uploadif (!item.isformfield() string fieldname = item.getfieldname(); string filename = item.getname(); string contenttype = item.getcontenttype(); boolean isinmemory = item.isinmemory

27、(); long sizeinbytes = item.getsize(); .with uploaded files, you generally will not want to access them via memory, unless they are small, or unless you have no other alternative. rather, you will want to process the content as a stream, or write the entire file to its ultimate location. fileupload

28、provides simple means of accomplishing both of these. / process a file uploadif (writetofile) file uploadedfile = new file(.); item.write(uploadedfile); else inputstream uploadedstream = item.getinputstream(); . uploadedstream.close();note that, in the default implementation of fileupload, write() w

29、ill attempt to rename the file to the specified destination, if the data is already in a temporary file. actually copying the data is only done if the the rename fails, for some reason, or if the data was in memory. if you do need to access the uploaded data in memory, you need simply call the get()

30、 method to obtain the data as an array of bytes. / process a file upload in memorybyte data = item.get();.resource cleanupthis section applies only, if you are using the diskfileitem. in other words, it applies, if your uploaded files are written to temporary files before processing them. such tempo

31、rary files are deleted automatically, if they are no longer used (more precisely, if the corresponding instance of java.io.file is garbage collected. this is done silently by an instance of mons.io.filecleaningtracker, which starts a reaper thread. in what follows, we assume that you are writing a w

32、eb application. in a web application, resource cleanup is controlled by an instance of javax.servlet.servletcontextlistener. in other environments, similar ideas must be applied. the filecleanercleanupyour web application should use an instance of mons.fileupload.filecleanercleanup. thats very easy,

33、 youve simply got to add it to your web.xml: . mons.fileupload.servlet.filecleanercleanup .creating a diskfileitemfactorythe filecleanercleanup provides an instance of mons.io.filecleaningtracker. this instance must be used when creating a mons.fileupload.disk.diskfileitemfactory. this should be don

34、e by calling a method like the following: public static diskfileitemfactory newdiskfileitemfactory(servletcontext context, file repository) filecleaningtracker filecleaningtracker = filecleanercleanup.getfilecleaningtracker(context); return new diskfileitemfactory(filecleaningtracker, diskfileitemfa

35、ctory.default_size_threshold, repository); disabling cleanup of temporary filesto disable tracking of temporary files, you may set the filecleaningtracker to null. consequently, created files will no longer be tracked. in particular, they will no longer be deleted automatically. interaction with vir

36、us scannersvirus scanners running on the same system as the web container can cause some unexpected behaviours for applications using fileupload. this section describes some of the behaviours that you might encounter, and provides some ideas for how to handle them. the default implementation of file

37、upload will cause uploaded items above a certain size threshold to be written to disk. as soon as such a file is closed, any virus scanner on the system will wake up and inspect it, and potentially quarantine the file - that is, move it to a special location where it will not cause problems. this, o

38、f course, will be a surprise to the application developer, since the uploaded file item will no longer be available for processing. on the other hand, uploaded items below that same threshold will be held in memory, and therefore will not be seen by virus scanners. this allows for the possibility of

39、 a virus being retained in some form (although if it is ever written to disk, the virus scanner would locate and inspect it). one commonly used solution is to set aside one directory on the system into which all uploaded files will be placed, and to configure the virus scanner to ignore that directo

40、ry. this ensures that files will not be ripped out from under the application, but then leaves responsibility for virus scanning up to the application developer. scanning the uploaded files for viruses can then be performed by an external process, which might move clean or cleaned files to an approv

41、ed location, or by integrating a virus scanner within the application itself. the details of configuring an external process or integrating virus scanning into an application are outside the scope of this document. watching progressif you expect really large file uploads, then it would be nice to re

42、port to your users, how much is already received. even html pages allow to implement a progress bar by returning a multipart/replace response, or something like that. watching the upload progress may be done by supplying a progress listener: /create a progress listenerprogresslistener progresslisten

43、er = new progresslistener() public void update(long pbytesread, long pcontentlength, int pitems) system.out.println(we are currently reading item + pitems); if (pcontentlength = -1) system.out.println(so far, + pbytesread + bytes have been read.); else system.out.println(so far, + pbytesread + of + pcontentlength + bytes have been read.); ;upload.setprogresslistener(progresslistener);do yourself a favour and i

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論