欢迎来到天天文库
浏览记录
ID:51144060
大小:150.50 KB
页数:10页
时间:2020-03-19
《图片上传技术.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、apachecommons-fileupload是一个很好的文件上传工具,最近使用commons-fileupload实现了图片的上传及显示,可将图片保存在指定的文件夹中,也可以将图片存放在数据库,并支持四种常用的图片格式:jpg,png,gif,bmp。首先,跟上传一般文件一样,需要写-•个servlet來处理上传的文件,你可以修改保存路径或选择将图片保存在数据库中,只需耍做简单的修改就行了,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
此文档下载收益归作者所有