简体   繁体   English

如何通过Java Http Post发送和接受文件?

[英]how to send and accept file over Java Http Post?

I just want to send a text file and a JPEG file over the network. 我只想通过网络发送文本文件和JPEG文件。 fortunately, i have access to both the server code and the client code. 幸运的是,我可以访问服务器代码和客户端代码。 Here's my (google app engine) code. 这是我的(谷歌应用引擎)代码。

private void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
    GcsFilename filename = new GcsFilename("my-bucket", "my-file");
    Builder fileOptionsBuilder = new GcsFileOptions.Builder();
    fileOptionsBuilder.mimeType("text/html");
    GcsFileOptions fileOptions = fileOptionsBuilder.build();
    GcsOutputChannel outputChannel = gcsService.createOrReplace(filename, fileOptions);

    byte[] buffer = new byte[1024];
    InputStream reader = req.getInputStream();
    BufferedOutputStream outStream = new BufferedOutputStream(Channels.newOutputStream(outputChannel));
    while(true) {
        int bytesRead = reader.read(buffer);
        if (bytesRead == -1) {
            break; // have a break up with the loop.
        } else if (bytesRead < 1024) {
            byte[] temp = Arrays.copyOf(buffer, bytesRead);
            outStream.write(temp);
        } else {
            outStream.write(buffer);
        }
    }

    outStream.close();
    outputChannel.close();
}

As you can see, i use a raw InputStream to get all the data that is sent over the net. 如您所见,我使用原始InputStream来获取通过网络发送的所有数据。

and on the client side, i send a text file over like so: (in Android) 在客户端,我像这样发送文本文件:(在Android中)

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpost = new HttpPost("http://my-hosted-url/postit");
MultipartEntity entity = new entity.addPart("myImageFile", new FileBody(someLogFile));
httpost.setEntity(entity);
HttpResponse response;
response = httpClient.execute(httpost);

This works just fine... sort of. 这工作得很好...有点。 the problem is that when i try to view the file/data that is sent, it has a header on top of it, as such: 问题是,当我尝试查看发送的文件/数据时,它上面有一个标头,例如:

--NNqarc4FsG0G8hUzd82A6TCjgzKH Content-Disposition: form-data; name="myString" Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 8bit STRING_VALUE ---NNqarc4FsG0G8hUzd82A6TCjgzKH Content-Disposition: form-data; name="myImageFile"; filename="something.txt" Content-Type: application/octet-stream Content-Transfer-Encoding: binary

[Thu Aug 14 17:14:26 PDT 2014] then the real log starts here...

How do i get rid of the headers that is somehow stuck to the body? 我如何摆脱以某种方式卡在身体上的标头?

What you have here is a multipart request. 您在这里有一个多部分的请求。 It is a single request where the body consists of the various parts separated by a separator string. 这是单个请求,其中主体由用分隔符字符串分隔的各个部分组成。

In your case, it's more easily viewed as: 就您而言,它更易于查看为:

--NNqarc4FsG0G8hUzd82A6TCjgzKH 
Content-Disposition: form-data; name="myString" 
Content-Type: text/plain; charset=US-ASCII 
Content-Transfer-Encoding: 8bit 

STRING_VALUE 
---NNqarc4FsG0G8hUzd82A6TCjgzKH 
Content-Disposition: form-data; name="myImageFile"; filename="something.txt" 
Content-Type: application/octet-stream 
Content-Transfer-Encoding: binary

[binary here]

It has two parts where each part has its corresponding headers and body. 它有两个部分,每个部分都有对应的标题和正文。 I'm guessing you're interested in the bodies. 我猜你对尸体感兴趣。 You'll need to extract them. 您需要提取它们。

You can either read the HTTP specification and/or the specification about multipart requests and write your own parser, or you can use some built-in (I don't know if GAE is Servlet 3.0 ready or not) or 3rd party methods. 您可以阅读HTTP规范和/或有关多部分请求的规范并编写自己的解析器,也可以使用一些内置方法(我不知道GAE是否已准备好Servlet 3.0)或第三者方法。 See these 看这些

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM