简体   繁体   English

使用Java程序从Sharepoint下载文件

[英]Downloading a file from sharepoint using java program

I'm trying to download an excel workbook from a sharepoint site using java program. 我正在尝试使用Java程序从SharePoint网站下载Excel工作簿。 However I'm getting the error : Server redirected too many times (20) . 但是我得到了错误: 服务器重定向太多次(20) I searched the forums and it appears to be an issue with cookies. 我搜索了论坛,它似乎是cookie的问题。 After searching the forums, I added the first line of code to set default cookie handler, but the error still exists. 搜索论坛之后,我添加了第一行代码来设置默认的Cookie处理程序,但该错误仍然存​​在。

The java program is part of an osgi bundle. Java程序是osgi捆绑包的一部分。 Strangely the program works when I run from my local PC running an instance of servicemix. 奇怪的是,当我从运行Servicemix实例的本地PC运行该程序时,该程序正常工作。 But when I deploy this on the servicemix running on a remote server (linux machine), it gives this error. 但是,当我将其部署在远程服务器(Linux计算机)上运行的servicemix上时,会出现此错误。

I wanted to do this without having to use any third party libraries like apache-http etc. 我想这样做而不必使用任何第三方库,例如apache-http等。

Any suggestions what could be going wrong here? 有什么建议可能会出问题吗?

    CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));


    Authenticator.setDefault(new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {          
            return new PasswordAuthentication(USERNAME, 
                    PASSWORD.toCharArray());
        }
    });

    URL website = new URL(dataLocation);
    // data location points to the sharepoint file
    // its something like 
    // http://<company.sharepointsite.com>/project/sites/SampleProject/ProjectDocuments/Sample.xlsx


    ReadableByteChannel rbc = Channels.newChannel(website.openStream());
    FileOutputStream fos = new FileOutputStream(destinationFile);
    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

Finally, I ended up using Apache HTTP libraries which solved the problem. 最后,我最终使用了Apache HTTP库,该库解决了该问题。 although this is an old post, I'm updating this as it might help others who are facing similar issue. 尽管这是一篇过时的文章,但我正在对此进行更新,因为它可能会帮助面临类似问题的其他人。 http://mvnrepository.com/artifact/org.apache.httpcomponents http://mvnrepository.com/artifact/org.apache.httpcomponents

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

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