使用java程序从sharepoint下载文件

时间:2015-08-20 00:10:22

标签: java sharepoint cookies url-redirection

我尝试使用java程序从sharepoint站点下载excel工作簿。但是我收到错误: 服务器重定向次数太多(20) 。我搜索了论坛,这似乎是一个cookie的问题。在搜索论坛后,我添加了第一行代码来设置默认的cookie处理程序,但错误仍然存​​在。

java程序是osgi包的一部分。奇怪的是,当我从运行servicemix实例的本地PC运行时程序可以正常工作。但是当我在远程服务器(linux机器)上运行的servicemix上部署它时,它会出现此错误。

我想这样做而不必使用任何第三方库,如apache-http等。

有什么建议可能会出错吗?

    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);

1 个答案:

答案 0 :(得分:0)

最后,我最终使用Apache HTTP库解决了这个问题。虽然这是一篇旧帖子,但我正在更新它,因为它可能会帮助其他面临类似问题的人。 http://mvnrepository.com/artifact/org.apache.httpcomponents