How do I upload a document to SharePoint with Java?

前端 未结 6 1810
余生分开走
余生分开走 2020-12-30 03:47

I\'m creating some big files (DB exports) with Java and I need to put them somewhere on our SharePoint server. Right now, I\'m doing this with IE but I\'d like to automate t

相关标签:
6条回答
  • 2020-12-30 03:51

    In addition to Sacha's suggestions, you can use the SharePoint SOAP web services. Each SharePoint site exposes a bunch of web services via the path http://<Site>/_vti_bin/.

    In your case, you probably want the Lists web service (http://<Site>/_vti_bin/Lists.asmx). You can grab the WSDL from http://<Site>/_vti_bin/Lists.asmx?WSDL. The WSS 3.0 SDK has details on how to use the web service (you'll probably want to use the UpdateListItems and AddAttachment methods).

    All that said, Sacha's first option (mapping a document library to a drive) is probably the easiest way assuming you can get around the NTLM issues.

    If you're using Windows you can simply navigate to a UNC path for a document library. For example, if the browser URL for your document library is:

    http://<Site>/Foo/BarDocs/Forms/AllItems.aspx

    you can simply type the corresponding UNC path in the Windows Explorer address bar:

    \\<Site>\Foo\BarDocs

    and then drag and drop files to this location. If you'd like you can map this location to a drive letter using Windows Explorer or the SUBST.EXE command-line utility.

    0 讨论(0)
  • 2020-12-30 03:51

    Okay ... after several hours of work and biting myself through the "documentation" MicroSoft provides and all the hints randomly spread over the 'net, I've managed to write some sample code to browse the content of a SharePoint server: Navigating SharePoint Folders With Axis2.

    Next stop: Uploading something.

    0 讨论(0)
  • 2020-12-30 03:55

    An other solution is to use HTTP PUT method to send a file directly to the Sharepoint.

    For that you can use Apache HTTP Client:

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.2.3</version>
    </dependency>
    

    And to permit NTLMv2 authentication you need JCIF library.

    <dependency>
       <groupId>jcifs</groupId>
       <artifactId>jcifs</artifactId>
       <version>1.3.17</version>
    </dependency>
    

    First we need to write a wrapper to permit Apache HTTP Client to use JCIF for NTLMv2 support :

    public final class JCIFSEngine implements NTLMEngine {
    
        private static final int TYPE_1_FLAGS =
                NtlmFlags.NTLMSSP_NEGOTIATE_56
                | NtlmFlags.NTLMSSP_NEGOTIATE_128
                | NtlmFlags.NTLMSSP_NEGOTIATE_NTLM2
                | NtlmFlags.NTLMSSP_NEGOTIATE_ALWAYS_SIGN
                | NtlmFlags.NTLMSSP_REQUEST_TARGET;
    
        @Override
        public String generateType1Msg(final String domain, final String workstation)
                throws NTLMEngineException {
            final Type1Message type1Message = new Type1Message(TYPE_1_FLAGS, domain, workstation);
            return Base64.encode(type1Message.toByteArray());
        }
    
        @Override
        public String generateType3Msg(final String username, final String password,
                final String domain, final String workstation, final String challenge)
                throws NTLMEngineException {
            Type2Message type2Message;
            try {
                type2Message = new Type2Message(Base64.decode(challenge));
            } catch (final IOException exception) {
                throw new NTLMEngineException("Invalid NTLM type 2 message", exception);
            }
            final int type2Flags = type2Message.getFlags();
            final int type3Flags = type2Flags
                    & (0xffffffff ^ (NtlmFlags.NTLMSSP_TARGET_TYPE_DOMAIN | NtlmFlags.NTLMSSP_TARGET_TYPE_SERVER));
            final Type3Message type3Message = new Type3Message(type2Message, password, domain,
                    username, workstation, type3Flags);
            return Base64.encode(type3Message.toByteArray());
        }
    }
    

    Reference

    The main code to execute HTTP PUT with authentication:

        try {
    
            HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            DefaultHttpClient httpclient = new DefaultHttpClient(params);
    
            //Register JCIF NTLMv2 to manage ntlm auth.
            httpclient.getAuthSchemes().register("ntlm", new AuthSchemeFactory() {
                @Override
                public AuthScheme newInstance(HttpParams hp) {
                    return new NTLMScheme(new JCIFSEngine());
                }
            });
    
            //Provide login/password
            httpclient.getCredentialsProvider().setCredentials(
                    AuthScope.ANY,
                    new NTCredentials([LOGIN], [PASSWORD], "", [DOMAIN]));
            //Create HTTP PUT Request       
            HttpPut request = new HttpPut("http://[server]/[site]/[folder]/[fileName]");
            request.setEntity(new FileEntity([File]));            
    
            return httpclient.execute(request);
    
        } catch (IOException ex) {
          //...
        }
    
    0 讨论(0)
  • 2020-12-30 04:00

    I think my approach might help you.

    Initially i have created sharepoint account and followed the procedure in this link (http://www.ktskumar.com/2017/01/access-sharepoint-online-using-postman/) to get needed credentials for REST API's. once i got the credentials all i needed was the following dependency and code:

    <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5</version>
    </dependency>
    

    Since i used OAUTH2 authentication, the code to get access token helps for other CRUD operations.

    /* OAuth2 authentication to get access token */
    public String getSharePointAccessToken() throws ClientProtocolException, IOException
    {
        /* Initializing variables */
        String grant_type = RcConstants.GRANT_TYPE;
        String client_id = RcConstants.CLIENT_ID;
        String client_secret = RcConstants.CLIENT_SECRET;
        String resource = RcConstants.RESOURCE;
        String url = RcConstants.OAUTH_URL + RcConstants.URL_PARAMETER + "/tokens/OAuth/2";
    
        /*
         * NOTE: RcConstants.OAUTH_URL =
         * https://accounts.accesscontrol.windows.net/ RcConstants.URL_PARAMETER
         * = Bearer Realm from
         * (http://www.ktskumar.com/2017/01/access-sharepoint-online-using-
         * postman/) Figure 6.
         */
    
        /* Building URL */
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
        post.setHeader("Content-Type", "application/x-www-form-urlencoded");
    
        /* Adding URL Parameters */
        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("grant_type", grant_type));
        urlParameters.add(new BasicNameValuePair("client_id", client_id));
        urlParameters.add(new BasicNameValuePair("client_secret", client_secret));
        urlParameters.add(new BasicNameValuePair("resource", resource));
        post.setEntity(new UrlEncodedFormEntity(urlParameters));
    
        /* Executing the post request */
        HttpResponse response = client.execute(post);
        logger.debug("Response Code : " + response.getStatusLine().getStatusCode());
    
        String json_string = EntityUtils.toString(response.getEntity());
        JSONObject temp1 = new JSONObject(json_string);  
        if (temp1 != null)
        {
            /* Returning access token */
            return temp1.get("access_token").toString();
        }
        return RcConstants.OAUTH_FAIL_MESSAGE;
    }
    

    Once we get access token we can upload using following method:

    public String putRecordInSharePoint(File file) throws ClientProtocolException, IOException
    {
        /* Token variable declaration */
        String token = getSharePointAccessToken();
        /* Null or fail check */
        if (!token.equalsIgnoreCase(RcConstants.OAUTH_FAIL_MESSAGE))
        { 
            /* Upload path and file name declaration */
            String Url_parameter = "Add(url='" + file.getName() + "',overwrite=true)";
            String url = RcConstants.UPLOAD_FOLDER_URL + Url_parameter;
            /*
             * NOTE: RcConstants.UPLOAD_FOLDER_URL =
             * https://<your_domain>.sharepoint.com/_api/web/
             * GetFolderByServerRelativeUrl('/Shared%20Documents/<FolderName>')/
             * Files/
             */
    
            /* Building URL */
            HttpClient client = HttpClientBuilder.create().build();
            HttpPost post = new HttpPost(url);
            post.setHeader("Authorization", "Bearer " + token);
            post.setHeader("accept", "application/json;odata=verbose");
            /* Declaring File Entity */
            post.setEntity(new FileEntity(file));
    
            /* Executing the post request */
            HttpResponse response = client.execute(post);
            logger.debug("Response Code : " + response.getStatusLine().getStatusCode());
    
            if (response.getStatusLine().getStatusCode() == HttpStatus.OK.value()|| response.getStatusLine().getStatusCode() == HttpStatus.ACCEPTED.value())
            {
                /* Returning Success Message */
                return RcConstants.UPLOAD_SUCCESS_MESSAGE;
            }
            else
            {
                /* Returning Failure Message */
                return RcConstants.UPLOAD_FAIL_MESSAGE;
            }
        }
        return token;
    }
    
    0 讨论(0)
  • 2020-12-30 04:01

    I can think of different options:

    • Mapping the Document library to a file drive and just save the file like any other file in the file system.
    • Using HTTP WebDAV protocol.

    ...and for the NTLM authentication part:

    http://www.luigidragone.com/networking/ntlm.html

    0 讨论(0)
  • 2020-12-30 04:13

    I managed to up and download files to sharepoint with this code, using the integrated Windows identification, maybe it helps.

    public class HttpClient {               
        HttpClient() { }
    
        public static void download(final String source, final File resultingFile) {
            CloseableHttpClient client = WinHttpClients.createSystem();
            HttpGet httpRequest = new HttpGet(source);
    
            CloseableHttpResponse httpResponse = null;      
            try {
                httpResponse = client.execute(httpRequest);
                HttpEntity entity = httpResponse.getEntity();
    
                if(httpResponse.getStatusLine() != null && httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                    LOGGER.warn(httpResponse.getStatusLine());
                }else {  
                    LOGGER.debug(httpResponse.getStatusLine());
                    FileUtils.touch(resultingFile);
                    InputStream is = entity.getContent(); 
                    File outFile = new File(resultingFile.getAbsolutePath());
                    FileOutputStream fos = new FileOutputStream(outFile);
    
                    int inByte;
                    while ((inByte = is.read()) != -1) {
                        fos.write(inByte);
                    }
                    is.close();
                    fos.close(); 
                    client.close();
                }
            } catch (ClientProtocolException e) {
                LOGGER.warn(e);
            } catch (UnsupportedOperationException e) {
                LOGGER.warn(e);
            } catch (IOException e) {
                LOGGER.warn(e);
            }
        }
    
    
        public static void upload(final File source, final String destination) {    
            CloseableHttpClient httpclient = WinHttpClients.createSystem();
            HttpPut httpRequest = new HttpPut(destination);
            httpRequest.setEntity(new FileEntity(new File(source.getPath())));
    
            CloseableHttpResponse httpResponse = null;
            try {
                httpResponse = httpclient.execute(httpRequest);
                EntityUtils.consume(httpResponse.getEntity());
    
                if (httpResponse.getStatusLine() != null && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
                    LOGGER.debug(httpResponse.getStatusLine());
                    LOGGER.info("Upload of " + source.getName() + " via HTTP-Client succeeded.");
                } else if (httpResponse.getStatusLine() != null && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    LOGGER.debug(httpResponse.getStatusLine());
                }else {
                    LOGGER.warn("Uploading " + source.getName() + " failed.");
                    LOGGER.warn(httpResponse.getStatusLine().getStatusCode() + ": " + httpResponse.getStatusLine().getReasonPhrase());
                }
            } catch (IOException e) {
                LOGGER.warn(e);
                LOGGER.warn(e.getMessage());
            }       
            return;
        }
    }
    

    WinHttpClients:

     <dependency>
         <groupId>org.apache.httpcomponents</groupId>
         <artifactId>httpclient-win</artifactId>
         <version>4.4</version>
     </dependency> 
    

    Path:
    org.apache.http.impl.client.WinHttpClients

    Description:
    Factory methods for CloseableHttpClient instances configured to use integrated Windows authentication by default.

    0 讨论(0)
提交回复
热议问题