Servlet处理文件上传,为什么比原来大? [英] Servlet handling file-upload, Why bigger than the original?

查看:215
本文介绍了Servlet处理文件上传,为什么比原来大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Servlet doPost处理文件上传,

  InputStream in = req.getInputStream(); 

档案档案=新档案(c:/8.dat);
OutputStream out = new FileOutputStream(file);
byte [] buffer = new byte [1024];

int len = 0; ((len = in.read(buffer))!= - 1){
out.write(buffer,0,len);
while
}
bao.close();
out.close();
in.close();

剂量请求的getInputStream方法取http头信息?

为什么上传的文件比原来的要大?

解决方案

在HTTP请求中发送文件通常使用 multipart / form-data 编码完成。这使得服务器能够在单个请求中区分多个表单数据部分(否则将无法在单个请求中发送多个文件和/或输入字段)。每个部分由一个边界分隔,并以表单数据标题开头。整个请求体大致看起来像这样(以3个简单的< input type =text> 名称 name1 name2 name3 其值 value1 value2 value3 填充):

< pre-class =lang-none prettyprint-override> - SOME_BOUNDARY
content-disposition:form-data; name =name1
content-type:text / plain; charset = UTF-8
$ b $ value1
--SOME_BOUNDARY
content-disposition:form-data; name =name2
content-type:text / plain; charset = UTF-8
$ b value2
--SOME_BOUNDARY
content-disposition:form-data; name =name3
content-type:text / plain; charset = UTF-8

value3
--SOME_BOUNDARY--

使用名称 file1 的单个< input $ type =file> 字段,整个请求主体看起来像这样:

   -  SOME_BOUNDARY 
content-disposition:form-data; name =file1; filename =some.ext
content-type:application / octet-stream

二进制文件内容
--SOME_BOUNDARY--

这基本上就是你正在读的 request.getInputStream()。您应该将二进制文件内容解析出请求主体。正是这个边界和表单数据头让你上传的文件看起来更大(实际上也损坏了)。如果您使用的是servlet 3.0,则应该使用 request.getPart()来获得唯一的文件内容。

  InputStream content = request.getPart(file1)。getInputStream(); 
// ...

如果您仍然使用servlet 2.5或更旧版本,您可以使用Apache Commons FileUpload来解析它。



另请参阅:




Servlet doPost handing file-uploads,

    InputStream in = req.getInputStream();

    File file = new File("c:/8.dat");
    OutputStream out = new FileOutputStream(file);
    byte[] buffer = new byte[1024];

    int len =0;
    while((len=in.read(buffer))!=-1){
        out.write(buffer, 0, len);
    }
    bao.close();
    out.close();
    in.close();

Dose Request's getInputStream Method take the http header information?

Why is the uploaded file bigger than the original?

解决方案

Sending files in a HTTP request is usually done using multipart/form-data encoding. This enables the server to distinguish multiple form data parts in a single request (it would otherwise not be possible to send multiple files and/or input fields along in a single request). Each part is separated by a boundary and preceeded by form data headers. The entire request body roughly look like this (taking an example form with 3 plain <input type="text"> fields with names name1, name2 and name3 which have the values value1, value2 and value3 filled):

--SOME_BOUNDARY
content-disposition: form-data;name="name1"
content-type: text/plain;charset=UTF-8

value1
--SOME_BOUNDARY
content-disposition: form-data;name="name2"
content-type: text/plain;charset=UTF-8

value2
--SOME_BOUNDARY
content-disposition: form-data;name="name3"
content-type: text/plain;charset=UTF-8

value3
--SOME_BOUNDARY--

With a single <input type="file"> field with the name file1 the entire request body look like this:

--SOME_BOUNDARY
content-disposition: form-data;name="file1";filename="some.ext"
content-type: application/octet-stream

binary file content here
--SOME_BOUNDARY--

That's thus basically what you're reading by request.getInputStream(). You should be parsing the binary file content out of the request body. It's exactly that boundary and the form data header which makes your uploaded file to seem bigger (and actually also corrupted). If you're on servlet 3.0, you should have used request.getPart() instead to get the sole file content.

InputStream content = request.getPart("file1").getInputStream();
// ...

If you're still on servlet 2.5 or older, then you can use among others Apache Commons FileUpload to parse it.

See also:

这篇关于Servlet处理文件上传,为什么比原来大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆