我想在java代碼中獲取一個文件作爲流。該URI正在返回一個PDF文件。下面是我的代碼片段:從JAX-RS流式傳輸pdf文件響應:'401:Unauthorized'for url
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。由於
您還沒有一個正確的方式使用身份驗證。檢查這個http://stackoverflow.com/questions/496651/connecting-to-remote-url-which-requires-authentication-using-java –
這種身份驗證的方式通常是正確的。這個問題在WebLogic中。 –