2012-07-10 59 views
1

我試圖做一些相對簡單的事情。我需要使用正文中的文件進行簡單的PUT請求,以便將文件上傳到不在我控制範圍內的服務器。這裏是我到目前爲止的代碼:使用HttpURLConnection發送包含PUT請求的文件

connection = ((HttpURLConnection)new URL(ticket.getEndpoint()).openConnection()); 
connection.setRequestMethod("PUT"); 
connection.setRequestProperty("Content-Type", "video/mp4"); 
connection.setRequestProperty("Content-Length", String.valueOf(getStreamFile().length())); 
connection.setUseCaches(false); 
connection.setDoOutput(true); 

connection.connect(); 

outputStream = connection.getOutputStream(); 

streamFileInputStream = new FileInputStream(getStreamFile()); 
streamFileBufferedInputStream = new BufferedInputStream(streamFileInputStream); 

byte[] streamFileBytes = new byte[getBufferLength()]; 
int bytesRead = 0; 
int totalBytesRead = 0; 

while ((bytesRead = streamFileBufferedInputStream.read(streamFileBytes)) > 0) { 
    outputStream.write(streamFileBytes, 0, bytesRead); 
    outputStream.flush(); 

    totalBytesRead += bytesRead; 

    notifyListenersOnProgress((double)totalBytesRead/(double)getStreamFile().length()); 
} 

outputStream.close(); 

logger.debug("Wrote {} bytes of {}, ratio: {}", 
     new Object[]{totalBytesRead, getStreamFile().length(), 
      (double)totalBytesRead/(double)getStreamFile().length()}); 

我看我的網絡經理和我沒有任何文件的大小接近被髮送。事實上,我不知道是否有任何東西發送,但我看不到任何錯誤。

我需要能夠發送此請求並同時測量上傳的狀態,以便能夠通知我的聽衆上傳進度。我如何修改我現有的示例以僅工作™?

回答

2

嘗試設置content-type參數爲multipart/form-dataW3C forms

+0

我將視頻上傳到Vimeo及其[官方上傳指南](https://developer.vimeo.com/apis/advanced/upload#streaming-step3)告訴我以這種方式提出請求。 – 2012-07-10 21:32:08

相關問題