从经过身份验证的服务器下载带有java的大文件

时间:2018-05-18 18:29:21

标签: java authentication fileutils

我需要实现从经过身份验证的服务器下载多个文件的功能。但是我收到了以下结果,即使通过浏览器访问也能正常下载(如果经过身份验证)。

  

'服务器返回HTTP响应代码:500'

文件大小应介于7.10 KB和 4.8 GB之间

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;

public class Example {

    private static final String URL = "totally_a_valid_url";
    private static final String PATH = "D:\\___Automation\\_TEST";

    public static void main(String[] args) throws IOException {

        Authenticator.setDefault(new MyAuthenticator());

        URL url = new URL(URL);
        File destination = new File(PATH);
        FileUtils.copyURLToFile(url, destination);

    }

    public static class MyAuthenticator extends Authenticator {

        String uName = "not_my_real_usr";
        String uPass = "not_my_password";

        PasswordAuthentication authentication;

        MyAuthenticator() {
            authentication = new PasswordAuthentication(uName, uPass.toCharArray());
        }

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return authentication;
        }

    }

}

0 个答案:

没有答案