Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3663

503 Service unavailable while uploading file on SharePoint online in Finish Upload method

$
0
0

I am trying to upload the file on SharePoint larger than 250MB. I have divided data of the file in small chunks say 100MB and used Start upload, continue Upload and Finish Upload of SharePoint. I am getting 503 service unavailable in finish upload method if my file size is 250MB or greater. However, The below code runs successfully for the file size up to 249MB. Any leads or help is much appreciated.

Thanks,

Bharti Gulati

package test;import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.ParseException;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.ByteArrayEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import org.json.JSONObject;public class SampleFileUpload1 {    private final static int chunkSize = 100 * 1024 * 1024;    public static void main(String args[]) throws IOException {        SampleFileUpload1 fileUpload = new SampleFileUpload1();        File file = new File("C:\\users\\bgulati\\Desktop\\twofivefive.txt");        fileUpload.genereateAndUploadChunks(file);    }    private static void executeRequest(HttpPost httpPost, String urlString) {        try {            HttpClient client = new DefaultHttpClient();            HttpResponse response = client.execute(httpPost);            System.out.println("Response Code:  "+ response.getStatusLine().getStatusCode());            System.out.println("Response getReasonPhrase:  "+ response.getStatusLine().getReasonPhrase());            System.out.println("Response getReasonPhrase:  "+ response.getEntity().getContent().toString());            BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));            while (true) {                String s = br.readLine();                if (s == null)                    break;                System.out.println(s);            }        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        } catch (ClientProtocolException e) {            e.printStackTrace();        } catch (IllegalStateException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }    }    public static void executeMultiPartRequest(String urlString, byte[] fileByteArray) throws IOException {        HttpPost postRequest = new HttpPost(urlString);        postRequest = addHeader(postRequest, "accessToken");        try {            postRequest.setEntity(new ByteArrayEntity(fileByteArray));        } catch (Exception ex) {            ex.printStackTrace();        }        executeRequest(postRequest, urlString);    }    private static HttpPost addHeader(HttpPost httpPost, String accessToken) {        httpPost.addHeader("Accept", "application/json;odata=verbose");        httpPost.setHeader("Authorization", "Bearer "+ accessToken);        httpPost.setHeader("Content-Type", "application/json;odata=verbose;charset=utf-8");        return httpPost;    }    private static String getUniqueId(HttpResponse response, String key) throws ParseException, IOException {        if (checkResponse(response)) {            String responseString = EntityUtils.toString(response.getEntity(), "UTF-8");            JSONObject json = new JSONObject(responseString);            return json.getJSONObject("d").getString(key);        }        return "";    }    private static boolean checkResponse(HttpResponse response) throws ParseException, IOException {        if (response.getStatusLine().getStatusCode() == 200 || (response.getStatusLine().getStatusCode() == 201)) {            return true;        }        return false;    }    private String createDummyFile(String relativePath, String fileName) throws ClientProtocolException, IOException {        String urlString = "https://siteURL/_api/web/GetFolderByServerRelativeUrl('"+relativePath+"')/Files/add(url='"+fileName+"',overwrite=true)";        HttpPost postRequest = new HttpPost(urlString);        postRequest = addHeader(postRequest, "access_token");        HttpClient client = new DefaultHttpClient();        HttpResponse response = client.execute(postRequest);        return getUniqueId(response, "UniqueId");    }    private void genereateAndUploadChunks(File file) throws IOException {        String relativePath = "/relativePath";        String fileName = file.getName();        String gUid = createDummyFile(relativePath, fileName);        String endpointUrlS = "https://siteURL/_api/web/GetFileByServerRelativeUrl('"+relativePath+"/"+fileName+"')/savebinarystream";        HttpPost postRequest = new HttpPost(endpointUrlS);        postRequest = addHeader(postRequest, "access_token");        HttpClient client = new DefaultHttpClient();        HttpResponse response = client.execute(postRequest);        long fileSize = file.length();        if (fileSize <= chunkSize) {        }         else {            byte[] buffer = new byte[(int) fileSize <= chunkSize ? (int) fileSize : chunkSize];            long count = 0;            if (fileSize % chunkSize == 0)                count = fileSize / chunkSize;            else                count = (fileSize / chunkSize) + 1;            // try-with-resources to ensure closing stream            try (FileInputStream fis = new FileInputStream(file);                BufferedInputStream bis = new BufferedInputStream(fis)) {                int bytesAmount = 0;                ByteArrayOutputStream baos = new ByteArrayOutputStream();                int i = 0;                String startUploadUrl = "";                int k = 0;                while ((bytesAmount = bis.read(buffer)) > 0) {                    baos.write(buffer, 0, bytesAmount);                    byte partialData[] = baos.toByteArray();                    if (i == 0) {                        startUploadUrl = "https://siteURL/_api/web/GetFileByServerRelativeUrl('"+relativePath+"/"+fileName+"')/StartUpload(uploadId=guid'"+gUid+"')";                                               executeMultiPartRequest(startUploadUrl, partialData);                        System.out.println("first worked");                        // StartUpload call                    } else if (i == count-1) {                        String finishUploadUrl = "https://siteURL/_api/web/GetFileByServerRelativeUrl('"+relativePath+"/"+fileName+"')/FinishUpload(uploadId=guid'"+gUid+"',fileOffset="+i+")";                        executeMultiPartRequest(finishUploadUrl, partialData);                        System.out.println("FinishUpload worked");                        // FinishUpload call                    } else {                        String continueUploadUrl = "https://siteURL/_api/web/GetFileByServerRelativeUrl('"+relativePath+"/"+fileName+"')/ContinueUpload(uploadId=guid'"+gUid+"',fileOffset="+i+")";                        executeMultiPartRequest(continueUploadUrl, partialData);                        System.out.println("continue worked");                    }                    i++;                }            }        }    }}

Viewing all articles
Browse latest Browse all 3663

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>