您的位置:首页 > Web前端 > JavaScript

Jsp/Servlet:实现文件上传与下载

2012-05-06 22:58 633 查看
允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。/article/4381675.html



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
[align=left]<html>[/align]
[align=left]<head>[/align]

<meta http-equiv="Content-Type"content="text/html;
charset=GB18030">

<title>This page for FileUpload</title>
[align=left]</head>[/align]
[align=left]<body>[/align]

<p>Choose the file for uploading:

<form action="accept.jsp"method=postenctype="multipart/form-data">

<input type=filename=fileforloadsize=30>

<br>

<input type=submitvalue=commitname=submit>

</form>
[align=left]</body>[/align]
</html>
accept.jsp
[align=left]<html>[/align]
[align=left]<head>[/align]
<%@
page language="java"import="java.io.*"%>
<metahttp-equiv="Content-Type"content="text/html;
charset=GB18030">
[align=left]<title>This page for response</title>[/align]
[align=left]</head>[/align]
[align=left]<body>[/align]

<%

try {

if (request.getContentLength() > 297) {
[align=left] InputStream in = request.getInputStream();[/align]
[align=left] File f =new File("d:/temp","test.txt");[/align]
[align=left] FileOutputStream o =new FileOutputStream(f);[/align]
[align=left] byte b[] =newbyte[1024];[/align]
[align=left] int n;[/align]
[align=left] while ((n = in.read(b)) != -1) {[/align]

o.write(b, 0, n);
[align=left] }[/align]
[align=left] o.close();[/align]
[align=left] in.close();[/align]
[align=left] out.print("File upload success!");[/align]
[align=left] }else {[/align]
[align=left] out.print("No file!");[/align]
[align=left] }[/align]

} catch (IOException e) {
[align=left] out.print("upload error.");[/align]
[align=left] e.printStackTrace();[/align]

}

%>

</body>
</html>
服务器端得到的上传文件Ilike.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修改如下:
[align=left]<html>[/align]
[align=left]<head>[/align]
<%@
page language="java"import="java.io.*"%>
<metahttp-equiv="Content-Type"content="text/html;
charset=GB18030">
[align=left]<title>The real file</title>[/align]
[align=left]</head>[/align]
[align=left]<body>[/align]
[align=left]<%try{[/align]

//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[]=newbyte[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'){
[align=left] forthEndPosition=random.getFilePointer();[/align]
[align=left] forth++;[/align]

}

}

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'){
[align=left] endPosition=random.getFilePointer();[/align]
[align=left] j++;[/align]

}

}

//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!");

}
[align=left]}[/align]
[align=left]catch(IOException e){[/align]

out.print("upload error.");

e.printStackTrace();
[align=left]}[/align]
[align=left]%>[/align]
[align=left]</body>[/align]
</html>
(注:如果文件名是中文,会出现乱码。)
5.文件下载
Jsp内置对象response调用方法getOutputStream()可以获取一个指向客户的输出流,服务器将文件写入这个流,然后可下载此文件。
6.测试
download.jsp显示下载选项,LoadFile.java(Servlet)负责下载文件。
download.jsp
[align=left]<html>[/align]
[align=left]<head>[/align]

<meta http-equiv="Content-Type"content="text/html;
charset=GB18030">

<title>download page</title>
[align=left]</head>[/align]
[align=left]<body>[/align]

<a href=loadFile>Download:test.zip</a>
[align=left]</body>[/align]
</html>
LoadFile.java
[align=left]package com.zj.sample;[/align]
[align=left]import java.io.File;[/align]
[align=left]import java.io.FileInputStream;[/align]
[align=left]import java.io.IOException;[/align]
[align=left]import java.io.OutputStream;[/align]
[align=left]import javax.servlet.ServletException;[/align]
[align=left]import javax.servlet.http.HttpServlet;[/align]
[align=left]import javax.servlet.http.HttpServletRequest;[/align]
[align=left]import javax.servlet.http.HttpServletResponse;[/align]
[align=left] [/align]
publicclass LoadFileextends
HttpServlet {

publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
[align=left] throws IOException, ServletException {[/align]

OutputStream o = response.getOutputStream();

byte b[] =newbyte[1024];

// the file to download.

File fileLoad = new File("d:/temp","test.rar");

// the dialogbox of download file.

response.setHeader("Content-disposition","attachment;filename="
[align=left] +"test.rar");[/align]

// 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) {
[align=left] o.write(b, 0, n);[/align]

}

}
[align=left] [/align]

publicvoid doPost(HttpServletRequest request, HttpServletResponse response) throws
IOException, ServletException {

doGet(request, response);

}
}
web.xml(注册servlet)
[align=left]<servlet>[/align]

<servlet-name>LoadFileServlet</servlet-name>

<servlet-class>com.zj.sample.LoadFile</servlet-class>
[align=left]</servlet>[/align]
[align=left]<servlet-mapping>[/align]

<servlet-name>LoadFileServlet</servlet-name>

<url-pattern>/loadFile</url-pattern>
</servlet-mapping>
本文出自 “子 孑” 博客,请务必保留此出处/article/4381675.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: