2013-02-20 78 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(); 
相关问题