您的位置:首页 > 编程语言 > Java开发

JAVA 文件上传下载笔记之使用基本IO流实现上传

2011-04-08 23:11 1061 查看
文件的上传下载在网络应用中随处可见,但具体的实现又多种多样,因此这里将上传下载功能在JAVA WEB中的实现做一个总结

当我们需要上传一个文件的时候,就要在form表单中添加一个file域,该域就说明要上传一个文件。同时要更改表单的提交方式,也就是要更改form域的enctype属性,该属性的默认值是:application/x-www-form-urlencoded,如果要包含file域
则需要将其改为:multipart/form-data。

我们先不改enctype属性,得到request流看不包含file域的表单数据打印出什么东西。

index.jsp

=======

<form action="<%=path%>/servlet/upload" method="post" enctype="application/x-www-form-urlencoded">
<input type="text" name="name" /><br/>
<input type="text" name="pwd" /><br/>
<input type="submit" value="submit"/>
</form>


从request的输入流中打印的内容:
name=liuyalong&pwd=123456

在上面表单中添加一个file域,打印结果:

name=liuyalong&pwd=123456&files=F%3A%5Cones.exe

可以看出file域提交上来的内容是上传文件的路径和文件名,但特殊字符都用另一种方式表示。将这种表示用URLDecode类进行解码得出的结果:F:/ones.exe,得到的就是上传路径和文件名,那么我们就可以得出,使用默认的enctype类型对于file域只能提交文件的路径和文件名。那么如果要将文件本身上传到服务器上面的话,就需要更改表单的提交类型了。

测试表单的enctype类型为:multipart/form-data

不包含file域的request输入流的内容。

-----------------------------7db3d838206d8
Content-Disposition: form-data; name="name"

liuyalong
-----------------------------7db3d838206d8
Content-Disposition: form-data; name="pwd"

123456
-----------------------------7db3d838206d8--

包含file域的的表单:

上传一个文本文件:

数据打印开始
-----------------------------7db1f438206d8
Content-Disposition: form-data; name="name"

liuyalong
-----------------------------7db1f438206d8
Content-Disposition: form-data; name="pwd"

123456
-----------------------------7db1f438206d8
Content-Disposition: form-data; name="files"; filename="C:/Documents and Settings/Administrator/妗岄潰/娴嬭瘯鏂囨湰.txt"
Content-Type: text/plain

这是测试上传文件的一个文本文件。this is a text file which test upload!
-----------------------------7db1f438206d8--
数据打印完毕

可以看出,这时request中包含的内容中file域不是文件路径和文件名而且包含文件的类型和内容。因为是字符流,所以文本文件的内容可以显示。而其他格式的文本则以"乱码"的形式。

分析内容:通过enctype=multipart/form-data得到的内容中,表单的每一个域都由多个"-"和一串字符串开始(这个字符串不固定),
文件域除了打印表单域名称之外还有一个filename=***,它的值就是文件名和路径。最后表单以多个"-"和字符串再加上两个"-"结束。那么我们就可以根据这些来处理上传的文件,我们先处理上传的文本文件,其他类型的文件只要使用字节流就行。

下面是代码:

index.jsp
=========

<form action="<%=path%>/servlet/upload" method="post" enctype="multipart/form-data">
<input type="text" name="name" /><br/>
<input type="text" name="pwd" /><br/>
<input type="file" name="files" /><br/>
<input type="submit" value="submit"/>
</form>


UploadServlet
=============

BufferedReader br = new BufferedReader(new InputStreamReader(input));
String temp = null ;
while((temp = br.readLine()) != null){
//当读取到的行满足下面格式的时候就说明表单结束
if(temp.startsWith("-----------------------------") && temp.endsWith("--")){
break;
}

//以-----开始则说明开始了一个表单域
if(temp.startsWith("-----------------------------")){
String filePath = null;
//下行包含filename则说明当前表单域是一个file域
if((filePath = br.readLine()).indexOf("filename") > 1){
//截取文件名
filePath = filePath.substring(filePath.lastIndexOf('//') + 1,filePath.lastIndexOf("/""));

br.readLine();	//跳过一行,这行是Content-Type:**
br.readLine();	//跳过一行,这行是空行,是必须的。表明从下一行开始就是上传的内容
System.out.println("filePath->" + filePath);
System.out.println("f--->" + getServletContext().getRealPath("/upload") + "//" + filePath);
File file = new File(getServletContext().getRealPath("/upload") + "//" + filePath);

if(!file.exists()){
System.out.println(file.getPath());
System.out.println(file.getName());
file.createNewFile();
}
PrintStream ps = new PrintStream(new FileOutputStream(file));
String content = null ;
while((content = br.readLine()) != null){
//已经读取到了下一个表单域
if(content.startsWith("-----------------------------")){
break ;
}
ps.println(content);
}
ps.flush();
ps.close();
}
}
}
br.close();
resp.sendRedirect(req.getContextPath() + "//index.jsp");


这是使用字符流的形式将文本文件上传到服务器,如果是其他类型的文件则需要使用字节流进行读写,方法应该是一样的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: