【问题标题】:Java Authenticator "Server redirected too many times" from Tomcat9/JDK8 in Docker container来自 Docker 容器中 Tomcat9/JDK8 的 Java Authenticator“服务器重定向太多次”
【发布时间】:2020-07-13 17:00:02
【问题描述】:

我正在从共享点列表的固定 URL(用于测试)下载 pdf 文件。在基于独立 VM 的 tomcat9 上运行代码时,它显示 pdf,但如果从基于 Docker 的 tomcat9 运行相同的工作代码,则它给服务器重定向太多次(20)。 下面是代码段-

String attachtmentname = "https://<full url>";
String file_name = "<file>.pdf";
String Userid = "<some id>";
String password = "<some password>";
response.setHeader("Content-type", "application/pdf");
response.setHeader("ReportPDF", "content-description");
response.setHeader("Content-Disposition", "inline; filename=" + file_name);
response.setHeader("Cache-Control", "cache,must-revalidate");
System.setProperty("http.proxyUser", Userid);
System.setProperty("http.proxyPassword", password);
Authenticator.setDefault(new Authenticator() {
            
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(Userid, password.toCharArray());
                }
            });
            CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
BufferedInputStream in = new BufferedInputStream(new URL(attachtmentname).openStream());
<rest of the code for reading pdf file>

出现“服务器重定向次数过多 (20)”错误

java.net.ProtocolException: Server redirected too many  times (20)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1908)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:268)
    at java.net.URL.openStream(URL.java:1068)

【问题讨论】:

    标签: docker https java-8 tomcat9 authenticator


    【解决方案1】:

    使用 Java nio 库让它工作。下面是测试代码,如果它对某人有帮助。

    String attachtmentname = "https://full url/somefile.pdf";
    String Userid = "domain\\username";
    String password = "password"; 
    
            Authenticator.setDefault(new Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(Userid, password.toCharArray());
                    }
                });
    URL url = new URL(attachtmentname);
    try (InputStream in = url.openStream()) {
        Path pwd = Paths.get("Somepath\\someFile1.pdf").toAbsolutePath();
        out.println(pwd);
        
      Files.copy(in, pwd, StandardCopyOption.REPLACE_EXISTING);
    } catch (Exception e) {
       e.printStackTrace();
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-19
      • 2018-11-22
      • 2012-08-23
      • 2013-09-21
      • 2023-03-25
      • 2015-02-16
      • 1970-01-01
      • 2012-06-16
      • 1970-01-01
      相关资源
      最近更新 更多