编程语言
首页 > 编程语言> > java根据url下载文件,并且解决资源验证需要用户名密码问题

java根据url下载文件,并且解决资源验证需要用户名密码问题

作者:互联网

今天接到一个任务,根据jar包清单,去maven仓库中下载jar包和pom文件,并且按照仓库里的目录结构保存。
本来用python可以很好的解决这项任务,不过我还没有用过java下载文件,想着学习一下。

整体流程

  1. 读jar包清单文件,获取信息,拼接成url
  2. 添加用户名密码认证
  3. 根据ur获取到文件输入流
  4. 根据jar信息,创建目录,通过输出流把文件保存在指定目录。

代码部分

此处代码太多,可直接拉到下方看关键代码,是一样的

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class MavenDownload {

    private static final String EXCEL_PATH = "excel文件路径";
    private static final String URL_HEAD = "http://nexus.****.com.cn/repository/maven-releases/";
    private static final String DIRECTORY_HEAD = "保存jar的目录";
    private static final String USERNAME_PASSWORD = "用户名" + ":" + "密码";
    // 编码用户名和密码,拼接成认证信息
    private static final String AUTHOR = "Basic " + Base64.getEncoder().encodeToString(USERNAME_PASSWORD.getBytes());

    public static void main(String[] args) throws IOException {
    /*...拼接出下载连接*/
        Map<String, String> urlMap = new HashMap<>();
        urlMap.put("jar", jarUrl);
        urlMap.put("pom", pomurl);
        for (String key : urlMap.keySet()) {
            InputStream inputStream = existUrl(urlMap.get(key));
            if (!Objects.isNull(inputStream)) {
                download(inputStream, path, fileName + "." + key);
            } else {
                System.out.println("资源不存在: " + urlMap.get(key));
            }
        }
    }

    public static void download(String url, String directory, String fileName) throws IOException {
        File path = new File(DIRECTORY_HEAD + directory);
        if (!path.exists()) {
            path.mkdirs();
        }
        File file = new File(DIRECTORY_HEAD + directory + fileName);

        URL url1 = new URL(url);
        InputStream inputStream = null;
        try (
                FileOutputStream fileOutputStream = new FileOutputStream(file, false);
        ) {
            URLConnection urlConnection = url1.openConnection();
            urlConnection.setRequestProperty("Authorization", AUTHOR);
            urlConnection.connect();
            inputStream = urlConnection.getInputStream();
            byte[] bytes = new byte[1024];
            int count = 0;
            while ((count = inputStream.read(bytes)) != -1) {
                fileOutputStream.write(bytes, 0, count);
            }
            System.out.println("资源下载完成: " + directory + ":" + fileName);
        } catch (Exception e) {
            System.out.println("下载过程中出错: " + directory + ":" + fileName);
        } finally {
            if (!Objects.isNull(inputStream)) {
                inputStream.close();
            }
        }
    }

    /**
     * inputStream:文件下载的输入流
     * directory:文件的目录
     * fileName:文件名称
     */
    public static void download(InputStream inputStream, String directory, String fileName) throws IOException {
    	// 根据仓库目录结构创建文件夹
        File path = new File(DIRECTORY_HEAD + directory);
        if (!path.exists()) {
            path.mkdirs();
        }
        File file = new File(DIRECTORY_HEAD + directory + fileName);
        // 不需要创建文件,文件输出流找不到文件,会自动创建新文件

        try (
                FileOutputStream fileOutputStream = new FileOutputStream(file, false);
        ) {
            byte[] bytes = new byte[1024];
            int count = 0;
            while ((count = inputStream.read(bytes)) != -1) {
                fileOutputStream.write(bytes, 0, count);
            }
            System.out.println("资源下载完成: " + directory + ":" + fileName);
        } catch (Exception e) {
            System.out.println("下载过程中出错: " + directory + ":" + fileName);
        } finally {
            if (!Objects.isNull(inputStream)) {
                inputStream.close();
            }
        }
    }

	// 判断url资源是否存在,有的jar包在仓库里不存在,不存在的就略过,存在返回输入流
    private static InputStream existUrl(String url) {
        try {
            URL u = new URL(url);
            URLConnection urlConnection = u.openConnection();
            urlConnection.setRequestProperty("Authorization", AUTHOR);
            urlConnection.connect();
            InputStream inputStream = urlConnection.getInputStream();
            return inputStream;
        } catch (Exception e) {
            return null;
        }
    }
}

关键代码

用户名密码认证

注意:"Basic "这里有一个空格,忘记会认证失败。

private static final String USERNAME_PASSWORD = "用户名" + ":" + "密码";
// 编码用户名和密码,拼接成认证信息
private static final String AUTHOR = "Basic " + Base64.getEncoder().encodeToString(USERNAME_PASSWORD.getBytes());

从url到获取文件输入流的过程
	URL u = new URL(url);
	URLConnection urlConnection = u.openConnection();
	urlConnection.setRequestProperty("Authorization", AUTHOR);
	urlConnection.connect();
	InputStream inputStream = urlConnection.getInputStream();

文件保存

不用执行创建文件的操作,输出流如果找不到文件,会自动创建文件,但是目录要主动创建,输出流找不到目录会报错,所以在创建输出流之前,要确保目录存在。
创建输出流的第二个参数false,表示如果此文件存在,则从文件头部重新写入而不是追加,简单说就是覆盖已存在文件。

File path = new File(DIRECTORY_HEAD + directory);
if (!path.exists()) {
    path.mkdirs();
}

FileOutputStream fileOutputStream = new FileOutputStream(file, false);

标签:urlConnection,用户名,java,String,url,inputStream,static,directory,new
来源: https://blog.csdn.net/Java_web12138667/article/details/120713131