2013-01-17 137 views
0

所以,我不能擺脫這個問題:我需要上傳一個XML文件和.jpg文件從我的Android應用程序(API 8)到HTTP服務器(勝利2008服務器與IIS 7.5)。我已經啓用PUT動詞,並根據以前的搜索建議卸載WebDav & webdav方法。 另外,我不知道我做的是正確的服務器端,因爲我無法得到任何迴應。如何使用HTTPUrlConnection urlConnection.setRequestMethod(「PUT」)上傳文件;或HttpPut?

這裏是我的代碼

 URL fileurl = new URL("Server Upload Path"); 

     HttpURLConnection urlConnection = (HttpURLConnection) fileurl 
       .openConnection(); 
     urlConnection.setRequestMethod("PUT"); 
     urlConnection.setDoOutput(true); 
     urlConnection.connect(); 

     OutputStream os = urlConnection.getOutputStream(); 

     File upFile = new File("My Local File"); 
        //I'm sure the file exists 
     FileInputStream fis = new FileInputStream(upFile); 
     BufferedInputStream bfis = new BufferedInputStream(fis); 
     byte[] buffer = new byte[1024]; 
     int bufferLength = 0; 

     // now, read through the input buffer and write the contents to the 
     // file 
     while ((bufferLength = bfis.read(buffer)) > 0) { 
      os.write(buffer, 0, bufferLength); 

     } 

很抱歉,如果我忘了一些信息,您可能需要幫助。我對android和IIS也是新手。

+0

首先測試你的服務器端,並確保它的工作,甚至不擔心客戶端直到你可以驗證。使用命令行中的curl或hurl.it:http://www.hurl.it/等。 –

回答

-1

爲什麼不嘗試一個標準的多部分文件上傳請求(基於POST而不是PUT):

 final static String MULTIPART_BOUNDARY = "------------------563i2ndDfv2rTHiSsdfsdbouNdArYfORhxcvxcvefj3q2f"; 

public static void sendFileToServer(String url, File logFiles) { 
    HttpURLConnection connection = null; 
    OutputStream os = null; 
    DataInputStream is = null; 
try { 
    StringBuilder fullUrl = new StringBuilder(url); 
    connection = (HttpURLConnection) new URL(url).openConnection(); 
    connection.setRequestMethod("POST"); 
    connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + MULTIPART_BOUNDARY); 
    connection.setDoOutput(true); 
    connection.setDoInput(true); 
    connection.setRequestProperty("Connection", "Keep-Alive"); 
    connection.connect(); 
    os = new BufferedOutputStream(connection.getOutputStream()); 
    if(os != null) { 
      os.write(("--" + MULTIPART_BOUNDARY + EOL).getBytes()); 
      os.write(String.format("Content-Disposition:form-data;name=\"UploadedFile\";filename=\"%s\"\r\nContent-Type: application/x-zip-compressed\r\n\r\n", UPLOADED_FILE_NAME).getBytes()); 

      // Upload file(s) data here and send 

      os.write((EOL + "--" + MULTIPART_BOUNDARY + "--" + EOL + EOL).getBytes()); 
      os.flush(); 
      os.close(); 
      os = null; 
     } 
     if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) { 
       // Process server response 
     } 
    } catch (MalformedURLException e) { 


    } catch (IOException e) { 


    } catch (Exception e1) { 

    } finally { 
    try { 
     if(os != null) { 
      os.close(); 
     } 
    } catch (IOException e) { 
     Log.e(TAG, "sendFileToServer exception: close OutputStream", e); 
    } 
    try { 
     if(is != null) { 
      is.close(); 
     } 
    } catch (IOException e) { 
     Log.e(TAG, "sendFileToServer exception: close InputStream", e); 
    } 
    if(connection != null) { 
     connection.disconnect(); 
    } 
    } 
} 
+0

我需要一些asp或php服務器端嗎? – JJonDuty

+0

服務器需要支持標準文件上傳RFC。我認爲大多數服務器都支持這種開箱即用的方式。如果你需要一些特殊的處理,你可能需要一個服務器端頁面(php或asp或servlet - 你的選擇)。在上面的示例中,我發送了一個壓縮文件內容,所以我有一個服務器端頁面來解壓縮它。 –

+0

我有一個叫做htm頁面的asp。有了這個解決方案,服務器返回了411長度所需的錯誤。有沒有辦法設置它? – JJonDuty