具有输入流的 httpclient 多部分发布

发布于 2024-12-16 14:49:10 字数 650 浏览 2 评论 0原文

我正在寻找简单而干净的 HTTP 多部分发布解决方案,它将发送一些字符串(表单数据)和几个具有流支持的文件。 (文件需要流式传输以避免内存不足错误) 如果可能的话,我想通过内置的“org.apache.httpclient”来实现这一点。

我能够使用 HttpURLConnection 创建一个干净的解决方案。 尽管我对此解决方案做出了所有努力,但字符串还是使用 8859-x 编码而不是 UTF-8 发送。

编辑:我的代码可在 MultiPart with HttpURLConnection source

以下代码创建了一个输出流:

HttpURLConnection connection = setupConnection();
dataOutputStream = new DataOutputStream(connection.getOutputStream());

我使用 我只是用 dataOutputStream.writeBytes 写入数据

如果我可以从 httpclient 获取输出流那就太好了,但它似乎以不同的方式工作。

任何帮助表示赞赏。 谢谢

I'm looking for simple and clean solution for HTTP multipart post, which will send some Strings (form data) and several files with streaming support. (files needs to be streamed to avoid out of memory error)
I'd like to achieve this with the built-in "org.apache.httpclient" if possible.

I was able to create a clean solution with HttpURLConnection.
Despite all my efforts with this solution strings were sent with 8859-x encoding instead of UTF-8.

EDIT: My code is available at MultiPart with HttpURLConnection source

I created an output stream with this code:

HttpURLConnection connection = setupConnection();
dataOutputStream = new DataOutputStream(connection.getOutputStream());

After this I just wrote the data with dataOutputStream.writeBytes

If i could get an outputstream from httpclient it would be great, however it seems it works a different way.

Any help is appreciated.
Thanks

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

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

发布评论

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

评论(1

琉璃繁缕 2024-12-23 14:49:10

我刚刚为此创建了一个简单的解决方案:android_multipart_entity

它是免费的(包括商业用途),但是如果可能的话,请在我的课程中保留对我的引用。

它旨在与内置的 Android HttpClient 一起使用。示例使用代码:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yourhost.com");
MultipartEntity entity = new MultipartEntity();
entity.addPart(new StringPart("name", "yourname"));
File imageFile = // .. get your image file
entity.addPart(new FilePart("picture", imageFile, null, "image/jpeg"));
httppost.setEntity(entity);
HttpResponse httpResponse = httpclient.execute(httppost);

编辑:

我查看了您的 MultiPart with HttpURLConnection 代码。由于使用 DataOutputStream,您会遇到 UTF-8 问题。 API 对于该类表示:

包装现有的 OutputStream 并向其中写入 big-endian 类型的数据。通常,该流可以由 DataInputStream 读取

这个课程不适合你的需要。为了读取数据,您必须在另一端有其直接相反的数据输入流 - DataInputStream

所以我的建议是使用普通的OutputStream。并向其中写入字节。像这样:

outputStream.write(partSeparator.getBytes());
outputStream.write(("Content-Disposition: form-data; name=\"" + parameter.getKey() + "\"" + lineEnd).getBytes());
outputStream.write(("Content-Type: text/plain; charset=UTF-8" + lineEnd).getBytes());
outputStream.write(("Content-Transfer-Encoding: 8bit" + lineEnd).getBytes());
outputStream.write(lineEnd.getBytes());
outputStream.write(parameter.getValue().getBytes("UTF-8")); // <= this is it!
outputStream.write(lineEnd.getBytes());
outputStream.write(partSeparator.getBytes());

I've just created a simple solution for this: android_multipart_entity.

It's free (including for commercial usage), however if this is possible please keep references to me inside of my classes.

It's designed to be used with the built in Android HttpClient. Sample usage code:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yourhost.com");
MultipartEntity entity = new MultipartEntity();
entity.addPart(new StringPart("name", "yourname"));
File imageFile = // .. get your image file
entity.addPart(new FilePart("picture", imageFile, null, "image/jpeg"));
httppost.setEntity(entity);
HttpResponse httpResponse = httpclient.execute(httppost);

EDIT:

I reviewed your MultiPart with HttpURLConnection code. You get UTF-8 issue because of DataOutputStream usage. API says for that class:

Wraps an existing OutputStream and writes big-endian typed data to it. Typically, this stream can be read in by DataInputStream.

This class just does not suit your needs. In order to read the data you would have to have its direct opposite - DataInputStream on the other end.

So my advice would be to use plain OutputStream. And write bytes to it. Smth like this:

outputStream.write(partSeparator.getBytes());
outputStream.write(("Content-Disposition: form-data; name=\"" + parameter.getKey() + "\"" + lineEnd).getBytes());
outputStream.write(("Content-Type: text/plain; charset=UTF-8" + lineEnd).getBytes());
outputStream.write(("Content-Transfer-Encoding: 8bit" + lineEnd).getBytes());
outputStream.write(lineEnd.getBytes());
outputStream.write(parameter.getValue().getBytes("UTF-8")); // <= this is it!
outputStream.write(lineEnd.getBytes());
outputStream.write(partSeparator.getBytes());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文