简体   繁体   English

使用 JAVA 中的 REST API 将文件在线上传到 Sharepoint

[英]Upload a file to Sharepoint online using REST API in JAVA

I am new to sharepoint rest API and am facing some issue while upload a file(image, document, pdf etc.,) to sharepoint online.我是 sharepoint rest API 的新手,在将文件(图像、文档、pdf 等)上传到 sharepoint 在线时遇到了一些问题。 Thanks in advance.提前致谢。

The below is our requirement.以下是我们的要求。

  1. User will upload the document which are stored at a particular location in application server.用户将上传存储在应用服务器中特定位置的文档。

  2. A cron job will be running on application server and push the documents to share point online depend upon business needs.一个 cron 作业将在应用服务器上运行,并根据业务需要将文档推送到在线共享点。

To achieve it, we follow the below steps.为了实现它,我们按照以下步骤操作。

  1. Authentication done via AZURE access token (We have used client credential flow to get access token from AZURE AD and able to communicate with sahrepoint online with access token.)通过 AZURE 访问令牌完成身份验证(我们使用客户端凭据流从 AZURE AD 获取访问令牌,并能够使用访问令牌与 sahrepoint 在线通信。)

  2. We have consumed the sharepoint online REST API to do file operation like upload, download etc,.我们已经使用了 sharepoint 在线 REST API 来执行文件操作,例如上传、下载等。 using java code.使用java代码。

Here we are able to download the file from sharepoint online but when we upload the file, we are getting response as "BAD REQUEST" and status code is "400"在这里,我们可以从 sharepoint 在线下载文件,但是当我们上传文件时,我们收到的响应为“BAD REQUEST”,状态代码为“400”

Sharepoint online rest API to create a file:创建文件的Sharepoint在线rest API:

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

My Java code :我的Java代码:

            //Create HttpURLConnection
            String token ="js#1ikssj......RDS2" // This is just sample
            String request = "Create a File with raw string !!!";
            java.net.URL url = new java.net.URL("http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files/add(url='a.txt',overwrite=true)");
            java.net.URLConnection connection = url.openConnection();
            java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connection;
            //Set Header
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);        
            httpConn.setRequestMethod("POST");                                                  
            httpConn.setRequestProperty("Authorization", "Bearer " +token);                                                                                       
          httpConn.setRequestProperty ("Accept", "application/json;odata=verbose");       
            httpConn.setRequestProperty("binaryStringRequestBody", "true");                                                          
            //Send Request
            java.io.DataOutputStream wr = new java.io.DataOutputStream(httpConn.getOutputStream ());                                                   
            wr.writeBytes(request);
            wr.flush();
            wr.close();
              //Read the response.
            String StatusMessage = "HTTP ResponseCode: " + httpConn.getResponseCode() + " "+  httpConn.getResponseMessage();            

Response : 400 - BAD REQUEST.响应:400 - 错误请求。

You can take a look of this project where you can find a working implementation of uploading files, creating folders, managing folder user permissions and more.你可以看看这个项目,在那里你可以找到上传文件、创建文件夹、管理文件夹用户权限等的工作实现。 It's a very easy to use API with most common operations of the rest API这是一个非常易于使用的 API,具有其余 API 的最常见操作

https://github.com/kikovalle/PLGSharepointRestAPI-java https://github.com/kikovalle/PLGSharepointRestAPI-java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM