資源描述:
《圖片上傳技術.doc》由會員上傳分享,免費在線閱讀,更多相關內容在工程資料-天天文庫。
1、apachecommons-fileupload是一個很好的文件上傳工具,最近使用commons-fileupload實現(xiàn)了圖片的上傳及顯示,可將圖片保存在指定的文件夾中,也可以將圖片存放在數(shù)據(jù)庫,并支持四種常用的圖片格式:jpg,png,gif,bmp。首先,跟上傳一般文件一樣,需要寫-?個servlet來處理上傳的文件,你可以修改保存路徑或選擇將圖片保存在數(shù)據(jù)庫中,只需耍做簡單的修改就行了,servlet代碼如下:FileUploadServlet.javajava代碼1.2.4.5.6.7.8.9.10.packagecom.ek.
2、servlet;importimportimportimportimportimportimportimportimport11.importimportimportimportimportimportimportimportimportpublic/**12.13.14.15.16.17.18.19.20.java?awt?image?Bufferedlmage;java?io?ByteArrayOutputstrearn;java.io.File;java?io?IOException;java?io?InputStrearn;jav
3、a?util?Iterator;java?util?List;java?util?regex?Matcher;java?util?regex?Pattern;javaXeServlet?ServletException;javax.servlet?http.HttpServlet;javax.servlet.http.HttpServletRequest;javax.servlet?http.HttpServletResponse;org.apache.commons.fileupload.Fileitem;org.apache.comm
4、ons.fileupload.FileUploadException;org>apache.commons.fileupload.disk?DiskFileltemFactory;org.apache.commons.fileupload.servlet.ServletFileUpload;com.ek.image.Imagelltil;classFileUploadServletextendsHttpServlet{21.22.23.24.25.26.*/privatestaticprivatestatic/**finallongser
5、ialVersionUID=IL;StringfilePath=,,H;27.*Destructionoftheservlet.28.*/29.publicvoiddestroy(){30.super.destroy();//Justputs"destroy1'stringinlog31.//Putyourcodehere32?}33?/**34.*ThedoPostmethodoftheservlet.35?36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57
6、.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77.78.*Thismethodiscalledwhenaformhasitstagvaluemethodequalsto*post.**@paramrequest*therequestsendbythedienttotheserver*@paramresponse*theresponsesendbytheservertothedient*@throwsServletException*ifanerroroccurred*
7、@throwsIOException*ifanerroroccurred*/publicvoiddoPost(HttpServletRequestreq^HttpServletResponseres)throwsServletException,IOException{res.setContentType(Htext/html;charset=UTF-8");DiskFileltemFactoryfactory=newDiskFileltemFactory();//maximumsizethatwillbestoredinmemoryfa
8、ctory.setSizeThreshold(4096);//thelocationforsavingdatathatislargerthangetSizeThreshold()factory