jsp+servlet实现文件上传与下载功能
本文实例为大家分享了jspservlet实现文件上传与下载的具体代码,供大家参考,具体内容如下
上传:
需要导入两个包:commons-fileupload-1.2.1.jar,commons-io-1.4.jar
importjava.io.File; importjava.io.IOException; importjava.util.List; importjavax.servlet.ServletException; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importorg.apache.commons.fileupload.FileItem; importorg.apache.commons.fileupload.disk.DiskFileItemFactory; importorg.apache.commons.fileupload.servlet.ServletFileUpload; /** *上传附件 *@authornew * */ publicclassUploadAnnexServletextendsHttpServlet{ privatestaticStringpath=""; publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ doPost(request,response); } /* *post处理 *(non-Javadoc) *@seejavax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) */ publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ path=this.getServletContext().getRealPath("/upload"); try{ DiskFileItemFactoryfactory=newDiskFileItemFactory(); ServletFileUploadup=newServletFileUpload(factory); Listls=up.parseRequest(request); for(FileItemfileItem:ls){ if(fileItem.isFormField()){ StringFieldName=fileItem.getFieldName(); //getName()返回的是文件名字普通域没有文件返回NULL //StringName=fileItem.getName(); StringContent=fileItem.getString("gbk"); request.setAttribute(FieldName,Content); }else{ Stringnm=fileItem.getName().substring( fileItem.getName().lastIndexOf("\\")+1); Filemkr=newFile(path,nm); if(mkr.createNewFile()){ fileItem.write(mkr);//非常方便的方法 } request.setAttribute("result","上传文件成功!"); } } }catch(Exceptione){ e.printStackTrace(); request.setAttribute("result","上传失败,请查找原因,重新再试!"); } request.getRequestDispatcher("/pages/admin/annex-manager.jsp").forward( request,response); } }
下载(i/o流)无需导包:
importjava.io.IOException; importjava.net.URLEncoder; importjavax.servlet.ServletException; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; /** *下载文件 *@author * */ publicclassDownloadFilesServletextendsHttpServlet{ /** * */ privatestaticfinallongserialVersionUID=8594448765428224944L; publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ doPost(request,response); } /* *处理请求 *(non-Javadoc) *@seejavax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) */ publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse) throwsServletException,IOException{ Stringname=request.getParameter("fileName"); System.out.print("dddddddddd:"+name); //web绝对路径 Stringpath=request.getSession().getServletContext().getRealPath("/"); StringsavePath=path+"upload"; //设置为下载application/x-download response.setContentType("application/x-download"); //即将下载的文件在服务器上的绝对路径 Stringfilenamedownload=savePath+"/"+name; //下载文件时显示的文件保存名称 Stringfilenamedisplay=name; //中文编码转换 filenamedisplay=URLEncoder.encode(filenamedisplay,"UTF-8"); response.addHeader("Content-Disposition","attachment;filename=" +filenamedisplay); try{ java.io.OutputStreamos=response.getOutputStream(); java.io.FileInputStreamfis=newjava.io.FileInputStream( filenamedownload); byte[]b=newbyte[1024]; inti=0; while((i=fis.read(b))>0){ os.write(b,0,i); } fis.close(); os.flush(); os.close(); }catch(Exceptione){ } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。