站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题文件上传原理

文件上传原理

摘要: 文件上传原理

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="664" border="0"> <tr> <td width="380">我们知道文件上传利用类似如下的表单来进行文件的选择:
upload.html
<%@ page contentType="text/html; charset=gb2312" %>

<body bgcolor="white">
<form action="doUpload.jsp" method="post" enctype="multipart/form-data">
<table>
<tr align="center">
<td width="55%" align="left"> <input type="file" name="text1"></td>
</tr>
<tr align="center">
<td width="55%" align="left"> <input type="file" name="text2"></td>
</tr>
<tr align="center">
<td colspan="2"> <input type="submit" value="上传"></td>
</tr>
</table> </form> </body> </td> <td width="274"> </td> </tr> </table>
表单中用enctype="multipart/form-data"来标明该表单可以进行文件上传操作。
那么当我们选择两个小文件1.txt与1.zip并点击提交按钮后,处理表单的jsp页面doUpload.jsp可以收到upload.html传过来的哪些信息呢?
请看:doUpload.jsp,它从请求对象中获取输入流,并将请求内容分行输出。
<%@ page contentType="text/html; charset=gb2312" %>
<%
int size=0;
byte readByte[]=new byte[4096];
int j=1;
ServletInputStream servletInputStream=request.getInputStream();
while((size=servletInputStream.readLine(readByte,0,readByte.length))!=-1){
String line=new String(readByte,0,size);
out.println("("+j+") "+line+"
");
j++;

}
%>

当上面文件执行以后会产生如下结果:(选择的上传文件不同,结果类似)
(1) -----------------------------7d45a76025c
(2) Content-Disposition: form-data; name="text1"; filename="C:\Documents and Settings\cwb\桌面\1.txt"
(3) Content-Type: text/plain
(4)
(5) public static void main(String[] args) {
(6) FileUtil.makeDir("dir1/dir2/dir3/dir4","c:/java/");
(7) }
(8) }
(9) -----------------------------7d45a76025c
(10) Content-Disposition: form-data; name="text2"; filename="C:\Documents and Settings\cwb\桌面\1.zip"
(11) Content-Type: application/x-zip-compressed
(12)
(13) PK&?1
(14) 6ex1.txtSP((M噬LV(.I,Re?)
(15) ???%E?檠?
(16) ?榕?
(17) 占\
(18) ??Z?????ぁ?Yd?$??1?0Q?J兑?,K?掖?T??怂U PK&?1
(19) 6ex ??1.txtPK3?
(20) -----------------------------7d45a76025c--

这里(1)(9)(20)行是分界线。从第(2)行与第(10)行中有上传文件的全名及其它信息。(4)与(12)是空行,空行后紧跟着上传文件的内容。如此一来,你应该明白怎样写上传组件了吧!提取空行后的文件内容,将它写入FileOutputStream。(请参看ntsky的上传组件源码)。
</td> </tr> <tr>


↑返回目录
前一篇: 发邮件激活注册账号
后一篇: 计算机单选题测试