通过 URLConnection 写入图像
我正在尝试通过 HttpURLConnection 写入图像。
我知道如何写文字,但我在尝试时遇到了真正的问题 写入图像
我已成功使用 ImageIO 写入本地 HD:
但我尝试通过 ImageIO 在 url 上写入图像,但失败了
URL url = new URL(uploadURL);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type", "multipart/form-data;
boundary=" + boundary);
output = new DataOutputStream(connection.getOutputStream());
output.writeBytes("--" + boundary + "\r\n");
output.writeBytes("Content-Disposition: form-data; name=\"" + FIELD_NAME + "\";
filename=\"" + fileName + "\"\r\n");
output.writeBytes("Content-Type: " + dataMimeType + "\r\n");
output.writeBytes("Content-Transfer-Encoding: binary\r\n\r\n");
ImageIO.write(image, imageType, output);
uploadURL 是服务器上 asp 页面的 url,该页面将上传带有文件名的图像在“content-Disposition:部分中给出。
现在,当我发送此内容时,asp页面会找到请求并找到文件名。但没有找到要上传的文件。
问题是,当通过ImageIO在URL上写入时,会发生什么? ImageIO 正在写入的文件的名称,
所以请帮助我 ImageIO 如何在 URLConnection 上写入图像以及我如何知道我必须在 asp 页面中使用的文件的名称来上传文件
感谢您采取是时候阅读这篇文章了 迪利普·阿加瓦尔
I am trying to write an image over an HttpURLConnection.
I know how to write text but I am having real problems trying
to write an image
I have succeeded in writing to the local HD using ImageIO:
But I am trying to write Image by ImageIO on url and failed
URL url = new URL(uploadURL);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type", "multipart/form-data;
boundary=" + boundary);
output = new DataOutputStream(connection.getOutputStream());
output.writeBytes("--" + boundary + "\r\n");
output.writeBytes("Content-Disposition: form-data; name=\"" + FIELD_NAME + "\";
filename=\"" + fileName + "\"\r\n");
output.writeBytes("Content-Type: " + dataMimeType + "\r\n");
output.writeBytes("Content-Transfer-Encoding: binary\r\n\r\n");
ImageIO.write(image, imageType, output);
the uploadURL is the url to an asp page on the server which will upload the image with the file name given in "content-Disposition: part.
now when I send this then asp page find the request and find the name of file. but does not find the file to be uploaded.
The problem is that when writing by ImageIO on URL what will the name of the file on which the ImageIO is writing,
So please help me how ImageIO will write an image on URLConnection and how can I know the name of the file which I have to use in the asp page to upload the file
Thanks for taking the time to read this post
Dilip Agarwal
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我认为您应该在写入图像后调用 io.flush() ,然后调用 io.close() 。
第二种内容类型对我来说似乎很奇怪。看来您正在尝试提交表单,而它实际上是图像。我不知道您的 asp 期望什么,但通常当我编写通过 HTTP 传输文件的代码时,我会发送适当的内容类型,例如
image/jpeg
。例如,下面是我从我编写的一个小实用程序中提取的代码片段,并在当前工作中使用:
我在这里使用了从 Jakarta IO utils 获取的方法 copy()。下面是供参考的代码:
显然服务器端必须准备好直接从 POST body 读取图像内容。
我希望这有帮助。
First I believe that you should call
io.flush()
and thenio.close()
after writing image.Second content type seems strange for me. It seems that you are trying to submit form while it is actually image. I do not know what does your asp expect but typically when I write code that should transfer file over HTTP I send appropriate content type, e.g.
image/jpeg
.Here is for example code snippet I extracted from one small utility that I wrote and I am using during my current work:
I used here method copy() that I took from Jakarta IO utils. Here is the code for reference:
Obviously the server side must be ready to read the image content directly from POST body.
I hope this helps.
OP 似乎已被遗忘,但为了 Kite 先生的利益:
您可能需要添加 Gzip 压缩 - 请参阅 当我将其发布到时文件已损坏使用 GZIPOutputStream 的 servlet 用于具有或不具有 Gzip 的工作类。 ImageIO 这里没有位置 - 只需将字节写入线路并在服务器上使用 ImageIO 即可。基于@BalusC答案
The OP seems lost into oblivion but for the benefit of Mister Kite :
You may want to add Gzip compression - see file corrupted when I post it to the servlet using GZIPOutputStream for a working class with or without Gzip. The ImageIO has no place here - just write the bytes past the wire and use ImageIO to your heart's content on the server. Based on @BalusC answer