我试图将java代码中的文件作为流来获取。 URI正在返回PDF文件。以下是我的代码段:
public void downloadToLocal() {
try{
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("test",
"test".toCharArray());
}
});
URL url = new URL("http://***");
/*String encodedAuthorizedUser = new String(com.sun.jersey.core.util.Base64.encode("***" + ":" + "***"));
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setRequestProperty("Authorization", "Basic " + encodedAuthorizedUser);*/
InputStream is = url.openStream();
FileOutputStream fos = new FileOutputStream("C:\\db\\test.pdf");
byte[] buffer = new byte[1024];
int noOfBytes = 0;
while ((noOfBytes = is.read(buffer)) != -1) {
fos.write(buffer, 0, noOfBytes);
}
fos.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
以下是我得到的例外情况:
java.io.FileNotFoundException: Response: '401: Unauthorized' for url: '*********'
at weblogic.net.http.HttpURLConnection.getInputStream(HttpURLConnection.java:487)
at weblogic.net.http.SOAPHttpURLConnection.getInputStream(SOAPHttpURLConnection.java:37)
at java.net.URL.openStream(URL.java:1010)
at com.bt.ngwfmt.framework.services.dlite.DliteServiceClient.downloadToLocal(DliteServiceClient.java:882)
at com.bt.ngwfmt.framework.rest.DliteRestHandler.persistDCV(DliteRestHandler.java:167)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:183)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3717)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:178)
来自here我正在参考。我从那里尝试了两种方法。我也发现了类似的问题here。但是,我无法获得跳过此密码身份验证所需的操作。当我在浏览器中访问url并手动提供用户名和密码时,这是有效的。
任何帮助都将受到高度赞赏。 :)
注意:我使用的是weblogic 10.3.5。感谢
答案 0 :(得分:2)
看起来像Weblogic特定的问题与默认验证器
尝试显式设置auth标头:
URLConnection conn = url.openConnection();
String creds = user + ":" + pass;
String encodedCreds = "Basic " + new String(new Base64().encode(creds.getBytes()));
conn.setRequestProperty ("Authorization", encodedCreds);
InputStream in = conn.getInputStream();
答案 1 :(得分:1)
这是因为WebLogic使用自己的HttpURLConnection实现,并且遇到了一些问题。
另一个解决方案是强制Weblogic使用标准的Sun的HttpURLConnection
将选项 UseSunHttpHandler 设置为true