Java 应用程序通过 POST 发送(无代码)

发布于 2024-11-27 07:35:12 字数 695 浏览 0 评论 0原文

我现在没有可用的代码,但稍后我会描述我的情况并发布代码:)。基本上,我的问题是通过 POST(从 Java 桌面应用程序/客户端)将图像上传到服务器。我在这里找到了一些关于这个问题的讨论,但它要么包括使用 org.apache.commons.httpclient 类,并且我使用 URLConnection 类实现了大部分代码,要么其中存在错误。检查此选定的答案,以尽可能详细地了解我遵循的内容:使用 java.net.URLConnection 来触发和处理 HTTP 请求

不管怎样,我在本地服务器上创建了 php 脚本来处理文件上传的表单。我从与 .php 一起创建的一个静态 html 中检查了它。然后我在没有上传文件的情况下检查了它,它工作正常。 当我发送纯文本文件时它也可以正常工作。这看起来像是与编码相关的问题。也许某个奇怪的角色正在破坏我的传输?

正如我所说,今天晚些时候如有必要,我将添加代码,但我对您在通过 POST 发送时与编码图像相关的意见/经验感兴趣。我应该做其他事情,上面的链接中没有提到。

问题:通过 http post 以多部分消息发送二进制文件时,是否需要对实际数据使用某种编码?

抱歉,现在没有代码。

干杯

I don't have the code available right now, but I'll describe my situation and post code later on :). Basically, my problem is with uploading image to server via POST (from Java desktop application / client). I found some discussions here on this matter, but it either included using org.apache.commons.httpclient classes, and I have most of the code implemented using URLConnection class, OR with bugs in it. Check this selected answer to see what I followed in as much detail as possible: Using java.net.URLConnection to fire and handle HTTP requests

Anyways, I created php script on my local server that handles form with file upload. I checked it from one static html that was created along with .php. I then checked it without file upload, and it works ok.
It also works ok when I send plain text file. This looks like a problem related to encoding. Maybe some strange character is breaking my transfer?

As I said, I'll include code if necessary later today, but I'm interested in your opinions / experience related to encoding images when sending via POST. Should I do something else, that's not being mentioned in the link above.

QUESTION: do I need to use some kind of encoding to actual data when sending binary file in multipart message via http post?

Sorry for no code right now.

Cheers

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

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

发布评论

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

评论(2

呢古 2024-12-04 07:35:12

至于你的问题:是的,你需要对文件进行编码。

我会为此使用 HttpClient,因为它会为您处理所有这些事情。无需重新发明轮子。请参阅如何上传使用 Java HttpClient 库与 PHP 一起使用的文件 代码。

As for your question: yes, you need to encode the file.

I would use HttpClient for this as it takes care of all those things for you. No need to reinvent the wheel. See How to upload a file using Java HttpClient library working with PHP for code.

贱贱哒 2024-12-04 07:35:12

如果您使用 URLConnection 手动处理多部分/表单数据,我想您需要将“Content-Transfer-Encoding:binary”添加到图像部分。请参阅 W3C 文档

If you are using URLConnection to handle multipart/form-data manually, I guess you you need to add "Content-Transfer-Encoding: binary" to the image part. See W3C's docs.

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