get a document from sharepoint from java

Solutions on MaxInterview for get a document from sharepoint from java by the best coders in the world

showing results for - "get a document from sharepoint from java"
Karl
22 Mar 2020
1/*
2Sharepoint REST details to retrieve a specific file
3url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files('file name')/$value
4method: GET
5headers:
6    Authorization: "Bearer " + accessToken
7*/
8
9try {		
10   //Frame SharePoint siteURL			
11   String siteURL = "https://<host>/<path>";
12		
13   //Frame SharePoint URL to retrieve all of the files in a folder
14   String wsUrl = siteURL + "/_api/web/GetFolderByServerRelativeUrl('Shared%20Documents')/Files('XYZ.txt')/$value";
15	
16   //Create HttpURLConnection
17   URL url = new URL(wsUrl);
18   URLConnection connection = url.openConnection();
19   HttpURLConnection httpConn = (HttpURLConnection) connection;
20						
21   //Set Header		
22   httpConn.setRequestMethod("GET");				
23   httpConn.setRequestProperty("Authorization", "Bearer " + accessToken);
24  
25   //Read the response
26   String httpResponseStr = "";
27   InputStreamReader isr = null;
28   if (httpConn.getResponseCode() == 200) {
29     isr = new InputStreamReader(httpConn.getInputStream());
30   } else {
31     isr = new InputStreamReader(httpConn.getErrorStream());
32   }		
33   BufferedReader in = new BufferedReader(isr); 	 		
34   String strLine = "";
35   while ((strLine = in.readLine()) != null) {
36     httpResponseStr = httpResponseStr + strLine;
37   }
38   //System.out.println(httpResponseStr);	//Print response
39} catch (Exception e) {
40   //System.out.println("Error while reading file: " + e.getMessage());
41}		
42