Java 应用程序通过 POST 发送(无代码)
我现在没有可用的代码,但稍后我会描述我的情况并发布代码:)。基本上,我的问题是通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
至于你的问题:是的,你需要对文件进行编码。
我会为此使用 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.
如果您使用 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.