下载使用Java互联网的文件:如何鉴别? [英] Download a file from the internet using java : How to authenticate?

查看:101
本文介绍了下载使用Java互联网的文件:如何鉴别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于此线程<一个href=\"http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java\">http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java
我知道如何下载一个文件,我现在的问题是,我需要在我从中你下载断绝验证。这是一个HTTP接口Subversion服务器。我需要哪个领域仰望?

Thanks to this thread http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java I know how to download a file, now my problem is that I need to authenticate on the sever from which I'm dowloading. It's an http interface to a subversion server. Which field do I need to look up into ?

使用code贴在过去的评论,我得到这个异​​常:

Using the code posted in the last comment, I get this exception:

java.io.IOException异常:服务器返回的HTTP响应code:401网址: HTTP: //myserver/systemc-2.0.1.tgz
    在sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1305)
    在java.net.URL.openStream(URL.java:1009)
    在mypackage.Installer.installSystemc201(Installer.java:29)
    在mypackage.Installer.main(Installer.java:38)

谢谢,

推荐答案

您实施的身份验证类注册。在链路的javadoc解释如何。

You implement the Authenticator class and register it. The javadocs at the link explain how.

我不知道,如果这个工程与得到公认的问题的答案NIO的方法,但它肯定作品的老式方法,这是下一个问题的答案。

I don't know if this works with the nio method that got the accepted answer to the question, but it for sure works for the old fashioned way that was the answer under that one.

在验证器类的实现,你可能会使用一个<一个href=\"http://java.sun.com/j2se/1.5.0/docs/api/java/net/PasswordAuthentication.html\">PasswordAuthentication并重写你的鉴定者实施的getPasswordAut​​hentication()方法返回它。这将是被传递你所需要的用户名和密码的类。

Within the authenticator class implementation, you are probably going to use a PasswordAuthentication and override the getPasswordAuthentication() method of your Authenticator implementation to return it. That will be the class which is passed the user name and password you need.

按照您的要求,下面是一些示例code:

Per your request, here is some sample code:

public static final String USERNAME_KEY = "username";
public static final String PASSWORD_KEY = "password";
private final PasswordAuthentication authentication;

public MyAuthenticator(Properties properties) {
    String userName = properties.getProperty(USERNAME_KEY);
    String password = properties.getProperty(PASSWORD_KEY);
    if (userName == null || password == null) {
        authentication = null;
    } else {
        authentication = new PasswordAuthentication(userName, password.toCharArray());
    }
}

protected PasswordAuthentication getPasswordAuthentication() {
    return authentication;
}

和你的主要方法进行注册(或沿调用URL前行的地方):

And you register it in the main method (or somewhere along the line before you call the URL):

Authenticator.setDefault(new MyAuthenticator(properties));

用法很简单,但我发现这个API费解和实物向后你通常如何看待这些事情。 pretty典型的单设计的。

The usage is simple, but I find the API convoluted and kind of backwards for how you typically think about these things. Pretty typical of singleton design.

这篇关于下载使用Java互联网的文件:如何鉴别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆