zoukankan      html  css  js  c++  java
  • 根据图片URL地址下载到本地

    public static void main(String[] args) {
    String replace = "https://betapic.uhomecp.com/service/2020/12/09/843/202012091938511177_734_743.JPEG";
    String tempDirectoryPath = "/home/si/apache-tomcat-8.0.32/webapps/tempPath/";
    String[] split = replace.split("/");
    try {
    URL url = new URL(replace);
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    InputStream inputStream = conn.getInputStream();
    File file = new File( tempDirectoryPath+"/"+split[split.length-1]);
    System.out.println(file);
    FileItemFactory factory = new DiskFileItemFactory(16, null);//image/jpeg:jpg
    FileItem item=factory.createItem(file.getName(),"text/plain",true,file.getName());
    int bytesRead = 0;
    byte[] buffer1 = new byte[8192];
    // 下载文件到一个指定地方
    FileUtils.copyURLToFile(url, file);
    FileInputStream fis = new FileInputStream(file);
    OutputStream os = item.getOutputStream();
    while ((bytesRead = fis.read(buffer1, 0, 8192)) != -1) {
    os.write(buffer1, 0, bytesRead);
    }
    MultipartFile multipartFile = new CommonsMultipartFile(item);
    System.out.println("4.multipartFile.getName----------图片文件"+multipartFile.getName());
    os.close();
    fis.close();
    inputStream.close();
    if (file.exists()){
    if (file.isFile()){
    FileUtils.deleteQuietly(file);
    }

    }
    } catch (IOException e) {
    // TODO 自动生成的 catch 块
    e.printStackTrace();
    }
    }
  • 相关阅读:
    Linux守护进程
    sequel pro无法连接mysql服务器
    socket编程之并发回射服务器2
    Unix的I/O模型
    nginx.conf laravel 配置
    phpstudy使用PHP+nginx配置Laravel
    nginx配置文件分开配置
    centos安装composer
    linux下 设置php的环境变量 php: command not found
    laravel 安装
  • 原文地址:https://www.cnblogs.com/qhzyds/p/14119144.html
Copyright © 2011-2022 走看看