首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Docker容器中Tomcat9/JDK8的Java Authenticator“服务器重定向次数过多”

Docker容器中Tomcat9/JDK8的Java Authenticator“服务器重定向次数过多”
EN

Stack Overflow用户
提问于 2020-07-14 01:00:02
回答 1查看 104关注 0票数 0

我正在下载一个pdf文件从一个固定的网址(用于测试),这是一个共享的list.When运行的代码在一个独立的VM基于tomcat9,它显示的pdf,但如果运行相同的工作代码从Docker的tomcat9,然后它的给予服务器重定向太多次(20)。下面是代码片段-

代码语言:javascript
复制
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)”错误

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2020-07-18 21:47:18

使用Java nio让它工作library.Below是测试代码,如果它对某人有帮助的话。

代码语言:javascript
复制
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();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62880685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档