将图片打成压缩包,并转换为byte[]

package com.util;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;


public class Tupload {
    public static void main(String[] args) {
        Tupload upload = new Tupload();
        String pathName = "E:/exportFile/missyou/";//文件存放路径
        String zipName = "xiaoshasha_1";//压缩包名称
        String encoding = "UTF-8";//编码格式,处理中文文件名乱码问题
        String filePath = pathName + zipName + "/";
        List<BufferedInputStream> listBis = upload.getInputStream();//根据数据库存放图片的路径,生成inpuStream流
        upload.outputStream(listBis, filePath);//根据生成的inputstream流,输出到硬盘的某个文件夹下
        upload.fileToZip(pathName, zipName, encoding);//将多个文件压缩为ZIP
        byte[] byteZIP = upload.getbyte(pathName + zipName + ".zip");//获取ZIP的流 
        upload.outbyte(byteZIP);//测试byteZIP的流是否正确
        
        //暂时注释
        // upload.deleteFileDirectory(filePath);//删除自动生成的文件。
        //upload.deleteFileDirectory(pathName + zipName + ".zip");  //删除压缩包
        // 
        // 程序大概思路: 
        // 1.通过数据库存放人员的信息得到源图片的存放路径及图片名,
        // 2.通过源图片的路径及图片名生成一个临时文件夹(放入用户多张照片)
        // ,文件夹的名称要确保不能重复(可以考虑有规律的生成)。
        // 3.将临时文件夹打成压缩包.
        // 4.将压缩包转成第三方系统需要的byte[]字节.
        // 5.将临时文件删除(源文件夹及zip压缩包)
        // 6.不建议你将文件夹 img 建到web-inf下,会威胁到整个项目的源代码,避免造成不必要的风险.。
        // 建议模仿源图片的路径放到服务器的某个路径下


    }


    public void deleteFileDirectory(String path) {
        File dir = new File(path);
        if (dir.isDirectory()) {
            File[] children = dir.listFiles();
            for (int i = 0; i < children.length; i++) {
                deleteFileDirectory(children[i].getPath());
            }
        }
        dir.delete();
    }

    public byte[] getbyte(String zipFilePath) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        BufferedInputStream bis = null;
        FileInputStream in = null;
        try {
            File zipfile = new File(zipFilePath);
            in = new FileInputStream(zipfile);
            bis = new BufferedInputStream(in);
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = bis.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            bis.close();
            in.close();
            return out.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null)
                    bis.close();
                if (in != null)
                    in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;


    }


    public boolean fileToZip(String pathName, String zipName, String encoding) {
        boolean result = true;
        try {
            File pathFileName = new File(pathName + "/" + zipName);
            if (!pathFileName.exists()) {
                pathFileName.mkdirs();
            }
            File zipFile = new File(pathName + "/" + zipName + ".zip");
            if (zipFile.exists()) {
                zipFile.delete();
            }
            FileOutputStream fos = new FileOutputStream(zipFile);
            ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(
                    fos));
            // 添加编码,如果不添加,当文件以中文命名的情况下,会出现乱码
            // ZipOutputStream的包一定是apache的ant.jar包。JDK也提供了打压缩包,但是不能设置编码
            zos.setEncoding(encoding);// gbk
            FileInputStream fis = null;
            BufferedInputStream bis = null;


            File[] files = pathFileName.listFiles();
            byte[] by;
            if (files != null && files.length > 0) {
                for (File f : files) {
                    zos.putNextEntry(new ZipEntry(f.getName()));
                    fis = new FileInputStream(f);
                    bis = new BufferedInputStream(fis, 1024);
                    by = new byte[1024];
                    int len;
                    while ((len = bis.read(by, 0, 1024)) != -1) {
                        zos.write(by, 0, len);
                    }
                    zos.flush();
                    bis.close();
                    fis.close();
                }
                zos.flush();
                zos.close();
            }


        } catch (FileNotFoundException e) {
            result = false;
        } catch (IOException e) {
            result = false;
        }


        return result;
    }


    public List<BufferedInputStream> getInputStream() {
        File fileOne = new File("E:/exportFile/123.bmp");
        File fileFour = new File("E:/exportFile/456.bmp");
        List<BufferedInputStream> listBis = new ArrayList<BufferedInputStream>();
        FileInputStream inputOne = null;
        FileInputStream inputFour = null;
        BufferedInputStream bisOne = null;
        BufferedInputStream bisFour = null;
        try {
            inputOne = new FileInputStream(fileOne);
            inputFour = new FileInputStream(fileFour);
            bisOne = new BufferedInputStream(inputOne);
            bisFour = new BufferedInputStream(inputFour);


            listBis.add(bisOne);
            listBis.add(bisFour);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


        return listBis;
    }


    public void outputStream(List<BufferedInputStream> listBis, String filePath) {
        for (int i = 0; i < listBis.size(); i++) {
            BufferedInputStream fis = (BufferedInputStream) listBis.get(i);
            try {
                File fileZip = new File(filePath);
                if (!fileZip.exists()) {
                    fileZip.mkdirs();
                }
                FileOutputStream fot = new FileOutputStream(new File(filePath
                        + i + ".bmp"));//输出图片文件名称 要根据实际情况来修改
                BufferedOutputStream bos = new BufferedOutputStream(fot);
                int len;
                byte[] by = new byte[1024];
                try {
                    while ((len = fis.read(by, 0, 1024)) != -1) {
                        bos.write(by, 0, len);
                    }
                    bos.flush();
                    bos.close();
                    fot.close();
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }


    }


    public void outbyte(byte[] byteZIP) {
        FileOutputStream out;
        try {
            File outFile = new File("E:/exportFile/missYouxiaoshasha/");
            if (!outFile.exists()) {
                outFile.mkdirs();
            }
            out = new FileOutputStream(outFile + "/xiaojianjian_1.zip");
            out.write(byteZIP);
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值