`
zc4530
  • 浏览: 85207 次
  • 性别: Icon_minigender_1
  • 来自: 长春
社区版块
存档分类
最新评论

Jsp中实现文件上传与下载

阅读更多
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://zhangjunhd.51cto.com/blog/113473/19631
This doc shows how to upload and download files with Jsp& Servlet technology.
author: ZJ 07-3-7
Blog: http://zhangjunhd.blog.51cto.com/
1.客户端上传文件
    客户端通过一个Jsp页面,上传文件到服务器,该Jsp页面必须含有File类表单,并且表单必须设置enctype="multipart/form- data"。提交表单时通过内置对象request,request.getInputStream();方法获得一个输入流。
    在上传文件时,会有附加信息,如下所示:
-----------------------------7d71042a40328
Content-Disposition: form-data; name="fileforload"; filename="C:\Documents and Settings\ZJ\桌面\book.txt"
Content-Type: text/plain
//此处为文件内容
-----------------------------7d71042a40328
Content-Disposition: form-data; name="submit"
commit
-----------------------------7d71042a40328--
    附加信息大小为297字节(不确定这个值,测试得到),可通过request.getContentLength()>297来判断是否上传了文件还是提交空字符串。
2.测试
    fileupload.jsp负责提交文件,accept.jsp负责实现上传功能。
fileupload.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>This page for FileUpload</title>
</head>
<body>
<p>Choose the file for uploading:
<form action="accept.jsp" method=post enctype="multipart/form-data">
  <input type=file name=fileforload size=30>
  <br>
  <input type=submit value=commit name=submit>
</form>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>This page for response</title>
</head>
<body>
<%try{
 if(request.getContentLength()>297){
   InputStream in=request.getInputStream();
   File f=new File("d:/temp","test.txt");
   FileOutputStream o=new FileOutputStream(f);
   byte b[]=new byte[1024];
   int n;
   while((n=in.read(b))!=-1){
  o.write(b,0,n);
   }
   o.close();
   in.close();
   out.print("File upload success!");
 }
 else{
  out.print("No file!");
 }
}
catch(IOException e){
 out.print("upload error.");
 e.printStackTrace();
}
%>
</body>
</html>
    服务器端得到的上传文件I like.txt,取名为test.txt
-----------------------------7d75b1540328
Content-Disposition: form-data; name="fileforload"; filename="C:\Documents and Settings\ZJ\桌面\I like.txt"
Content-Type: text/plain
我喜欢驾驭着代码在风驰电掣中创造完美;
我喜欢操纵着代码在随心所欲中体验生活;
我喜欢用心情代码编制我小小的与众不同;
每一段新的代码在我手中延生对我来说就象观看刹那花开的感动;
我不需要焦点.因为我就是焦点!
-----------------------------7d75b1540328
Content-Disposition: form-data; name="submit"
commit
-----------------------------7d75b1540328--
3.去除附加信息
    按照HTTP协议,文件表单提交的信息中,前4行和后5行是表单本身的信息,中间部分才是上传的文件的内容。下例对上传的文件进行处理,获取文件名,并去除附加信息。
4.测试
    fileupload.jsp不变,accept.jsp修改如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>The real file</title>
</head>
<body>
<%try{
 //use sessionid to create a temp file.
 String tempFileName=(String)session.getId();
 //create the temp file.
 File temp=new File("d:/temp",tempFileName);
 FileOutputStream o=new FileOutputStream(temp);
 if(request.getContentLength()>297){
   //write the upload content to the temp file.
   InputStream in=request.getInputStream();
   byte b[]=new byte[1024];
   int n;
   while((n=in.read(b))!=-1){
    o.write(b,0,n);
   }
   o.close();
   in.close();
   //read the temp file.
   RandomAccessFile random=new RandomAccessFile(temp,"r");
   //read Line2 to find the name of the upload file.
   int second=1;
   String secondLine=null;
   while(second<=2){
    secondLine=random.readLine();
    second++;
   }
   //get the last location of the dir char.'\\'.
   int 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.
   long forthEndPosition=0;
   int forth=1;
   while((n=random.readByte())!=-1&&(forth<=4)){
    if(n=='\n'){
     forthEndPosition=random.getFilePointer();
     forth++;
    }
   }
   File realFile=new File("d:/temp",fileName);
   RandomAccessFile random2=new RandomAccessFile(realFile,"rw");
   //locate the end position of the content.Count backwards 6 lines.
   random.seek(random.length());
   long endPosition=random.getFilePointer();
   long mark=endPosition;
   int j=1;
   while((mark>=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 lines's end position.
   random.seek(forthEndPosition);
   long startPoint=random.getFilePointer();
   //read the real content and write it to the realFile.
   while(startPoint<endPosition-1){
    n=random.readByte();
    random2.write(n);
    startPoint=random.getFilePointer();
   }
   random2.close();
   random.close();
   //delete the temp file.
   temp.delete();
   out.print("File upload success!");
 }
 else{
  out.print("No file!");
 }
}
catch(IOException e){
 out.print("upload error.");
 e.printStackTrace();
}
%>
</body>
</html>
    (注:如果文件名是中文,会出现乱码。)
5.文件下载
    Jsp内置对象response调用方法getOutputStream()可以获取一个指向客户的输出流,服务器将文件写入这个流,然后可下载此文件。
6.测试
    download.jsp显示下载选项,LoadFile.java(Servlet)负责下载文件。
download.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>download page</title>
</head>
<body>
<a href=loadFile>Download:test.zip</a>
</body>
</html>
package com.zj.sample;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class LoadFile extends HttpServlet{
 public void doGet(HttpServletRequest request,HttpServletResponse response)
   throws IOException,ServletException{
    OutputStream o=response.getOutputStream();
    byte b[]=new byte[1024];
    //the file to download.
    File fileLoad=new File("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.
    long fileLength=fileLoad.length();
    String length=String.valueOf(fileLength);
    response.setHeader("Content_Length",length);
    //download the file.
    FileInputStream in=new FileInputStream(fileLoad);
    int n=0;
    while((n=in.read(b))!=-1){
     o.write(b,0,n);
    }
 }
 public void doPost(HttpServletRequest request,HttpServletResponse response)
 throws IOException,ServletException{
  doGet(request,response);
 }
web.xml(注册servlet)
<servlet>
  <servlet-name>LoadFileServlet</servlet-name>
  <servlet-class>com.zj.sample.LoadFile</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>LoadFileServlet</servlet-name>
  <url-pattern>/loadFile</url-pattern>
</servlet-mapping>
分享到:
评论

相关推荐

    JSP实现文件上传与下载.pdf

    "JSP实现文件上传与下载" JSP(Java Server Pages)是一种动态网页技术,用于开发基于Web的应用程序。JSP技术可以和其他Java技术集成,实现复杂的Web应用程序。下面是JSP实现文件上传与下载的知识点总结: 一、JSP...

    利用jsp实现文件上传下载

    JSP 实现文件上传下载 在本文中,我们将学习如何使用 JSP 实现文件上传和下载功能。在这个过程中,我们将使用 Apache 的 Commons FileUpload 和 Commons IO 两个库来处理文件上传和下载。 首先,让我们了解一下...

    jsp实现文件上传文档jsp实现文件上传文档

    jsp实现文件上传文档是指使用jsp技术实现文件上传和下载功能的文档。该文档主要介绍了jspSmartUpload组件的安装和使用方法,以及该组件的特点和功能。 jsp实现文件上传文档的主要内容包括: 1、jspSmartUpload组件...

    使用jspSmartUpload实现文件上传下载

    总结,`jspSmartUpload`为Java Web开发者提供了一个强大的工具,使他们能够方便地实现文件上传和下载功能。通过了解`SmartUpload`类的关键方法和使用示例,开发者可以更好地在项目中集成这个组件,提升用户体验,...

    jsp实现文件上传下载

    以上就是使用Java和JSP实现文件上传下载的基本步骤。在实际项目中,还需要考虑错误处理、安全性(防止文件覆盖、非法文件上传等)、用户体验优化(如上传进度显示)等问题。`SmartUpload`库提供的API可以方便地解决...

    jsp文件的上传和下载

    本项目专注于解决在JSP中实现文件上传和下载时遇到的一些常见问题,特别是针对中文文件名的处理。下面我们将深入探讨这个主题。 首先,文件上传通常涉及到HTTP协议中的multipart/form-data类型表单。在JSP中,我们...

    JSP实现文件上传和下载

    JSP实现文件上传和下载需要理解HTTP协议、Servlet原理以及文件操作。通过使用Apache Commons FileUpload库,可以简化文件上传的处理,而文件下载则需要设置正确的响应头和处理文件流。在实际应用中,还需要考虑安全...

    jsp实现文件的上传与下载

    以上就是使用JSP和SmartUpload组件实现文件上传与下载的基本步骤和关键点。在实际应用中,还需要考虑错误处理、安全性(防止文件覆盖、大小限制、非法文件类型)等问题,以及优化用户体验,如显示上传进度、预览文件...

    jspsmart实现文件上传下载 jspSmartUpload.jar下载

    通过对上述知识点的学习,相信读者已经掌握了如何使用`jspSmartUpload`实现文件上传下载的基本操作,以及如何与数据库进行集成。在未来的工作中,可以进一步探索`jspSmartUpload`的更多高级功能,以便更好地满足各种...

    JSP中实现文件的上传和下载

    总结,JSP中的文件上传和下载可以通过使用第三方库如`jspSmartUpload.jar`来简化实现,同时注意安全性和性能优化,以确保Web应用的稳定和用户友好。通过以上步骤,开发者可以构建出健壮的文件管理功能,满足用户的...

    jsp实现上传和下载

    本项目专注于使用JSP来实现文件的上传和下载功能,这对于Web应用程序来说是至关重要的,特别是那些需要处理用户数据交互的系统。 一、JSP上传文件 1. **表单设计**:在JSP页面中,我们需要创建一个HTML表单,包含`...

    基于jsp的文件上传下载

    本项目详细阐述了如何利用JSP实现文件的上传和下载功能。 首先,文件上传涉及的主要技术有HTML表单、Servlet和多部分请求。在HTML表单中,我们需要设置`&lt;input type="file"&gt;`标签让用户选择要上传的文件。当用户...

    jsp实现文件上传下载功能

    ### JSP 实现文件上传与下载功能详解 #### 一、引言 在现代Web应用开发中,文件的上传和下载是一项基本而重要的功能。JavaServer Pages (JSP) 是一种广泛使用的服务器端脚本技术,它允许开发者创建动态网页,并且...

    jsp+js+oracle 实现文件上传数据库,下载到本地

    安全性和性能优化也是实现文件上传下载时必须考虑的问题。例如,限制文件大小以防止DoS攻击,对文件名进行清理以防止路径遍历漏洞,以及使用缓存和分块传输优化大文件下载速度。 总结一下,"jsp+js+oracle 实现文件...

    利用Javabean+JSP 实现文件的上传、显示、下载

    综上所述,利用Javabean和JSP实现文件的上传、显示和下载涉及到了HTTP请求处理、文件I/O、数据库操作、安全性控制等多个方面,是一个典型的Java Web应用场景。在实际项目中,还可以结合Servlet、MVC框架(如Spring ...

    jspSmartUpload实现文件上传下载

    ### jspSmartUpload实现文件上传下载 #### 一、SmartUpload简介 SmartUpload是一个非常流行的Java Web组件,用于处理文件的上传与下载操作。它简化了文件上传过程中的复杂度,使得开发人员能够轻松地在Web应用中...

    jsp 实现文件上传

    在 do_upload.jsp 文件中,我们使用了 Java 语言来实现文件上传功能。首先,我们使用了 File 类来创建一个文件对象,该文件对象用于存储上传的文件。然后,我们使用了 FileOutputStream 类来将上传的文件写入到...

    jsp页面实现文件上传下载

    在Java Web开发中,JSP(JavaServer Pages)是一种用于创建动态网页的...总的来说,JSP页面实现文件上传下载涉及了HTML表单、Servlet/JSP处理、文件I/O操作等多个方面,理解这些知识点对于进行Java Web开发至关重要。

    jsp+servlet实现文件上传下载

    ### jsp+servlet实现文件上传下载 在现代Web开发中,文件上传下载是常见的功能之一。JSP(JavaServer Pages)与Servlet技术结合可以轻松实现这一功能。本篇将详细介绍如何利用jspSmartUpload组件实现文件的上传与...

Global site tag (gtag.js) - Google Analytics