预览加载中,请您耐心等待几秒...
1/3
2/3
3/3

在线预览结束,喜欢就下载吧,查找使用更方便

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

jsp/servlet文件上传整理 一、采用的架包 commons-fileupload-1.2.1.jar、commons-io-1.4.jar 二、jsp页面如下 file_upload.jsp <%@pagelanguage="java"pageEncoding="utf-8"%> <% Stringpath=request.getContextPath(); StringbasePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"> <html> <head> <basehref="<%=basePath%>"> <title>文件上传演示例子</title> </head> <body> <h3>文件上传</h3> <hr/> <formaction="fileUpload"method="post"enctype="multipart/form-data"> <table> <tr> <td> &nbsp; </td> <td> <inputtype="file"name="file"/> </td> </tr> <tr> <td> &nbsp; </td> <td> <inputtype="submit"value="文件上传"/> </td> </tr> </table> </form> </body> </html> jsp/servlet文件上传整理 三、servlet代码 FileUploadServlet.java packagecom.util; importjava.io.*; importjava.util.List; importjavax.servlet.*; importjavax.servlet.http.*; importorg.apache.commons.fileupload.FileItem; importorg.apache.commons.fileupload.disk.DiskFileItemFactory; importorg.apache.commons.fileupload.servlet.ServletFileUpload; publicclassFileUploadServletextendsHttpServlet{ privatestaticfinallongserialVersionUID=1L; publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse) throwsIOException,ServletException{ this.doPost(request,response); } @SuppressWarnings("unchecked") publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse) throwsIOException,ServletException{ //采用apache工具包进行文件上传操作 DiskFileItemFactoryfactory=newDiskFileItemFactory(); ServletFileUploadupload=newServletFileUpload(factory); try{ List<FileItem>fileitems=upload.parseRequest(request); for(FileItemitem:fileitems){ if(item.isFormField()){ Stringname=item.getFieldName(); Stringvalue=item.getString(); //转换下字符集编码 value=newString(value.getBytes("iso-8859-1"),"utf-8"); System.out.println(name+"="+value); }else{ Stringfilename=item.getName(); //System.out.println(filename); ServletContextcontext=getServletContext(); jsp/servlet文件上传整理 //上传的文件存放路径为...\\WebRoot\\upload\\filenam