在java中通过HTTP post接收文件:文件损坏

发布于 2024-11-25 04:12:43 字数 1783 浏览 0 评论 0原文

我刚刚编写了一个通过 HTTP 接收 POST 请求的 HTTP 服务器。特别是,它接收作为多部分表单数据的请求:

POST / HTTP/1.1
Host: 192.168.7.4:5000
User-Agent: Mozilla/5.0 (X11; Linux i686; rv:5.0) Gecko/20100101 Firefox/5.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------197987737412371961922053527775
Content-Length: 4306786


-----------------------------197987737412371961922053527775
Content-Disposition: form-data; name="filename"

poison.mp3
-----------------------------197987737412371961922053527775
Content-Disposition: form-data; name="prova"

provaV
-----------------------------197987737412371961922053527775
Content-Disposition: form-data; name="datafile"; filename="01-Poison.mp3"
Content-Type: audio/mpeg

file......

在标头之后,每个输入都被编码在表单中:

----------------------------- 197987737412371961922053527775 内容处置 ...\r\n\r\n “输入内容”

最后一个包含二进制形式的文件。

这是我的服务器,它首先获取标头中包含的所有信息,然后尝试重建文件。 如果我从本地计算机发送请求,它工作正常,但如果我尝试从远程客户端发送文件,它会损坏文件。 我使用直接从套接字打开的简单输入流。

这是尝试创建发送文件的方法:

private void payloadFileCreation(InputStream in,boolean t1, FileOutputStream fos, long filesize ) throws IOException{

    int dyn_data_index=0;
    int chunk=2048;
    byte[] dyn_data = new byte[chunk];
    int av = in.available();
    while (filesize>chunk){
           in.read(dyn_data,0,chunk);
           fos.write(dyn_data,0,chunk);
       fos.flush();
       filesize -= chunk;

    }
    in.read(dyn_data,0,(int) filesize );
    fos.write(dyn_data,0, (int) filesize);
    fos.flush();
    fos.close();

    }

有什么想法吗? 谢谢

I've just wrote an HTTP server that receive POST request via HTTP. In particular it receives requests as multipart form data:

POST / HTTP/1.1
Host: 192.168.7.4:5000
User-Agent: Mozilla/5.0 (X11; Linux i686; rv:5.0) Gecko/20100101 Firefox/5.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------197987737412371961922053527775
Content-Length: 4306786


-----------------------------197987737412371961922053527775
Content-Disposition: form-data; name="filename"

poison.mp3
-----------------------------197987737412371961922053527775
Content-Disposition: form-data; name="prova"

provaV
-----------------------------197987737412371961922053527775
Content-Disposition: form-data; name="datafile"; filename="01-Poison.mp3"
Content-Type: audio/mpeg

file......

After the Header every input is encripted in the form:

-----------------------------197987737412371961922053527775
Conten_disposition ...\r\n\r\n
"input content"

The last one contains the file in binary form.

This is my Server that first grabs all informations contained in the header then try to rebuild the file.
If i send a request from my local machines it works fine, but if I try to send a file from a remote client it corrupts the file.
I'm using a simple InputStream directly opened from the socket.

This is the method that tries to created the sent file:

private void payloadFileCreation(InputStream in,boolean t1, FileOutputStream fos, long filesize ) throws IOException{

    int dyn_data_index=0;
    int chunk=2048;
    byte[] dyn_data = new byte[chunk];
    int av = in.available();
    while (filesize>chunk){
           in.read(dyn_data,0,chunk);
           fos.write(dyn_data,0,chunk);
       fos.flush();
       filesize -= chunk;

    }
    in.read(dyn_data,0,(int) filesize );
    fos.write(dyn_data,0, (int) filesize);
    fos.flush();
    fos.close();

    }

any ideas?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

碍人泪离人颜 2024-12-02 04:12:43

您可以使用 Apache Commons FileUpload 库来解析多部分表单请求。

You can use Apache Commons FileUpload library to parse multipart form requests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文