2013-02-20 99 views
1

我想從blobstore上傳文件到名爲Wisita的服務。 下面是上傳AP​​I文檔:GAE用blobstore用SSL發送多部分/表單數據文件請求

http://wistia.com/doc/upload-api

這裏是我到目前爲止的代碼(我想我可能缺少SSL選項)

FileService fileService = FileServiceFactory.getFileService(); 
AppEngineFile binaryFile = fileService.getBlobFile(new BlobKey("my blob key")); 

String param = "api_password=" + URLEncoder.encode("my wisita key", "UTF-8") + 
       "&project_id=" + URLEncoder.encode("folder_id", "UTF-8"); 
String charset = "UTF-8"; 

String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value. 
String CRLF = "\r\n"; // Line separator required by multipart/form-data. 

URLConnection connection = new URL("https://upload.wistia.com/").openConnection(); 
connection.setDoOutput(true); 
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); 
PrintWriter writer = null; 
try { 
    OutputStream output = connection.getOutputStream(); 
    writer = new PrintWriter(new OutputStreamWriter(output, charset), true); // true = autoFlush, important! 

    // Send normal param. 
    writer.append("--" + boundary).append(CRLF); 
    writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF); 
    writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); 
    writer.append(CRLF); 
    writer.append(param).append(CRLF).flush(); 

    // Send binary file. 
    writer.append("--" + boundary).append(CRLF); 
    writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getNamePart() + "\"").append(CRLF); 
    writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getNamePart())).append(CRLF); 
    writer.append("Content-Transfer-Encoding: binary").append(CRLF); 
    writer.append(CRLF).flush(); 
    InputStream input = null; 
    try { 
      FileReadChannel ch = fileService.openReadChannel(binaryFile, true); 
      input = Channels.newInputStream(ch); 
      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. 

    // End of multipart/form-data. 
    writer.append("--" + boundary + "--").append(CRLF); 

    //resp part 
    InputStream responseStream = new BufferedInputStream(connection.getInputStream()); 

    BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream)); 
    String line = ""; 
    StringBuilder stringBuilder = new StringBuilder(); 
    while ((line = responseStreamReader.readLine()) != null) 
    { 
     stringBuilder.append(line).append("\n"); 
    } 
    responseStreamReader.close(); 
    String response = stringBuilder.toString(); 
    resp.getWriter().write(response); 

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

我得到了一個500錯誤與此代碼

回答

1

謝謝你的問題。作爲參考,它解決了我正在做類似任務的問題。

我對我的代碼所做的第一項更改是將doOutput和doInput都設置爲true(而不僅僅是doOutput)。我不確定這是否有所作爲,因爲我相信真是默認。

con.setDoOutput(true); 
con.setDoInput(true); 

您確定自己的內容編碼正確嗎?我用同樣的設置和配置你有,但不是使用一個PrintWriter,我只是叫寫,並通過它從我的文件內容從我的字符串和字節字節:

connection.getOutputStream().write("Content-Disposition blah...".getBytes()); 
connection.getOutputStream().write(fileContentsByteArray); 
connection.flush(); 

我然後只讀取數據我從我的網絡服務需要通過從連接的輸入流讀取像這樣:

FileOutputStream out = new FileOutputStream("received.bin"); 
InputStream in = connection.getInputStream(); 
byte[] buffer = new byte[1024]; 
int len; 
while ((len = in.read(buffer)) != -1) { 
    out.write(buffer, 0, len); 
} 
out.close(); 
0

如何更改傳遞正常參數?

前:

param=..api_password...project_id...; 
// Send normal param. 
writer.append("--" + boundary).append(CRLF); 
writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF); 
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); 
writer.append(CRLF); 
writer.append(param).append(CRLF).flush(); 

後:

// Send normal param. 
writer.append("--" + boundary).append(CRLF); 
writer.append("Content-Disposition: form-data; name=\"api_password\"").append(CRLF); 
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); 
writer.append(CRLF); 
writer.append(api_password).append(CRLF).flush(); 
// Send normal param. 
writer.append("--" + boundary).append(CRLF); 
writer.append("Content-Disposition: form-data; name=\"project_id\"").append(CRLF); 
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); 
writer.append(CRLF); 
writer.append(project_id).append(CRLF).flush(); 
相關問題