How to access .xlsx file stored on Sharepoint (with authentication) for Apache POI?

我的梦境 提交于 2021-01-24 10:57:26

问题


  1. I have a .xlsx file stored on sharepoint, which requires login in order to get/see the excel file.
  2. I need that file in order to do some ApachePOI operations on it
  3. How to download that file?

I have tried doing it via InputStream but I don't know how to authenticate and I get 403 error which is obvious, as I don't provide password/login (don't know how)

InputStream inputStream = new URL("https://example.sharepoint.com/something/something1/file.xlsx").openStream();
Files.copy(inputStream, Paths.get("C:/file.xlsx"));

Is there a simple way to access that file?


回答1:


I would try java.net.HttpURLConnection together with java.net.Authenticator and java.net.PasswordAuthentication.

Example:

import org.apache.poi.ss.usermodel.*;

import java.io.InputStream;
import java.net.URL;
import java.net.HttpURLConnection;
import java.net.Authenticator;
import java.net.PasswordAuthentication;

class ReadExcelFromURL {

 public static void main(String[] args) throws Exception {

  Authenticator.setDefault (new Authenticator() {
   private String username = "Domain\\UserName";
   private String password = "password";
   protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication (username, password.toCharArray());
   }
  });

  String stringURL = "https://example.sharepoint.com/something/something1/file.xlsx";

  URL url = new URL(stringURL);
  HttpURLConnection con = (HttpURLConnection)url.openConnection();

  InputStream in = con.getInputStream();
  Workbook workbook = WorkbookFactory.create(in);

  for (Sheet sheet : workbook) {
   System.out.println(sheet); // success?
  }

  workbook.close();
 }
}



回答2:


Using this maven dependency:

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

import org.apache.http.client.CredentialsProvider;
import org.apache.http.auth.AuthScope;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.auth.NTCredentials;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;

public class SharePointClientAuthentication {

public static void main(String[] args) throws Exception {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
            new AuthScope(AuthScope.ANY),
            new NTCredentials("username", "password", "https://hostname", "domain"));
    CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider)
            .build();
    try {
        HttpGet httpget = new HttpGet("http://hostname/_api/web/lists");

        System.out.println("Executing request " + httpget.getRequestLine());
        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            EntityUtils.consume(response.getEntity());
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }


来源:https://stackoverflow.com/questions/58626879/how-to-access-xlsx-file-stored-on-sharepoint-with-authentication-for-apache-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!