2012-04-24 36 views
-3

我試圖上傳圖像到圖像託管網站(fastpic.ru),但我無法得到正確的響應,如我所料。我用提琴手來檢查我會發送正確的參數,一切似乎都很好,但我無法得到正確的答案。你能指導我如何上傳並以適當的方式得到迴應嗎?如何上傳到圖像託管網站在Java?

正確的反應我的意思是我應該得到的東西像http://fastpic.ru/session/2012/0425/Y6sEtGjtT1.html但我只收到http://fastpic.ru/index.php

謝謝

這是我的代碼

String urlToConnect = "http://fastpic.ru/uploadmulti"; 
    String boundary = Long.toHexString(System.currentTimeMillis()); // Generate random boundary 
    URLConnection connection = new URL(urlToConnect).openConnection(); 
    connection.setDoOutput(true); // This sets request method to POST. 
    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); 
    OutputStream output = null; 
    PrintWriter writer = null; 
    try { 
     output = connection.getOutputStream(); 
     writer = new PrintWriter(new OutputStreamWriter(output, "UTF-8"), true); // true = Autoflush, important! 

     writer.println("-----------------------------" + boundary); 
     writer.println("Content-Disposition: form-data; name=\"file[]\"; filename=\"" + fileToUpload.getName() + "\""); 
     writer.println("Content-Type: image/jpeg"); 
     writer.println(); 
     InputStream input = null; 
     try { 
      input = new FileInputStream(fileToUpload); 
      byte[] buffer = new byte[1024]; 
      for (int length = 0; (length = input.read(buffer)) > 0;) { 
       output.write(buffer, 0, length); 
      } 
      output.flush(); 
     } finally { 
      if (input != null) { 
       try { 
        input.close(); 
       } catch (IOException logOrIgnore) { 
       } 
      } 
     } 
     writer.println(); 

     writer.println("-----------------------------" + boundary); 
     writer.println("Content-Disposition: form-data; name=\"submit\""); 
     writer.println(); 
     writer.println("Загрузить"); 

     writer.println("-----------------------------" + boundary); 
     writer.println("Content-Disposition: form-data; name=\"uploading\""); 
     writer.println(); 
     writer.println("1"); 
     writer.println("-----------------------------" + boundary + "--"); 

    } finally { 
     if (writer != null) { 
      writer.close(); 
     } 
    } 

    BufferedReader in = new BufferedReader(
           new InputStreamReader(
           connection.getInputStream())); 
    String decodedString; 
    while ((decodedString = in.readLine()) != null) { 
     System.out.println(decodedString); 
    } 
    in.close(); 
+0

好的我在這裏看到拇指。可能你認爲我之前沒有研究過,但是我在1天內沒有結果嘗試過。所以至少給我什麼文章/職位我應該讀取學習。謝謝 – KingOfDCP 2012-04-24 18:18:57

+1

*「......我無法得到正確的答覆,因爲我預期......」*是非常沒用的,因爲它代表。 – 2012-04-24 18:23:29

+2

您應該更準確地說明根據您的期望不能達到的目標以及您想要的結果,從而改善您的問題。你的問題太含糊,代碼太長。 – MarioDS 2012-04-24 18:23:46

回答

1

如果沒有返回正確的輸出,輸入可能有不正確的地方。

我會推薦使用Apache HTTP組件,如MultipartEntityhttp://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntity.html用於發佈此類數據。如果您試圖手動對數據進行編碼,那麼很容易犯一個簡單的錯誤來阻止整個事情的發生。有很多使用Apache組件的例子,它的使用非常簡單。

+0

我用小提琴來比較我的應用程序和瀏覽器上傳之間的輸入數據,除邊界外兩者都是相同的。問題是我不知道我的代碼中出了什麼問題,所以我希望有些人可以指出什麼是錯的,並指向正確的方向。順便說一句,我會看看Apache的HTTP組件 – KingOfDCP 2012-04-25 11:16:04

相關問題