JspServlet实现文件上传与下载.doc_第1页
JspServlet实现文件上传与下载.doc_第2页
JspServlet实现文件上传与下载.doc_第3页
JspServlet实现文件上传与下载.doc_第4页
JspServlet实现文件上传与下载.doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1. Jsp/Servlet:实现文件上传与下载 标签: uploadfilerandomservletdownloadjsp 2012-05-14 11:18 315人阅读 评论(0) 收藏 举报 This doc shows how to upload and download files with Jsp& Servlet technology.author: ZJ07-3-7Blog:url/url1客户端上传文件客户端通过一个Jsp 页面,上传文件到服务器,该Jsp页面必须含有File类表单,并且表单必须设置enctype=multipart/form-data。提交表单 时通过内置对象request,request.getInputStream();方法获得一个输入流。在上传文件时,会有附加信息,如下所示:-7d71042a40328Content-Disposition: form-data; name=fileforload; filename=C:Documents and SettingsZJ桌面book.txtContent-Type: text/plain/此处为文件内容-7d71042a40328Content-Disposition: form-data; name=submitcommit-7d71042a40328-附加信息大小为297字节(不确定这个值,测试得到),可通过request.getContentLength()297来判断是否上传了文件还是提交空字符串。2测试fileupload.jsp负责提交文件,accept.jsp负责实现上传功能。fileupload.jspThis page for FileUploadChoose the file for uploading:accept.jspThis page for response 297) InputStream in = request.getInputStream();File f =newFile(d:/temp,test.txt);FileOutputStream o =newFileOutputStream(f);byteb =newbyte1024;intn;while(n = in.read(b) != -1) o.write(b, 0, n);o.close();in.close();out.print(File upload success!);elseout.print(No file!);catch(IOException e) out.print(upload error.);e.printStackTrace();%服务器端得到的上传文件I like.txt,取名为test.txt-7d75b1540328Content-Disposition: form-data; name=fileforload; filename=C:Documents and SettingsZJ桌面I like.txtContent-Type: text/plain我喜欢驾驭着代码在风驰电掣中创造完美;我喜欢操纵着代码在随心所欲中体验生活;我喜欢用心情代码编制我小小的与众不同;每一段新的代码在我手中延生对我来说就象观看刹那花开的感动;我不需要焦点.因为我就是焦点!-7d75b1540328Content-Disposition: form-data; name=submitcommit-7d75b1540328-3去除附加信息按照HTTP协议,文件表单提交的信息中,前4行和后5行是表单本身的信息,中间部分才是上传的文件的内容。下例对上传的文件进行处理,获取文件名,并去除附加信息。4测试fileupload.jsp不变,accept.jsp修改如下:The real file297)/write the upload content to the temp file.InputStream in=request.getInputStream();byteb=newbyte1024;intn;while(n=in.read(b)!=-1)o.write(b,0,n);o.close();in.close();/read the temp file.RandomAccessFile random=newRandomAccessFile(temp,r);/read Line2 to find the name of the upload second=1;String secondLine=null;while(second=2)secondLine=random.readLine();second+;/get the last location of the dir position=secondLine.lastIndexOf();/get the name of the upload file.String fileName=secondLine.substring(position+1,secondLine.length()-1);/relocate to the head of file.random.seek(0);/get the location of the char.Enter in Line4.longforthEndPosition=0;intforth=1;while(n=random.readByte()!=-1&(forth=0)&(j=6)mark-;random.seek(mark);n=random.readByte();if(n=n)endPosition=random.getFilePointer();j+;/locate to the begin of content.Count for 4 liness end position.random.seek(forthEndPosition);longstartPoint=random.getFilePointer();/read the real content and write it to the realFile.while(startPoint(注:如果文件名是中文,会出现乱码。)5文件下载Jsp内置对象response调用方法getOutputStream()可以获取一个指向客户的输出流,服务器将文件写入这个流,然后可下载此文件。6测试download.jsp显示下载选项,LoadFile.java(Servlet)负责下载文件。download.jspdownload pageDownload:test.zipLoadFile.javapackagecom.zj.sample;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.OutputStream;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;publicclassLoadFileextendsHttpServlet publicvoiddoGet(HttpServletRequest request, HttpServletResponse response)throwsIOException, ServletException OutputStream o = response.getOutputStream();byteb =newbyte1024;/ the file to download.File fileLoad =newFile(d:/temp,test.rar);/ the dialogbox of download file.response.setHeader(Content-disposition,attachment;filename=+test.rar);/ set the MIME type.response.setContentType(application/x-tar);/ get the file length.longfileLength = fileLoad.length();String length = String.valueOf(fileLength);response.setHeader(Content_Length, length);/ download the file.FileInputStream in =newFileInputStream(fileLoad);intn = 0;while(n = in.read(b) != -1) o.write(b, 0, n);publicvoiddoPost(HttpServletRequest request, HttpServletResponse response)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论