I Know there are several post on sharepoint.stackexchange/stack overflow for this. But I didn't get from any of the post how to achieve this. I have access of share point (I mean share point credentials are same as my windows credential )

So, without using any Tool if I am hitting this URLfrom browser


I am able to download the file.
But How I can achieve it using Java Restful API call (Not SOAP call)?
Will plain Java will be enough or we need any Third Party (not commercial Jar) to connect to shaare point from Java ?
How we can upload a local file using Java to share point?

3 Answers 3


For connecting to share point using rest Api 1st we need to request client id, client secret for a share point library.
Step 1 - once you got client id and secret follow steps mentioned at - https://www.ktskumar.com/2017/01/access-sharepoint-online-using-postman/ to generate access token. using access token we can connect to share point using Rest Api or Java.
Download a file -

url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files('file name')/$value
method: GET
    Authorization: "Bearer " + accessToken // what we got in step 1

Upload File : -
if your share point don't have any approval work flow then :-

url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files/add(url='a.txt',overwrite=true)
method: POST
body: "Contents of file"
    Authorization: "Bearer " + accessToken
    content-length:length of post body

If your share point library have approval work flow then we have to follow these steps: -
Step a) - upload file using Post request as I mentioned above
Step b) - check in last uploaded file using post request

url: http://site url/_api/web/GetFileByServerRelativeUrl('/Folder Name/file name')/CheckIn(comment='Comment',checkintype=0)
method: POST
    Authorization: "Bearer " + accessToken

Step c)- update approval status of recent uploaded file follow this answer or post - https://sharepoint.stackexchange.com/a/253137/79002
Best blog to read :- https://blogs.sap.com/2018/02/01/consume-sharepoint-rest-to-create-a-file-using-java-sap-pi-udf/


This Code is working perfectly to upload file up to 249MB using SP online. You need to add "httpclient-4.3.6" this jar in your build path.

package test;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.DefaultHttpClient;

public class fileUpload {
private static void executeRequest(HttpPost httpPost) {
    try {
        HttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(httpPost);
        System.out.println("Response Code:  " + response.getStatusLine().getStatusCode());
    }  catch (UnsupportedEncodingException e) {
    } catch (ClientProtocolException e) {
    } catch (IllegalStateException e) {
    } catch (IOException e) {

public void executeMultiPartRequest(String urlString, File file) throws IOException {
    HttpPost postRequest = new HttpPost(urlString);
    postRequest = addHeader(postRequest, "Access Token");
    try {
        postRequest.setEntity(new FileEntity(file));
    } catch (Exception ex) {

private static HttpPost addHeader(HttpPost httpPost, String accessToken) {
    httpPost.addHeader("Accept", "application/json;odata=verbose");
    httpPost.setHeader("Authorization", "Bearer " + accessToken);
    return httpPost;

public static void main(String args[]) throws IOException {
    fileUpload fileUpload = new fileUpload();
    File file = new File("C:\\users\\bgulati\\Desktop\\test.docx");
            "Here Goes the URL", file);


I do not how to do it but these links below will definitely lead you to your answer.

How to call the SharePoint rest API using Java web application?

Connect to Sharepoint Online REST API using Java

  • 1
    I have already gone through both links but no Luck :( Oct 14, 2018 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.