3

我想通过HttpURLConnection写图像。通过URLConnection写图像

我知道怎么写的文字,但我有试图 写的图像

我已经成功地写入本地HD使用ImageIO的实际问题:

但我想通过写图片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页:部分

现在,当我发送这个然后asp页面找到请求并找到文件的名称。但没有找到要上传的文件。

的问题是,ImageIO的URL上书写时会出现什么文件的名称在其上的ImageIO是写作,

所以,请帮助我的ImageIO如何写上URLConnection的图像,我怎么能知道我有在ASP页面中使用上传文件

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

回答

4

首先,我认为,你应该叫io.flush()的文件名,然后写入后io.close()图片。

第二种内容类型对我来说似乎很奇怪。看起来你试图提交表单,而实际上是图像。我不知道你的期望是什么,但通常当我编写应通过HTTP传输文件的代码时,我会发送适当的内容类型,例如image/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); 
    } 

我这里使用的方法复制(),我从雅加达IO utils的了。下面是引用代码:

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体内的图像内容。 我希望这可以帮助。

+0

实际上我在客户端使用小程序,所以我无法访问客户端的文件系统。我必须在ImageIO的帮助下创建小程序显示的图表的图像(如它在写入文件系统期间工作)。所以我必须直接从ImageIO上写URLConnection。这就是为什么我问ImageIO如何将图像内容写入URLConnection中的图像文件以及如何设置该图像文件的名称或知道该图像文件的名称 – 2011-02-03 08:24:02

0

的OP似乎失去被遗忘但对于风筝先生的利益:

// main method 
URL url = new URL(uploadURL); 
connection = (HttpURLConnection) url.openConnection(); 
connection.setDoOutput(true); // triggers "POST" 
// connection.setDoInput(true); // only if needed 
connection.setUseCaches(false); // dunno 
final String boundary = Long.toHexString(System.currentTimeMillis()); 
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" 
                   + boundary); 
output = new DataOutputStream(connection.getOutputStream()); 
try { 
    // image must be a File instance 
    flushMultiPartData(image, output, boundary); 
} catch (IOException e) { 
    System.out.println("IOException in flushMultiPartData : " + e); 
    return; 
} 
// ... 
private void flushMultiPartData(File file, OutputStream serverOutputStream, 
      String boundary) throws FileNotFoundException, IOException { 
    // SEE https://stackoverflow.com/a/2793153/281545 
    PrintWriter writer = null; 
    try { 
     // true = autoFlush, important! 
     writer = new PrintWriter(new OutputStreamWriter(serverOutputStream, 
       charsetForMultipartHeaders), true); 
     appendBinary(file, boundary, writer, serverOutputStream); 
     // End of multipart/form-data. 
     writer.append("--" + boundary + "--").append(CRLF); 
    } finally { 
     if (writer != null) writer.close(); 
    } 
} 

private void appendBinary(File file, String boundary, PrintWriter writer, 
     OutputStream output) throws FileNotFoundException, IOException { 
    // Send binary file. 
    writer.append("--" + boundary).append(CRLF); 
    writer.append(
     "Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" 
      + file.getName() + "\"").append(CRLF); 
    writer.append("Content-Type: " 
      + URLConnection.guessContentTypeFromName(file.getName())) 
      .append(CRLF); 
    writer.append("Content-Transfer-Encoding: binary").append(CRLF); 
    writer.append(CRLF).flush(); 
    InputStream input = null; 
    try { 
     input = new FileInputStream(file); 
     byte[] buffer = new byte[1024]; 
     for (int length = 0; (length = input.read(buffer)) > 0;) { 
      output.write(buffer, 0, length); 
     } 
     output.flush(); // Important! Output cannot be closed. Close of 
     // writer will close output as well. 
    } finally { 
     if (input != null) try { 
      input.close(); 
     } catch (IOException logOrIgnore) {} 
    } 
    writer.append(CRLF).flush(); // CRLF is important! It indicates end of 
    // binary boundary. 
} 

您可能需要添加Gzip压缩 - 见file corrupted when I post it to the servlet using GZIPOutputStream有或没​​有Gzip已工人阶级。 ImageIO有这里没有地方 - 只需写出通过线路的字节并将ImageIO用于服务器上的内容。基于@BalusC answer