我试图通过浏览器访问 URL 没有问题,但是我的程序抛出: java.io.IOException: Server returned HTTP response code: 403 for URL:
这里的 URL 只是我的Sharepoint Online服务器上列表项的附件文件路径。我正在尝试获取该文件的内容。它从浏览器中打开,但从代码中抛出异常。
代码:
private String getAttachmentContent(String attachmentURL) throws IBSharePointException
{
InputStream is = null;
try
{
String fileName=attachmentURL.substring(attachmentURL.lastIndexOf("/")+1);
String urlPath=attachmentURL.substring(0, attachmentURL.lastIndexOf("/")+1);
fileName=URLEncoder.encode(fileName, "UTF-8");
if(fileName.contains("+"))
fileName=fileName.replace("+", "%20");
URL u=new URL(urlPath+fileName);
// Following Line Throws Exception : java.io.IOException: Server returned HTTP response code: 403 for URL:
is = u.openStream();
ByteArrayOutputStream bais = new ByteArrayOutputStream();
byte[] byteChunk = new byte[4096];
int n;
while ( (n = is.read(byteChunk)) > 0 )
{
bais.write(byteChunk, 0, n);
}
}catch(Exception e)
{
throw e;
}
}
我已经完成了代码中的所有设置,甚至尝试了与该主题相关的所有可能解决方案,但仍然无法正常工作。