使用HttpClient上传视频文件

时间:2010-10-11 22:35:05

标签: java file-upload httpclient pandastream

我正在构建Java客户端,它将自动将文件从Java服务器上传到我使用他们的AMI安装在我的EC2云上的Panda Instance。我正在尝试使用Apache HTTP Components上传到Panda服务器(Panda Stream)。它适用于我的浏览器客户端但由于某种原因我无法从该库上传。这是我的代码:

String videoUploadUrl = "http://[panda server ip]/videos/" + getVideoID() + "/upload.xml";
File file = new File("/temp/videofile.mp4");

DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

HttpPost httppost = new HttpPost(videoUploadUrl);

MultipartEntity mpEntity = new MultipartEntity();

ContentBody fBody = new FileBody(file);

mpEntity.addPart("userfile", fBody);

httppost.setEntity(mpEntity);


System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response2 = httpclient.execute(httppost); // <-- FAILS HERE
System.out.println("finish executing request");

HttpEntity resEntity = response2.getEntity();

System.out.println(response2.getStatusLine());
if (resEntity != null) {
    System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
    resEntity.consumeContent();
}

httpclient.getConnectionManager().shutdown();

这是我的错误日志。

executing request POST http://[panda server ip]/videos/0fbd7300-b7b2-012d-7c3f9-1223456a654/upload.xml HTTP/1.1 Method: POST
Oct 11, 2010 3:09:25 PM org.apache.http.impl.client.DefaultRequestDirector tryExecute
INFO: I/O exception (java.net.SocketException) caught when processing request: Broken pipe
Oct 11, 2010 3:09:25 PM org.apache.http.impl.client.DefaultRequestDirector tryExecute
INFO: Retrying request
Oct 11, 2010 3:09:55 PM org.apache.http.impl.client.DefaultRequestDirector tryExecute
INFO: I/O exception (java.net.SocketException) caught when processing request: Broken pipe
Oct 11, 2010 3:09:55 PM org.apache.http.impl.client.DefaultRequestDirector tryExecute
INFO: Retrying request
Exception in thread "main" java.net.SocketException: Broken pipe
        at java.net.SocketOutputStream.socketWrite0(Native Method)
        at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
        at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
        at org.apache.http.impl.io.AbstractSessionOutputBuffer.write(AbstractSessionOutputBuffer.java:124)
        at org.apache.http.impl.io.ContentLengthOutputStream.write(ContentLengthOutputStream.java:114)
        at org.apache.http.entity.mime.content.FileBody.writeTo(FileBody.java:105)
        at org.apache.http.entity.mime.HttpMultipart.doWriteTo(HttpMultipart.java:173)
        at org.apache.http.entity.mime.HttpMultipart.writeTo(HttpMultipart.java:191)
        at org.apache.http.entity.mime.MultipartEntity.writeTo(MultipartEntity.java:169)
        at org.apache.http.impl.entity.EntitySerializer.serialize(EntitySerializer.java:120)
        at org.apache.http.impl.AbstractHttpClientConnection.sendRequestEntity(AbstractHttpClientConnection.java:253)
        at org.apache.http.impl.conn.AbstractClientConnAdapter.sendRequestEntity(AbstractClientConnAdapter.java:227)
        at org.apache.http.protocol.HttpRequestExecutor.doSendRequest(HttpRequestExecutor.java:248)
        at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
        at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:623)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:455)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:694)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:625)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:603)
        at testmainjava.FileUpload.main(FileUpload.java:57)

我错过了什么?为什么我的管子坏了。如果我使用相同的代码上传到某个servlet,那么我工作正常,但不适用于panda Instance。 有什么想法吗?

3 个答案:

答案 0 :(得分:4)

我在尝试寻找从Java上传到pandastream的解决方案时看了这个问题。找不到具有此功能的库,所以更新了panda-lib来做到这一点。

https://github.com/maxhille/panda-lib

答案 1 :(得分:1)

为了简化您的故障排除并了解​​正在发生的事情:

  • 启用 HttpClient线级logging
  • 使用LiveHttpHeaders或类似的插件找出浏览器如何处理此
  • 使用WireShark或类似工具检查网络级流量

然后,比较浏览器和HttpClient之间的差异

我注意到的一件事:在您的代码中,您将视频发布为“userfile”参数,但PandaStream API documentation表示您应该使用“file”参数。但不确定它是否会有所作为。

即使参数名称错误,PandaStream仍应返回正确的响应,而不仅仅是在出错时关闭连接。

最后,您是否配置PandaStream向您发送错误消息?如果没有,这可能会有所帮助,如果它遇到一些错误(请参阅getting started guide)。

答案 2 :(得分:0)

嘿,我是来自pandastream的首席技术官Vivien。 我开始创建Java客户端,但我没有时间完成它。 如果你想分叉,那就免费。

https://github.com/vivienschilis/panda_client_java