David
David

Reputation: 45

Java Multipart File upload with JSON

I am developing the Google Drive API using the Restful API.

I need to upload a file and json body.

But when I try to upload with my java code, I was met error code from google-drive.

==> Invalid multipart request with 0 mime parts

Here is the Google-Drive's guide. enter image description here

And Here is my code. What is wrong in my code?

public int uploadFileToGoogleDrive(File file, Long acctId, String 
accessToken, JSONObject json) {
    HttpClient httpClient = new HttpClient();
    PostMethod method = null;
    Integer result = -1;
    String boundary = "---------------------------" + System.currentTimeMillis();
    try {
        method = new PostMethod("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart");
        method.setRequestHeader("Authorization", "Bearer " + accessToken);
        method.setRequestHeader("Content-Type", "multipart/related; boundary=" + boundary );

        Part[] parts = {new StringPart("",json.toString(),"utf-8"), new FilePart(file.getName(), file, null, "utf-8")};

        //MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, method.getParams());
        method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));

        httpClient.getHttpConnectionManager().closeIdleConnections(0);
        result  = httpClient.executeMethod(method);

        if (result == HttpStatus.SC_OK) {
            InputStream rstream = null;
            rstream = method.getResponseBodyAsStream();
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(rstream));
            String line;
            while ((line = br.readLine()) != null) {
                resultString += line;
            }
        }
        System.out.println("##############################################\n" + json.toString() + "\n##############################################");
        logger.debug(resultString);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        logger.error(e.getMessage(), e);
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        logger.error(e.getMessage(), e);
    }
    catch (ProtocolException e) {
        // TODO Auto-generated catch block
        logger.error(e.getMessage(), e);
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        logger.error(e.getMessage(), e);
    }finally {
        method.releaseConnection();
    }
    return result;
}

}

Upvotes: 1

Views: 2080

Answers (1)

user10089632
user10089632

Reputation: 5550

multipart form uploads work just fine. Just don't specify a custom Content-Type. Your content-type is going to fail otherwise because the form-data boundary is determined by the XHR library internally. If you want to use your Content-Type, then you will have to construct your request separately and paste it as text in the "Raw" mode

look at this discussioin and this one

Upvotes: 1

Related Questions