Java: 从 Multipart POST 请求中获取视频文件
Java: Get Video file from Multipart POST request
我正在将 Android 客户端的视频文件作为多部分请求发布到我的服务器。我需要编写一个服务器端方法来接收以下请求。
- 我使用 Jersey 作为服务器端框架
我的代码如下:
private void send_video_to_server(String videoPath) throws ParseException, IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://MY_SERVER_URL/videos/postvideo");
FileBody filebodyVideo = new FileBody(new File(videoPath));
StringBody title = new StringBody(titleBox.getText().toString());
StringBody description = new StringBody(captionBox.getText().toString());
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("videoFile", filebodyVideo);
reqEntity.addPart("title", title);
reqEntity.addPart("description", description);
httppost.setEntity(reqEntity);
// DEBUG
System.out.println( "executing request " + httppost.getRequestLine( ) );
HttpResponse response = httpclient.execute( httppost );
HttpEntity resEntity = response.getEntity( );
// DEBUG
System.out.println( response.getStatusLine( ) );
if (resEntity != null) {
System.out.println( EntityUtils.toString( resEntity ) );
} // end if
if (resEntity != null) {
resEntity.consumeContent( );
} // end if
httpclient.getConnectionManager( ).shutdown( );
}
如何将 SERVER 端代码写入 RECEIVE 上述请求? 方法签名足以回答问题 :)
到底是什么问题?
不知道泽西岛,但步骤是:
1)写一个servlet(http://www.tutorialspoint.com/servlets/servlets-first-example.htm)
2) Servlet 输入参数 HttpServletRequest 包含 getParts() 方法,您可以在其中找到您发布的视频...以及其他部分(如果有的话)
编辑
未经测试,但这对您有帮助吗?你应该可以这样获取视频数据流。
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
Collection parts = req.getParts();
for (Part part : parts) {
//... determine if its a file part from content disposition for example
InputStream is = part.getInputStream();
//...work with your input stream
}
}
详细的例子,看spring是怎么做的:
See spring way
我正在将 Android 客户端的视频文件作为多部分请求发布到我的服务器。我需要编写一个服务器端方法来接收以下请求。
- 我使用 Jersey 作为服务器端框架
我的代码如下:
private void send_video_to_server(String videoPath) throws ParseException, IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://MY_SERVER_URL/videos/postvideo");
FileBody filebodyVideo = new FileBody(new File(videoPath));
StringBody title = new StringBody(titleBox.getText().toString());
StringBody description = new StringBody(captionBox.getText().toString());
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("videoFile", filebodyVideo);
reqEntity.addPart("title", title);
reqEntity.addPart("description", description);
httppost.setEntity(reqEntity);
// DEBUG
System.out.println( "executing request " + httppost.getRequestLine( ) );
HttpResponse response = httpclient.execute( httppost );
HttpEntity resEntity = response.getEntity( );
// DEBUG
System.out.println( response.getStatusLine( ) );
if (resEntity != null) {
System.out.println( EntityUtils.toString( resEntity ) );
} // end if
if (resEntity != null) {
resEntity.consumeContent( );
} // end if
httpclient.getConnectionManager( ).shutdown( );
}
如何将 SERVER 端代码写入 RECEIVE 上述请求? 方法签名足以回答问题 :)
到底是什么问题? 不知道泽西岛,但步骤是:
1)写一个servlet(http://www.tutorialspoint.com/servlets/servlets-first-example.htm)
2) Servlet 输入参数 HttpServletRequest 包含 getParts() 方法,您可以在其中找到您发布的视频...以及其他部分(如果有的话)
编辑
未经测试,但这对您有帮助吗?你应该可以这样获取视频数据流。
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
Collection parts = req.getParts();
for (Part part : parts) {
//... determine if its a file part from content disposition for example
InputStream is = part.getInputStream();
//...work with your input stream
}
}
详细的例子,看spring是怎么做的: See spring way