图像写在URLConnection上 [英] Image writing over URLConnection

查看:133
本文介绍了图像写在URLConnection上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在HttpURLConnection上写一个图像。

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

我已成功使用ImageIO写入本地高清:

I have succeeded in writing to the local HD using ImageIO:

但我正在尝试写Image by网址上的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);

uploadURL是服务器上asp页面的URL,它将上传带有文件名的图像在content-Disposition:part。

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.

中给出,现在当我发送它然后asp页面找到请求并找到文件的名称。但是找不到该文件已上传。

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.

问题是,当ImageIO在URL上写入ImageIO所写文件的名称时,

The problem is that when writing by ImageIO on URL what will the name of the file on which the ImageIO is writing,

那么请帮我看看ImageIO如何在URLConnection上写一个图像,我怎么知道我必须在asp页面上用来上传文件的文件的名称

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

感谢您花时间阅读这篇文章
Dilip Agarwal

Thanks for taking the time to read this post Dilip Agarwal

推荐答案

首先我相信您应该在写完图像后调用 io.flush()然后再调用 io.close()

First I believe that you should call io.flush() and then io.close() after writing image.

第二种内容类型对我来说似乎很奇怪。看来你正试图提交表单我不知道你的asp期望什么,但通常当我编写应该通过HTTP传输文件的代码时,我会发送适当的内容类型,例如: image / jpeg

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:

    URL url = new URL("http://localhost:8080/handler");
    HttpURLConnection con = (HttpURLConnection)url.openConnection();
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setUseCaches(false);
    con.setRequestProperty("Content-Type", "image/jpeg");
    con.setRequestMethod("POST");
    InputStream in = new FileInputStream("c:/temp/poc/img/mytest2.jpg");
    OutputStream out = con.getOutputStream();
    copy(in, con.getOutputStream());
    out.flush();
    out.close();
    BufferedReader r = new BufferedReader(new  InputStreamReader(con.getInputStream()));


            // obviously it is not required to print the response. But you have
            // to call con.getInputStream(). The connection is really established only
            // when getInputStream() is called.
    System.out.println("Output:");
    for (String line = r.readLine(); line != null;  line = r.readLine()) {
        System.out.println(line);
    }

我在这里使用了从Jakarta IO utils中获取的方法copy()。以下是供参考的代码:

I used here method copy() that I took from Jakarta IO utils. Here is the code for reference:

protected static long copy(InputStream input, OutputStream output)
        throws IOException {
    byte[] buffer = new byte[12288]; // 12K
    long count = 0L;
    int n = 0;
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

显然服务器端必须准备好直接从POST正文中读取图像内容。
我希望这会有所帮助。

Obviously the server side must be ready to read the image content directly from POST body. I hope this helps.

这篇关于图像写在URLConnection上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆