1

I´m trying to upload a file to Sharepoint server 2016. I used the following code, which authenticates me correctly in sharepoint server, but when it tries to upload the file, a CONFLICT occurs httpcode: 409. Do you know why? Thanks in advance

public class Upload {

public static void main(String[] args) throws Exception{
     CloseableHttpClient httpclient = HttpClients.custom()
      .setRetryHandler(new DefaultHttpRequestRetryHandler(0,false))
      .build();


     String user = "user";
     String pwd = "password";
     CredentialsProvider credsProvider = new BasicCredentialsProvider();
     credsProvider.setCredentials(AuthScope.ANY,
        new NTCredentials(user, pwd, "", ""));

     HttpHost target = new HttpHost("localhost",port, "http");
     HttpClientContext context = HttpClientContext.create();
     context.setCredentialsProvider(credsProvider);

     // The authentication is NTLM.
     // To trigger it, we send a minimal http request
     HttpHead request1 = new HttpHead("/");
     CloseableHttpResponse response1 = null;
     try {
       response1 = httpclient.execute(target, request1, context);
       EntityUtils.consume(response1.getEntity());
       System.out.println("1 : " + response1.getStatusLine().getStatusCode());
     }
     finally {
       if (response1 != null ) response1.close();
     }

     // The real request, reuse authentication
     String file = "C:\\Prueba.txt";
      HttpPut request2 = new HttpPut("my/personal/KIEWB/Documentos%20compartidos/Prueba.txt");
     request2.setEntity(new FileEntity(new File("C:\\Prueba.txt")));// source
     CloseableHttpResponse response2 = null;
     try {
       response2 = httpclient.execute(target, request2, context);
       EntityUtils.consume(response2.getEntity());
       int rc = response2.getStatusLine().getStatusCode();
       String reason = response2.getStatusLine().getReasonPhrase();
       // The possible outcomes :
       //    201 Created
       //        The request has been fulfilled and resulted in a new resource being created
       //    200 OK
       //        Standard response for successful HTTP requests.
       //    others
       //        we have a problem
       if (rc == HttpStatus.SC_CREATED) {
         System.out.println(file + " is copied (new file created)");
       }
       else if (rc == HttpStatus.SC_OK) {
         System.out.println(file + " is copied (original overwritten)");
       }
       else {
         throw new Exception("Problem while copying " + file
             + "  reason " + reason + "  httpcode : " + rc);
       }
     }
     finally {
       if (response2 != null) response2.close();
     }
     return;
   }

}

1

There are many reasons which might cause 409 conflict error when upload files, you can check the common reasons as following to narrow down:

  1. Make sure the URL which you want to upload file is correct, or you can switch to another library/site and check if the issue exists.

  2. Check if there is same file in the relevant library.

  3. Make sure the file you want to upload doesn’t exceed the size limitation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.