2016-04-06 9 views
0

Sadece Android Cihazımdan bir Java geri yüklememe ve bazı StackOverflow iş parçacıklarını okuduktan sonra bir video yükleyebilmem gerektiğini öğrendim. Bu videoyu benim POST'umun Java Backend'e Multipart olarak gönderilmesini istediğimi öğrendim.Android: Videoyu Java Backend'e POST isteği olarak mı yüklüyor?

Temel olarak, video dosyasını bir Çoklu Metin POST isteği olarak POST'lar olan aşağıdakileri gerçekleştirmeyi başardım.

Android Müşteri:

private void uploadVideo(String videoPath) throws ParseException, IOException { 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("MY_SERVER_URL"); 

     FileBody filebodyVideo = new FileBody(new File(videoPath)); 
     StringBody title = new StringBody("Filename: " + videoPath); 
     StringBody description = new StringBody("This is a description of the video"); 

     MultipartEntity reqEntity = new MultipartEntity(); 
     reqEntity.addPart("video", filebodyVideo); 
     reqEntity.addPart("title", title); 
     reqEntity.addPart("description", description); 
     httppost.setEntity(reqEntity); 

     // DEBUG 
     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(); 
} 

, nasıl Java Arka Ucu gelen dosya alırım edilir sorum? Değiştirmem gereken Backend yöntemi. Birisi video dosyasını arka uçtan nasıl alabileceğime işaret edebilir mi? Şu anda ne var

: I (hata işleme, doğrulama ve malzeme olmadan) bunu nasıl yaptı İşte

@Path("/user") 
public class UserAPI { 

    @POST 
    //To receive the file, What do I add below instead of the lines I've commented. 
    //@Produces(MediaType.APPLICATION_JSON) 
    //@Consumes(MediaType.APPLICATION_FORM_URLENCODED) 
    @Path("/postvideo") 
    public VideoResponse PostVideo(){ 
     //My code 
    } 
} 

cevap

1

olduğunu.

@POST 
@Path("/") 
@Consumes("multipart/form-data") 
public Response uploadFileMultipart(MultipartFormDataInput input) { 
     Map<String, List<InputPart>> uploadForm = input.getFormDataMap(); 

     List<InputPart> inputParts = uploadForm.get("video"); 

     String videoFileName = "GENERATE_YOUR_FILENAME_HERE.mp4"; 
     File file = new File(filename); 

     if (!file.exists()) { 
      file.createNewFile(); 
     } 

     FileOutputStream fop = new FileOutputStream(file); 
     for (InputPart inputPart : inputParts) { 
       InputStream inputStream = inputPart.getBody(InputStream.class, null); 
       byte[] content = IOUtils.toByteArray(inputStream); 
     fop.write(content); 
     } 

     fop.flush(); 
     fop.close(); 

     return Response.status(HttpStatus.SC_OK).build(); 
} 
+0

Böylece, içerik türü 'çok parçalı/form-veri' olur mu? Bu doğru mu? – Dinuka

+0

Bazı içe aktarma sorunlarım var. 'InputPart' ve 'MultipartFormDataInput' için bir ithalat bulmuyorum. Bir JAR eklesin mi? – Dinuka

+0

Hangi çerçeveyi kullanıyorsunuz? Belki de bunu sorgunuza ekleyebilirsin, cevabımdaki kod RESTEasy –