Java 在“完成上载”方法中在SharePoint online上上载文件时,503服务不可用

Java 在“完成上载”方法中在SharePoint online上上载文件时,503服务不可用,java,rest,sharepoint,sharepoint-2013,chunks,Java,Rest,Sharepoint,Sharepoint 2013,Chunks,我正在尝试在大于250MB的SharePoint上上载文件。我已经将文件的数据分成小块,比如100MB,并使用开始上传、继续上传和完成SharePoint的上传。如果我的文件大小为250MB或更大,我在FinishUpload方法中获得的503服务不可用。但是,以下代码在最大249MB的文件中成功运行。任何线索或帮助都将不胜感激 谢谢 古拉蒂酒店 package test; import java.io.BufferedInputStream; import java.io.BufferedR

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

谢谢

古拉蒂酒店

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

    }
}
封装测试;
导入java.io.BufferedInputStream;
导入java.io.BufferedReader;
导入java.io.ByteArrayOutputStream;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.io.UnsupportedEncodingException;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.ParseException;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.entity.ByteArrayEntity;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.impl.client.HttpClientBuilder;
导入org.apache.http.message.BasicNameValuePair;
导入org.apache.http.util.EntityUtils;
导入org.json.JSONObject;
公共类SampleFileUpload1{
私有最终静态int chunkSize=100*1024*1024;
公共静态void main(字符串args[])引发IOException{
SampleFileUpload1 fileUpload=新建SampleFileUpload1();
File File=新文件(“C:\\users\\bgulati\\Desktop\\twofivefive.txt”);
GenereAndUploadChunks(文件);
}
私有静态void executeRequest(HttpPost HttpPost,字符串urlString){
试一试{
HttpClient=new DefaultHttpClient();
HttpResponse response=client.execute(httpPost);
System.out.println(“响应代码:+Response.getStatusLine().getStatusCode());
System.out.println(“Response getReasonPhrase:+Response.getStatusLine().getReasonPhrase());
System.out.println(“Response getReasonPhrase:+Response.getEntity().getContent().toString());
BufferedReader br=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
while(true){
字符串s=br.readLine();
如果(s==null)
打破
系统输出打印项次;
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(非法状态){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
公共静态void executeMultiPartRequest(字符串urlString,字节[]fileByteArray)引发IOException{
HttpPost postRequest=新的HttpPost(urlString);
postRequest=addHeader(postRequest,“accessToken”);
试一试{
setEntity(newbytearrayentity(fileByteArray));
}捕获(例外情况除外){
例如printStackTrace();
}
executeRequest(postRequest,urlString);
}
私有静态HttpPost addHeader(HttpPost HttpPost,String accessToken){
addHeader(“Accept”,“application/json;odata=verbose”);
httpPost.setHeader(“授权”、“承载人”+accessToken);
setHeader(“内容类型”,“应用程序/json;odata=verbose;charset=utf-8”);
返回httpPost;
}
私有静态字符串getUniqueId(HttpResponse响应,字符串键)引发ParseException,IOException{
如果(检查响应(响应)){
字符串responseString=EntityUtils.toString(response.getEntity(),“UTF-8”);
JSONObject json=新的JSONObject(responseString);
返回json.getJSONObject(“d”).getString(key);
}
返回“”;
}
私有静态布尔检查响应(HttpResponse响应)引发ParseException、IOException{
if(response.getStatusLine().getStatusCode()=200 | |(response.getStatusLine().getStatusCode()=201)){
返回true;
}
返回false;
}
私有字符串createDummyFile(字符串相对路径,字符串文件名)引发ClientProtocolException,IOException{
字符串URL字符串=”https://siteURL/_api/web/GetFolderByServerRelativeUrl(“+relativePath+”)/Files/add(url=“+fileName+”,overwrite=true)”;
HttpPost postRequest=新的HttpPost(urlString);
postRequest=addHeader(postRequest,“访问令牌”);
HttpClient=new DefaultHttpClient();
HttpResponse response=client.execute(postRequest);
返回getUniqueId(响应,“UniqueId”);
}
私有void GenereAndUploadChunks(文件)引发IOException{
字符串relativePath=“/relativePath”;
字符串文件名=file.getName();
字符串gUid=createDummyFile(相对路径,文件名);
字符串端点URL=”https://siteURL/_api/web/GetFileByServerRelativeUrl(“+relativePath+”/“+fileName+”)/savebinarystream”;
HttpPost postRequest=新的HttpPost(端点URL);
postRequest=addHeader(postRequest,“访问令牌”);
HttpClient=new DefaultHttpClient();
HttpResponse response=client.execute(postRequest);
long fileSize=file.length();

如果(文件大小除以更多,服务器不接受250 mb,可能您有什么解决方案@Bharati@Rex不幸的是,没有。再分更多,服务器不接受250 mb。你有什么解决方案吗@Bharati@Rex不幸的是,没有。