503在“完成上传”方法中在线在SharePoint上上传文件时服务不可用

时间:2019-07-03 08:09:03

标签: java rest sharepoint sharepoint-2013 chunks

我正在尝试在大于250MB的SharePoint上上传文件。我已将文件数据分成几小段,例如100MB,并使用“开始上载”,“继续上载”和“完成SharePoint上载”。如果我的文件大小为250MB或更大,我将无法完成上传方法获得503服务。但是,以下代码成功运行,文件大小最大为249MB。任何线索或帮助都将不胜感激。

谢谢

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++;
                }
            }
        }

    }
}

0 个答案:

没有答案