使用java程序从sharepoint下载文件

使用java程序从sharepoint下载文件,java,sharepoint,cookies,url-redirection,Java,Sharepoint,Cookies,Url Redirection,我正在尝试使用java程序从sharepoint网站下载excel工作簿。但是我得到了一个错误:服务器重定向太多次(20次)。我搜索了论坛,似乎是cookies的问题。搜索论坛后,我添加了第一行代码来设置默认cookie处理程序,但错误仍然存在 java程序是osgi捆绑包的一部分。奇怪的是,当我从运行servicemix实例的本地PC上运行时,该程序会工作。但是,当我在远程服务器(linux机器)上运行的servicemix上部署它时,会出现此错误 我想这样做,而不必使用任何第三方库,如Apa

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

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

我想这样做,而不必使用任何第三方库,如ApacheHTTP等

有什么建议这里会出什么问题吗

    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);
CookieHandler.setDefault(新的CookieManager(null,CookiePolicy.ACCEPT_ALL));
setDefault(新验证器(){
@凌驾
受保护的密码身份验证getPasswordAuthentication(){
返回新密码身份验证(用户名,
密码。toCharArray());
}
});
URL网站=新URL(数据位置);
//数据位置指向sharepoint文件
//有点像
// http:///project/sites/SampleProject/ProjectDocuments/Sample.xlsx
ReadableByteChannel rbc=Channels.newChannel(website.openStream());
FileOutputStream fos=新的FileOutputStream(destinationFile);
fos.getChannel().transferFrom(rbc,0,Long.MAX_值);

最后,我使用Apache HTTP库解决了这个问题。虽然这是一篇老文章,但我正在更新它,因为它可能会帮助其他面临类似问题的人。

您使用的是哪一版本的SharePoint?在这两种情况下,考虑使用通过Web服务公开的客户端对象模型(CSOM)。在旧版本的SharePoint(20072010)中,web服务是ASMX,而新版本是RESTful。通过这种方式,您可以正确使用受支持的SharePoint对象模型。