因为公司项目需求,做一个所有数据以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(); } }