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