我正在尝试开发一种 put 方法来使用图形 API 在 Sharepoint 上创建文件。创建上传会话允许将文件从 4Mo 上传到 60Mo。我创建了这个会话并获得了一个看起来像这样的响应。
HTTP/1.1 200 OK
Content-Type: application/json
{
"uploadUrl": "https://url_for_uploading_file",
"expirationDateTime": "2015-01-29T09:21:55.523Z"
}
根据文档,我必须检索 uploadUrl 并发送我的文件。 https://docs.microsoft.com/fr-fr/graph/api/driveitem-createuploadsession?view=graph-rest-1.0
我创建了一个接收请求、上传 url 和我的令牌的方法。此方法允许设置 httpPut 参数。
public SharePointDocumentResponseModel putFileRequest(SharePointDocumentByNameRequest request,
String token) throws IOException {
SharePointDocumentResponseModel returnValue = new SharePointDocumentResponseModel();
filePath = request.getFilePath();
fileContent = FileUtils.readFileToByteArray(new File(request.getFilePath()));
byte[][] fileArrays = Utils.splitChunks(fileContent);
int size = fileContent.length;
long chunkSize = Constants.MAX_FILE_SPLIT;
int numberOfChunks = (int) (size / chunkSize);
int minRange = 0;
int maxRange = 0;
int count = 0;
SortedMap<Integer,Integer> ranges = new TreeMap<>();
// TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
// public X509Certificate[] getAcceptedIssuers() {
// return null;
// }
// public void checkClientTrusted(X509Certificate[] certs, String authType) {
// }
// public void checkServerTrusted(X509Certificate[] certs, String authType) {
// }
// }};
System.out.println("number of chunks " + (numberOfChunks+1));
for(int i = 0; i <= numberOfChunks; i++) {
maxRange += chunkSize;
if(maxRange < size) {
ranges.put(minRange, maxRange);
} else {
ranges.put(minRange, size);
}
minRange = maxRange + 1;
}
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPut httpPut = new HttpPut(String.valueOf(request.getUploadUrl()));
httpPut.setHeader("Authorization", "Bearer " + token);
httpPut.setHeader("Content-Type", "text/json;charset=UTF-8");
//httpPut.setHeader("Accept", "application/json");
httpPut.setHeader("Connection", "Keep-Alive");
httpPut.setHeader("Cache-Control", "no-cache");
// Install the all-trusting trust manager
// try {
// SSLContext sc = SSLContext.getInstance("TLSv1.2");
// sc.init(null, trustAllCerts, new SecureRandom());
// HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// } catch (Exception e) {
// e.printStackTrace();
// }
// splitter le fichier
for (Map.Entry<Integer, Integer> range: ranges.entrySet()) {
int step = range.getValue() - range.getKey();
//httpPut.setHeader("Content-Range",
// "bytes " + range.getKey() + "-" + range.getValue() + "/" + fileContent.length);
httpPut.setHeader("Content-Range",
"bytes " + range.getKey() + "-" + range.getValue() + "/" + Constants.CHUNK_SIZE);
InputStreamEntity inputStreamEntity =
new InputStreamEntity(
new ByteArrayInputStream(fileArrays[count]),
fileContent.length
);
count++;
//inputStreamEntity.setChunked(true);
httpPut.setEntity(inputStreamEntity);
System.out.println("\nSending 'PUT' request to URL : " + request.getUploadUrl());
System.out.println("Executing request : " + httpPut.getRequestLine());
try (CloseableHttpResponse response = httpclient.execute(httpPut)) {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity()));
//returnValue = gson.fromJson(response.getEntity(), SharePointDocumentResponseModel.class);
}
}
//InputStreamEntity inputStreamEntity = new InputStreamEntity(
// new FileInputStream(file), -1, ContentType.APPLICATION_OCTET_STREAM);
} finally {
httpclient.close();
}
return returnValue;
}
在这个阶段,返回值尚未设置。我获得了 SSL 异常和连接重置。我试图添加一个 trustAllCerts TrustManager 数组对象来解决这个问题,但也抛出了异常。在我的图书馆中创建了一个临时文档。
Exception in thread "main" javax.net.ssl.SSLException: Connection reset
at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:127)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:321)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:264)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:259)
at java.base/sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1314)
at java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:839)
at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:137)
at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:153)
at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:280)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:138)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163)
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:157)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
at fr.dsidiff.api.sharepoint.request.HttpRequest.putFileRequest(HttpRequest.java:363)
at fr.dsidiff.api.sharepoint.service.impl.DocumentServiceImpl.uploadWithSession(DocumentServiceImpl.java:39)
at fr.dsidiff.api.sharepoint.SharePointCaller.getResponse(SharePointCaller.java:410)
at fr.dsidiff.api.sharepoint.SharePointCaller.createDocument(SharePointCaller.java:363)
at fr.dsidiff.api.sharepoint.tests.DocumentCallTest.createDocumentByNameBigFile(DocumentCallTest.java:51)
at fr.dsidiff.api.sharepoint.tests.DocumentCallTest.main(DocumentCallTest.java:29)
Suppressed: java.net.SocketException: Connection reset by peer: socket write error
at java.base/java.net.SocketOutputStream.socketWrite0(Native Method)
at java.base/java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:110)
at java.base/java.net.SocketOutputStream.write(SocketOutputStream.java:150)
at java.base/sun.security.ssl.SSLSocketOutputRecord.encodeAlert(SSLSocketOutputRecord.java:81)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:352)
... 27 more
Caused by: java.net.SocketException: Connection reset
at java.base/java.net.SocketInputStream.read(SocketInputStream.java:186)
at java.base/java.net.SocketInputStream.read(SocketInputStream.java:140)
at java.base/sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:448)
at java.base/sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:68)
at java.base/sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1104)
at java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:823)
... 23 more