• 根据图片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();
    }
    }
  • 相关阅读:
    nginx服务与nfs服务
    linux基础(3)
    Linux基础(4)
    Linux命令基础(2)
    Linux命令基础(1)
    HTML——表单验证、正则表达式、事件
    css修改鼠标指针的形状
    ajax请求tab切换重新渲染Echarts图表
    5种状态下的HTTP状态码
    vue&Angular&React的优缺点
  • 原文地址:https://www.cnblogs.com/qhzyds/p/14119144.html
Copyright © 2020-2023  润新知