互联网开发--web文件上传原理

来源:互联网 发布:泰格软件安装 编辑:程序博客网 时间:2024/06/16 15:45

前台

<form name="form1" action="test.htm" enctype="multipart/form-data" method="post" >      <input type="text" name="p1" value="测试1"></input></BR>      <input type="text" name="p2" value="测试2"></input></BR>      <input type="file" name="f1_file1" ></input></BR>      <input type="file" name="f1_file2" ></input></BR>      <input type="submit" value="开始上传" /></BR></form>
  • 如果form里有文件上传控件,一定要有enctype=”multipart/form-data” 属性,否则文件是提交不到后台的。
  • 表单中enctype=”multipart/form-data”的意思是设置表单的MIME编码。默认情况这个编码格式是application/x-www-form-urlencoded,不能用于文件上传。只有使用了multipart/form-data,才能完整的传递文件数据。

后台

@RequestMapping("/test.htm")    public void test(HttpServletRequest request, HttpServletResponse response) {        //(1)标记1        // String p1 = request.getParameter("p1");        // String p2 = request.getParameter("p2");        // System.out.println(p1 + " && " + p2);        //(2)标记2        System.out.println(request.getHeader("Content-type"));        //(3)保存参数流到本地        java.io.InputStream is = null;        java.io.FileOutputStream fos = null;        try {            is = request.getInputStream();            fos = new java.io.FileOutputStream("d:/out.txt");            byte[] buffer = new byte[8192];            int count = 0;            while ((count = is.read(buffer)) > 0) {                fos.write(buffer, 0, count);            }            fos.close();            System.out.println("---上传完毕---");        } catch (IOException e) {            e.printStackTrace();        }     }

如果表单以文件流的形式提交参数,那么后台只能通过request.getInputStream()方法获取所有参数流来接收参数了。
在request.getInputStream()操作之前不能调用其他的获取参数的方法,否则参数流为空,会获取不到的。
比如代码(1)处使用了request.getParameter(“p1”);这时本地的out.txt会为空。我个人理解是request.getParameter(“p1”)阻断了流,所以会为空。
打开本地的out.txt文件,内容如下:

------WebKitFormBoundaryrFwIRHZT5SfIkx05Content-Disposition: form-data; name="p1"测试111------WebKitFormBoundaryrFwIRHZT5SfIkx05Content-Disposition: form-data; name="p2"测试222------WebKitFormBoundaryrFwIRHZT5SfIkx05Content-Disposition: form-data; name="f1_file1"; filename="bigbang歌词.xlsx"Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet文件二进制流............------WebKitFormBoundaryrFwIRHZT5SfIkx05Content-Disposition: form-data; name="f1_file2"; filename="壁纸20130301160822.jpg"Content-Type: image/jpeg文件二进制流...........------WebKitFormBoundaryrFwIRHZT5SfIkx05--

分析参数流,我们可以看出参数流里的参数结构如下:
(1)参数分割符:

------WebKitFormBoundaryrFwIRHZT5SfIkx05

当前这次请求的所有参数,分隔符都一样。但是每次请求的参数分割符都不一样,可以通过request.getHeader(“Content-type”)获取当前的参数分隔符。见代码(2)处.注意,通过这个方法获得的值比实际文件里的分隔符前面少了两个小横线-,不知道为啥。
(2)参数属性:

Content-Disposition: form-data; name=”f1_file2”; filename=”壁纸20130301160822.jpg”
Content-Type: image/jpeg

Content-Disposition: form-data; name=”p1”

name对应表单里控件元素的name,普通表单控件只有name属性,文件控件多了个 filename属性。注意浏览器不同,filename会有所不同。比如谷歌和火狐,filename就是文件原始名称,而在IE浏览器下,filename就是客户端的文件绝对路径了。
(3)一个空行
(4)参数值
以上就是参数结构了。
在所有参数最后,有一个参数流结束符,如下:

------WebKitFormBoundaryrFwIRHZT5SfIkx05--

注意:参数流结束符比参数分隔符后面多了两个-小横线。

总结:
设置了enctype=”multipart/form-data”的话,form里面的所有input的值都会以2进制的方式传到服务器。所以通过request.getParams(“参数名”)的方式获取参数值就行不通了。网上有好多封装好的工具类可以直接拿过来用,用来接收二进制参数。比如SmartUpload类,可以下载相应的jar引入到自己的工程里。使用方法:

smart.getRequest().getParameter(); //获取普通文本参数File[] myFiles= smart.getFiles();// 获得上传的所有文件String ext = smart.getFiles().getFile(0).getFileExt();// 获得某文件后缀

获取表单的二进制参数不局限于这一个方法。如果你的项目是spring项目的话,可以通过配置如下的bean:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    <property name="defaultEncoding" value="utf-8"></property>    <property name="maxUploadSize" value="10485760000"></property>    <property name="maxInMemorySize" value="40960"></property></bean>

然后在相应的action里通过注解就可以接收文本参数、文件了;

0 0
原创粉丝点击