最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java Springboot如何基于圖片生成下載鏈接

 更新時間:2020年03月30日 10:20:41   作者:NemoWang  
這篇文章主要介紹了Java Springboot如何基于圖片生成下載鏈接,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

現(xiàn)有一些圖片在服務器上的鏈接,在瀏覽器中打開這些鏈接是直接顯示在瀏覽器頁面的形式。

現(xiàn)在需要生成這些圖片的單獨下載以及打包下載鏈接,即在瀏覽器中打開下載鏈接后彈出下載框提示下載。由于前端存在跨域問題,所以圖片下載由后臺接口完成。

首先編寫文件下載工具類:

import java.net.URL;
import java.net.MalformedURLException;
import org.apache.commons.io.FileUtils;

public class FileDownloadUtil {
/**
   * 下載文件---返回下載后的文件存儲路徑
   *
   * @param url 文件路徑
  * @param dir 目標存儲目錄
  * @param fileName 存儲文件名
  * @return
  */
  public static void downloadHttpUrl(String url, String dir, String fileName) throws BusinessException {
    try {
    URL httpurl = new URL(url);
    File dirfile = new File(dir);
      if (!dirfile.exists()) {
        dirfile.mkdirs();
      }
      FileUtils.copyURLToFile(httpurl, new File(dir+fileName));
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();26     }
  }

  public static boolean deleteFile(File file) {
    if (file.exists()) {
      return file.delete();
    }
    return false;
  }
}

單張圖片下載

Controller層接口:

import org.apache.commons.lang.StringUtils;
import java.io.*;


protected HttpServletResponse response;

/**
   * 單張圖片下載
   *
   * @param url 要下載的圖片url
   * @author: nemowang
   */
  @ApiImplicitParams({
      @ApiImplicitParam(name = "url", value = "圖片url", required = true, dataType = "String", paramType = "query"),
  })
  @ApiOperation(value = "單張圖片下載", notes = "單張圖片下載")
  @RequestMapping(value = "/downloadPicture", method = RequestMethod.GET)
  public void downloadPicture(String url) {
    
    // 拼接完整圖片路徑。這里填寫圖片鏈接
    String urlPath = "";

    // 獲取圖片文件后綴名
    String postfix = "." + StringUtils.substringAfterLast(url, ".");

    // 獲取當前類的所在項目路徑
    File directory = new File("");
    String courseFile;

    String srcPath;
    File srcFile = null;
    FileInputStream fileInputStream = null;
    InputStream fis = null;
    OutputStream out = null;
    try {
      courseFile = directory.getCanonicalPath();
      String fileName = "\\" + StringUtil.getUUID() + postfix;
      // 下載文件
      FileDownloadUtil.downloadHttpUrl(urlPath, courseFile, fileName);

      srcPath = courseFile + fileName;
      srcFile = new File(srcPath);

      fileInputStream = new FileInputStream(srcPath);
      fis = new BufferedInputStream(fileInputStream);
      byte[] buffer = new byte[fis.available()];
      fis.read(buffer);

      response.setContentType("application/octet-stream");
      response.setHeader("Content-disposition", "attachment;filename=" + fileName);
      out = response.getOutputStream();
      out.write(buffer);
      out.flush();
      out.close();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        if (fileInputStream != null) {
          fileInputStream.close();
        }
        if (fis != null) {
          fis.close();
        }
        if (out != null) {
          out.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    // 刪除中間文件
    if (srcFile != null) {
      System.out.println(FileDownloadUtil.deleteFile(srcFile));
    }
  }

因為是GET請求,所以直接拼接接口路由+參數(shù),用瀏覽器打開就能彈出下載。

至此單張圖片下載接口結束。

多張圖片打包下載

Controller層接口:

/**
   * 圖片打包下載
   */
  @ApiImplicitParams({
      @ApiImplicitParam(name = "urls", value = "圖片url列表", required = true, dataType = "List", paramType = "query"),
  })
  @ApiOperation(value = "圖片打包下載", notes = "圖片打包下載")
  @RequestMapping(value = "/downloadPictureList", method = RequestMethod.GET)
  public void downloadPictureList(List urls) {
    List<String> fileNameList = new ArrayList<>();

    for (int i = 0; i < urls.size(); i++) {
      // 獲取文件名
      fileNameList.add(StringUtils.substringAfterLast(urls.get(i), "/"));

      // 拼接完整圖片路徑
      urls.set(i, DOMAIN + urls.get(i));
    }

    // 獲取當前類的所在項目路徑
    File directory = new File("");
    String courseFile;

    String srcPath;
    File srcFile = null;

    // 要打包的文件列表
    List<File> fileList = new ArrayList<>();

    ZipOutputStream zos = null;
    OutputStream out = null;
    try {
      courseFile = directory.getCanonicalPath();

      // 下載文件
      for (int i = 0; i < urls.size(); i++) {
        String fileName = "\\" + fileNameList.get(i);
        FileDownloadUtil.downloadHttpUrl(urls.get(i), courseFile, fileName);
        srcPath = courseFile + fileName;
        srcFile = new File(srcPath);
        fileList.add(srcFile);
      }



      long start = System.currentTimeMillis();

      response.setContentType("application/x-zip-compressed");
      response.setHeader("Content-disposition", "attachment;filename=" + StringUtil.getUUID() + ".zip");
      out = response.getOutputStream();
      zos = new ZipOutputStream(out);
      for (File file : fileList) {
        byte[] buf = new byte[BUFFER_SIZE];
        zos.putNextEntry(new ZipEntry(file.getName()));
        int len;
        FileInputStream in = new FileInputStream(file);
        while ((len = in.read(buf)) != -1) {
          zos.write(buf, 0, len);
        }
        zos.closeEntry();
        in.close();
      }
      long end = System.currentTimeMillis();
      System.out.println("壓縮完成,耗時:" + (end - start) + " ms");


      out.flush();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (Exception e) {
        throw new RuntimeException("zip error from ZipUtils", e);
    } finally {
      if (zos != null) {
        try {
          zos.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if (out != null) {
        try {
          zos.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }

    // 刪除中間文件
    if (fileList != null) {
      for (File file : fileList) {
        System.out.println(FileDownloadUtil.deleteFile(file));
      }
    }
  }

同樣是GET請求,所以也是拼接接口路由+參數(shù),用瀏覽器打開就能彈出下載。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • java8新特性 獲取list某一列的操作

    java8新特性 獲取list某一列的操作

    這篇文章主要介紹了java8新特性 獲取list某一列的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • java對象轉型實例分析

    java對象轉型實例分析

    這篇文章主要介紹了java對象轉型的概念及用法,并以實例形式進行了較為詳細的介紹,需要的朋友可以參考下
    2014-10-10
  • Java中數(shù)組協(xié)變和范型不變性踩坑記錄

    Java中數(shù)組協(xié)變和范型不變性踩坑記錄

    數(shù)組的協(xié)變性來源于數(shù)組的一個優(yōu)勢,這篇文章主要給大家介紹了關于Java中數(shù)組協(xié)變和范型不變性踩坑的一些內容,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-02-02
  • 使用Java編寫一個好用的解析配置工具類

    使用Java編寫一個好用的解析配置工具類

    這篇文章主要為大家詳細介紹了如何使用Java編寫一個好用的解析配置工具類,支持解析格式有properties,yaml和yml,感興趣的可以了解下
    2024-11-11
  • SpringSecurity 默認表單登錄頁展示流程源碼

    SpringSecurity 默認表單登錄頁展示流程源碼

    本篇主要講解 SpringSecurity提供的默認表單登錄頁 它是如何展示流程,本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下吧
    2020-01-01
  • 使用spring boot 整合kafka,延遲啟動消費者

    使用spring boot 整合kafka,延遲啟動消費者

    這篇文章主要介紹了使用spring boot 整合kafka,延遲啟動消費者的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java concurrency集合之ConcurrentSkipListSet_動力節(jié)點Java學院整理

    Java concurrency集合之ConcurrentSkipListSet_動力節(jié)點Java學院整理

    這篇文章主要為大家詳細介紹了Java concurrency集合之ConcurrentSkipListSet的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Java數(shù)據(jù)結構之稀疏矩陣定義與用法示例

    Java數(shù)據(jù)結構之稀疏矩陣定義與用法示例

    這篇文章主要介紹了Java數(shù)據(jù)結構之稀疏矩陣定義與用法,結合實例形式分析了java稀疏矩陣的定義、運算、轉換等相關操作技巧,需要的朋友可以參考下
    2018-01-01
  • Netty內存池泄漏問題以解決方案

    Netty內存池泄漏問題以解決方案

    這篇文章主要介紹了Netty內存池泄漏問題以解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • JAVA集成本地部署的DeepSeek的圖文教程

    JAVA集成本地部署的DeepSeek的圖文教程

    本文主要介紹了JAVA集成本地部署的DeepSeek的圖文教程,包含配置環(huán)境變量及下載DeepSeek-R1模型并啟動,具有一定的參考價值,感興趣的可以了解一下
    2025-03-03

最新評論

蕲春县| 崇礼县| 田东县| 定襄县| 济源市| 北碚区| 洪雅县| 文登市| 林口县| 长治县| 广安市| 会理县| 平远县| 那曲县| 平和县| 临汾市| 水城县| 和顺县| 蓬溪县| 收藏| 曲麻莱县| 牡丹江市| 阿拉尔市| 昌邑市| 双江| 长白| 哈密市| 云阳县| 博野县| 泗水县| 萨迦县| 台南县| 乳山市| 连州市| 交城县| 资溪县| 武邑县| 金平| 辽阳市| 张家港市| 河池市|