2014-10-03 4 views
0

https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart을 사용하여 메타 데이터 정보로 파일을 업로드합니다. 아래 코드를 업로드에 사용하고 있습니다.Google 드라이브 멀티 파트 업로드

HttpClient client=new DefaultHttpClient(); 
HttpPost post=new HttpPost("https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart"); 

String accessToken="ya29.kwDKqMocFOkP5jF7gRugZGebVDErW4toL-11i_SPO8wDam3X7GTNCEXL"; 
post.setHeader("Content-Type","multipart/related; boundary=foo_bar_baz"); 
post.addHeader("Authorization","Bearer "+accessToken);String value="--foo_bar_baz 
Content-Type:application/json charset=UTF-8"; 
String hello="ddddddddddddddd"; 
StringEntity str=new StringEntity(value+"\n"+object.toString()+"\n--foo_bar_baz \n Content-Type:plain/text \n"+hello+"\n \n--foo_bar_baz-- ","UTF-8"); 
post.setEntity(str); 
HttpResponse response = client.execute(post); 

그러나 내 응답은 mulipart body입니다. Pleae는이 문제를 해결하도록 도와줍니다.

감사

답변

0
String accessToken="ya29.kwDKqMocFOkP5jF7gRugZGebVDErW4toL-11i_SPO8wDam3X7GTNCEXL"; 
HttpPost httpPost = new HttpPost("https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart"); 
httpPost.addHeader("Authorization", "Bearer "+accessToken); 
httpPost.setHeader("Content-Type","multipart/related; boundary=foo_bar_baz"); 

JSONObject json = new JSONObject(); 
json.put("title", "NewFile.txt"); // {'title': 'My File.txt'} 

String fileContent = "This is in the file"; 

StringEntity str=new StringEntity("--foo_bar_baz\r\nContent-Type: application/json; charset=UTF-8\r\n\r\n\n"+json.toString()+"\r\n--foo_bar_baz\r\nContent-Type: plain/text\r\n\r\n"+fileContent+"\r\n--foo_bar_baz--"); 
httpPost.setEntity(str); 

CloseableHttpResponse response = new DefaultHttpClient().execute(httpPost); 
String jsonResponse = EntityUtils.toString(response.getEntity()); 
response.close(); 
System.out.println("jsonResponse:"+jsonResponse); 
관련 문제