java-通过URLConnection写入图像

博客内容讲述了在Java中如何使用HttpURLConnection上传图像到服务器的问题。作者在尝试通过ImageIO将图像写入URL时遇到困难,寻求解决方案。最佳答案建议在写入图像后调用flush()和close(),并确保设置正确的Content-Type。文章还提到了Inm小程序商店和Vultr中文网的相关信息。
摘要由CSDN通过智能技术生成

我正在尝试通过HttpURLConnection写入图像.

 

我知道如何写文字,但尝试时遇到了实际问题
写图像

我已经使用ImageIO成功写入本地HD:

但是我试图通过ImageIO在URL上写Image并失败

 

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.”中给出的文件名上传图像.

现在,当我发送此邮件时,则在ASP页面中找到请求并找到文件名.但找不到要上传的文件.

问题在于,当ImageIO在URL上进行写入时,ImageIO正在写入的文件的名称将是什么,

因此,请帮助我ImageIO如何在URLConnection上写入图像,以及如何知道必须在asp页中使用的文件名称才能上传文件

感谢您抽出宝贵时间阅读这篇文章
迪利普·阿加瓦尔

最佳答案

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

 

第二内容类型对我来说似乎很奇怪.您似乎在尝试提交实际上是图像的表单.我不知道您的ASP期望什么,但是通常,当我编写应通过HTTP传输文件的代码时,我会发送适当的内容类型,例如图片/ jpeg.

例如,这是我从一个小实用程序中提取的代码片段,该小实用程序是我编写的并且在当前工作中正在使用:

 

    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()方法.这是供参考的代码:

 

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主体读取图像内容.
我希望这有帮助.


Inm小程序商店

 

Inm小程序商店收录了最新,最热门的微信小程序和微信小游戏,是国内内容最丰富的集小程序游戏、小程序分发、小程序推广为一体的综合性小程序门户网站之一。


Vultr中文网

 

最低 $2.5/月 的VPS, 稳定, 可靠

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值