• 批量下载网络图片并zip打包


    该功能分为三步:

    先把图片下载到本地的一个文件夹

    public  void download(String urlString, String filename,String savePath) throws Exception {
    // 构造URL
    URL url = new URL(urlString);
    // 打开连接
    URLConnection con = url.openConnection();
    //设置请求超时为5s
    con.setConnectTimeout(5 * 1000);
    // 输入流
    InputStream is = con.getInputStream();

    // 1K的数据缓冲
    byte[] bs = new byte[1024];
    // 读取到的数据长度
    int len;
    // 输出的文件流
    File sf=new File(savePath);
    if(!sf.exists()){
    sf.mkdirs();
    }
    OutputStream os = new FileOutputStream(sf.getPath()+"\"+filename);
    // 开始读取
    while ((len = is.read(bs)) != -1) {
    os.write(bs, 0, len);
    }
    // 完毕,关闭所有链接
    os.close();
    is.close();
    }

    打包为zip
    @RequestMapping(value = "/downZip")
    public void downZip(String tcLwIds,HttpServletResponse response) throws Exception {
    List<File> files = new ArrayList<File>();
    List<String>pathDb=new ArrayList<>();
    pathDb.add("group1/M00/00/73/wKgCCFkC56OAbvweAABrRuCppJE685.png");
    pathDb.add("group1/M00/00/73/wKgCCFkC56OAYwToAAAuJ6ZQ3Lc233.png");
    pathDb.add("group1/M00/00/73/wKgCCFkC56OAQTGpAAAm_2X7-GM329.png");
    pathDb.add("group1/M00/00/5D/wKgCD1kC6FCAZxwSAAAm_2X7-GM372.png");
    for(int m=0;m<pathDb.size();m++){
    String filePath=STORAGE_SERVER_URL+pathDb.get(m);
    String fileName=pathDb.get(m).split("/")[4];
    String path=test5.class.getResource("/").getFile();
    download(filePath, fileName,path);
    File file = new File(path+fileName);
    files.add(file);
    }

    String fileName = UUID.randomUUID().toString() + ".zip";
    //在服务器端创建打包下载的临时文件
    String outFilePath = test5.class.getResource("/").getFile();
    createFile(outFilePath,fileName);
    File file = new File(outFilePath+fileName);
    //文件输出流
    FileOutputStream outStream = new FileOutputStream(file);
    //压缩流
    ZipOutputStream toClient = new ZipOutputStream(outStream);
    // toClient.setEncoding("gbk");
    zipFile(files, toClient);
    toClient.close();
    outStream.close();
    this.downloadFile(file, response, true);
    //删除本地文件
    for(File path:files) {
    path.delete();
    }
    }

    //创建文件
    public void createFile(String path,String fileName){
    //path表示你所创建文件的路径
    File f = new File(path);
    // fileName表示你创建的文件名;为txt类型;
    File file = new File(f,fileName);
    if(!file.exists()){
    try {
    file.createNewFile();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }


    }
    /** 压缩文件列表中的文件
    * @param files
    * @param outputStream
    * @throws IOException
    */
    public static void zipFile(List<File> files, ZipOutputStream outputStream) throws IOException,ServletException
    {
    try {
    int size = files.size();
    // 压缩列表中的文件
    for (int i = 0; i < size; i++) {
    File file =files.get(i);
    zipFile(file, outputStream);
    }
    } catch (IOException e) {
    throw e;
    }
    }
    /** 将文件写入到zip文件中
    *
    * @param inputFile
    * @param outputstream
    * @throws Exception
    */
    public static void zipFile(File inputFile, ZipOutputStream outputstream) throws IOException,ServletException
    {
    try {
    if (inputFile.exists()) {
    if (inputFile.isFile()) {
    FileInputStream inStream = new FileInputStream(inputFile);
    BufferedInputStream bInStream = new BufferedInputStream(
    inStream);
    ZipEntry entry = new ZipEntry(inputFile.getName());
    outputstream.putNextEntry(entry);


    final int MAX_BYTE = 10 * 1024 * 1024; // 最大的流为10M
    long streamTotal = 0; // 接受流的容量
    int streamNum = 0; // 流需要分开的数量
    int leaveByte = 0; // 文件剩下的字符数
    byte[] inOutbyte; // byte数组接受文件的数据


    streamTotal = bInStream.available(); // 通过available方法取得流的最大字符数
    streamNum = (int) Math.floor(streamTotal / MAX_BYTE); // 取得流文件需要分开的数量
    leaveByte = (int) streamTotal % MAX_BYTE; // 分开文件之后,剩余的数量


    if (streamNum > 0) {
    for (int j = 0; j < streamNum; ++j) {
    inOutbyte = new byte[MAX_BYTE];
    // 读入流,保存在byte数组
    bInStream.read(inOutbyte, 0, MAX_BYTE);
    outputstream.write(inOutbyte, 0, MAX_BYTE); // 写出流
    }
    }
    // 写出剩下的流数据
    inOutbyte = new byte[leaveByte];
    bInStream.read(inOutbyte, 0, leaveByte);
    outputstream.write(inOutbyte);
    outputstream.closeEntry(); // Closes the current ZIP entry
    // and positions the stream for
    // writing the next entry
    bInStream.close(); // 关闭
    inStream.close();
    }
    } else {
    throw new ServletException("文件不存在!");
    }
    } catch (IOException e) {
    throw e;
    }
    }


    删除存在本地的文件

    for(File path:files) {
    path.delete();
    }

    参考:http://takeme.iteye.com/blog/1683380
    http://bbs.csdn.net/topics/340174173
    http://www.aiisen.com/java-batch-download-photos-zh.html
  • 相关阅读:
    waitkey();
    一、数组--删除排序数组中的重复项
    core组件进阶
    opencv数据结构与基本绘图
    highGUI图形用户界面
    合并两个有序链表
    字符串
    内存
    9位运算
    8设备文件
  • 原文地址:https://www.cnblogs.com/zjn0zjn/p/6797213.html
Copyright © 2020-2023  润新知