【发布时间】:2021-11-08 12:39:13
【问题描述】:
我在从具有身份验证的 URL 下载文件时遇到问题。 我对这个话题很陌生,我需要你们的帮助! :(
我想从一个 url 下载一个 docx 文档。如果您手动输入网址,它会自动为您下载。就我而言,该文件位于我们的 Intranet 上。
这是我目前得到的: 主类
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Authenticator;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
public class JavaDownloadFileFromURL {
public static void main(String[] args) {
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
Authenticator.setDefault(new CustomAuthenticator());
String url = "http://ourintranet.site.com/some/path/file.docx";
try {
downloadUsingStream(url, "/Users/Me/Documents/test.docx");
} catch (IOException e) {
e.printStackTrace();
}
}
private static void downloadUsingStream(String urlStr, String file) throws IOException{
URL url = new URL(urlStr);
BufferedInputStream bis = new BufferedInputStream(url.openStream());
FileOutputStream fis = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int count=0;
while((count = bis.read(buffer,0,1024)) != -1)
{
fis.write(buffer, 0, count);
}
fis.close();
bis.close();
}
}
我发现我需要一个身份验证器,因为之前我遇到了 401 错误。所以这是我的身份验证器:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
public class CustomAuthenticator extends Authenticator
{
protected PasswordAuthentication getPasswordAuthentication()
{
String prompt = getRequestingPrompt();
String hostname = getRequestingHost();
InetAddress ipaddr = getRequestingSite();
int port = getRequestingPort();
String protocol = getRequestingProtocol();
String scheme = getRequestingScheme();
URL u = getRequestingURL();
RequestorType rtype = getRequestorType();
System.out.println("prompt:" + prompt);
System.out.println("hostname:" + hostname);
System.out.println("ipaddress:" + ipaddr);
System.out.println("port:" + port);
System.out.println("protocolo:" + protocol);
System.out.println("scheme:" + scheme);
System.out.println("URL:" + u);
System.out.println("Requester Type:" + rtype);
String username = "User123"; //hardcoded user
String password = "pwd123"; //hardcoded pwd
return new PasswordAuthentication(username, password.toCharArray());
}
}
我的问题是,我收到了错误:java.net.ProtocolException: Server redirected too many times (20),此时我尝试了很多东西,但我不知道更多。
你们能帮帮我吗? :(
非常感谢!
【问题讨论】:
-
你是什么意思(“如果你手动输入网址,它会自动为你下载。”)?你的意思是如果你尝试通过浏览器下载它?可能是您的浏览器中有会话 cookie?
-
很抱歉给您带来了困惑!是的,这就是我的意思。如果我把它放在浏览器中,它会自动下载。
-
你知道什么会有帮助吗?如果您可以在浏览器中打开开发人员工具栏--转到网络--> 清除所有内容--> 粘贴您的 URL 并单击 Enter --> 从您的网络选项卡中复制请求。我不知道请求是什么样的,所以我无能为力。您的浏览器可能会自动设置会话令牌,因为您有 cookie?您的浏览器可能正在发送授权令牌,因为您的用户名/密码已存储?很难帮助! :)
-
啊!我发现了错误!看来我在身份验证中遗漏了一些东西。我们的服务器管理员告诉我,我们需要在用户名之前添加一个域。它现在工作正常。感谢您的帮助! :)
标签: java file authentication url download