zoukankan      html  css  js  c++  java
  • Url获取图片流并打包~

    因为公司项目需求,做一个所有数据以excle的格式汇出,其中包括了图片。

    而数据库保存的是图片的url.

    自己捣鼓的代码.

    imageFile的类

    public class ImageFile {
        /**
         * 图片url
         */
        private String Path;
        /**
         * 图片名字
         */
        private String FileName;
        /**
         * 图片编号
         */
        private String CustomerNo;
    
        public ImageFile(String path, String FileName, String CustomerNo) {
            this.Path = path;
            this.FileName = FileName;
            this.CustomerNo = CustomerNo;
        }
    
        public ImageFile() {
    
        }
    
        public String getPath() {
            return Path;
        }
    
        public void setPath(String path) {
            Path = path;
        }
    
        public String getFileName() {
            return FileName;
        }
    
        public void setFileName(String fileName) {
            FileName = fileName;
        }
    
        public String getCustomerNo() {
            return CustomerNo;
        }
    
        public void setCustomerNo(String customerNo) {
            CustomerNo = customerNo;
        }
    
    }

    工具类

    package com.bnuz.utils;
    
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
    
    import org.apache.tomcat.util.http.fileupload.IOUtils;
    
    import com.bnuz.domain.ImageFile;
    
    public class Utils {
        /**
         * 
         * @param zip
         * @param file
         * @param index
         * @return index 当图片名字为空时,以index命名图片
         * @throws IOException
         */
        public static int ZipImage(ZipOutputStream zip, ImageFile file,
                String type, int index) throws IOException {
            // String fileName = StringUtils.isNotBlank(file.getFileName()) ? file
            // .getFileName() : index + "";
            String fileName = "";
            if (file.getFileName() == null || file.getFileName().trim().equals("")) {
                fileName = index + "";
            }
            ZipEntry entry = new ZipEntry(file.getCustomerNo() + "/" + fileName
                    + "_" + type + ".jpg");
            zip.putNextEntry(entry);
            InputStream in = loadImageInputStream(file);
            IOUtils.copy(in, zip);
            zip.closeEntry();
            index++;
            return index;
        }
    
        /**
         * 
         * @param imageFile
         * @return 
         */
        public static InputStream loadImageInputStream(ImageFile imageFile) {
            URL url;
            InputStream dataInputStream = null;
            try {
                url = new URL(imageFile.getPath());
                dataInputStream = new DataInputStream(url.openStream());
                dataInputStream.close();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return dataInputStream;
        }
    }

    因为是自己写的玩意,所以就随便弄了~

    package com.bnuz.zip;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.zip.ZipOutputStream;
    
    import com.bnuz.domain.ImageFile;
    import com.bnuz.utils.Utils;
    
    public class Zip {
        public void toZip() throws IOException {
            FileOutputStream out = new FileOutputStream("D:/sb.zip");
            String url = "http://cnc.ef-cdn.com/_imgs/lp/cn/2016yr/mobile/template/billboard/billboard-fly.jpg";
            String fileName = "";
            String customerNo = "sb";
            try {
                ZipOutputStream zip = new ZipOutputStream(out);
                List<ImageFile> photoList = new ArrayList<ImageFile>();
                ImageFile logo = new ImageFile(url, fileName, customerNo);
                ImageFile photo = new ImageFile(url, fileName, customerNo);
                photoList.add(photo);
                photoList.add(photo);
                photoList.add(photo);
                int index = 1;
                for (ImageFile file : photoList) {
                    index = Utils.ZipImage(zip, file, "en", index);
                    // index = ZipImage(zip, file, "cn", index);
                }
                index = Utils.ZipImage(zip, logo, "logo", index);
    
                zip.close();
                out.flush();
            } catch (Exception e) {
                System.out.println("??");
            } finally {
                out.close();
            }
        }
    
        
        public static void main(String[] args) throws IOException {
            Zip a = new Zip();
            a.toZip();
        }
    }
  • 相关阅读:
    从关系型数据库到非关系型数据库
    2016某知名互联网公司PHP面试题及答案
    企业网站核心关键词如何去选择
    写Seo网站标题应该注意什么
    什么样的外链才是优质外链
    什么是网站物理链接结构
    需要分析竞争对手的网站哪些SEO数据
    做外链的时候应该需要注意什么
    描述标签对关键词排名有影响吗
    网站外链对排名的影响有哪些
  • 原文地址:https://www.cnblogs.com/YYfish/p/6241542.html
Copyright © 2011-2022 走看看