2013-07-15 5 views
0

Jersey 2.0이 포함 된 Google 드라이브 용 REST 클라이언트를 구현하려고합니다.Google 드라이브 API에서 파일 업로드 실패

다음 문서에 따르면 다중 요청으로 파일을 보내는 코드를 만들었습니다.

https://developers.google.com/drive/v2/reference/files/insert https://developers.google.com/drive/manage-uploads

String targetUrl = "https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&access_token=" + token.access_token; 
    WebTarget target = client.target(targetUrl); 
    final FileDataBodyPart filePart = new FileDataBodyPart("file", file); 
    final MultiPart multipart = new FormDataMultiPart().field("title", file.getName()) 
     .field("description", file.getName()) 
     .bodyPart(filePart); 
    Response response = target.request(MediaType.APPLICATION_JSON_TYPE) 
      .post(Entity.entity(multipart, multipart.getMediaType())); 
    System.out.println("response:" + response.readEntity(String.class)); 

다음은 코드에 의해 POST 요청입니다.

POST /upload/drive/v2/files?uploadType=multipart&access_token={ACCESS_TOKEN} HTTP/1.1\r\n 
Accept: application/json\r\n 
Content-Type: multipart/form-data; boundary=Boundary_1_1833261898_1373877178038\r\n 
User-Agent: Jersey/2.0 (HttpUrlConnection 1.7.0_25)\r\n 
MIME-Version: 1.0\r\n 
Host: www.googleapis.com\r\n 
Content-Length: 42430\r\n 

MIME Multipart Media Encapsulation, Type: multipart/form-data, Boundary: "Boundary_1_1833261898_1373877178038" 
Type: multipart/form-data 
First boundary: --Boundary_1_1833261898_1373877178038\r\n 
Encapsulated multipart part: (text/plain) 
    Content-Type: text/plain\r\n 
    Content-Disposition: form-data; name="title"\r\n\r\n 
    Line-based text data: text/plain 
Boundary: \r\n--Boundary_1_1833261898_1373877178038\r\n 
Encapsulated multipart part: (text/plain) 
    Content-Type: text/plain\r\n 
    Content-Disposition: form-data; name="description"\r\n\r\n 
    Line-based text data: text/plain 
Boundary: \r\n--Boundary_1_1833261898_1373877178038\r\n 
Encapsulated multipart part: (text/plain) 
    Content-Type: text/plain\r\n 
    Content-Disposition: form-data; filename="GoogleDrive.txt"; modification-date="Fri, 12 Jul 2013 08:15:47 GMT"; size=41918; name="file"\r\n\r\n 
    Line-based text data: text/plain 
Last boundary: \r\n--Boundary_1_1833261898_1373877178038--\r\n 

내 POST 요청을 보냈지 만 다음과 같은 오류가 발생했다.

{ 
"error": { 
    "errors": [ 
    { 
    "domain": "global", 
    "reason": "parseError", 
    "message": "Parse Error" 
    } 
    ], 
    "code": 400, 
    "message": "Parse Error" 
} 
} 

이 오류를 방지하는 방법을 알려주십시오.

답변

0

업로드 요청에는 메타 데이터 및 파일 콘텐츠와 메타 데이터 부분이 JSON에 있어야합니다.

경우에 따라 각 속성에 대해 양식 인코딩 된 부분을 새로 만듭니다. 적절한 서식을보기 위해 문서에 대한 참조를 사용 https://developers.google.com/drive/manage-uploads#multipart

final MultiPart multipart = new FormDataMultiPart().field("title", file.getName()) 
    .field("description", file.getName()) 
    .bodyPart(filePart); 

귀하의 다중 몸은 유효한 JSON해야한다.

+0

답변 해 주셔서 감사합니다. 도간. 코드를 수정하고 코드가 올바르게 작동하고 Google 드라이브에 파일을 보낼 수 있는지 확인했습니다. 대단히 감사합니다. – moomindani

관련 문제