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

Url获取图片流并打包~的更多相关文章

  1. 通过http URL 获取图片流 转为字节数组

    通过http URL 获取图片流 转为字节数组 读取本地文件转为数组 /** * 获取 文件 流 * @param url * @return * @throws IOException */ pri ...

  2. kali之ARP欺骗获取图片流

    其实很简单,就两步: 1. 后接三个参数: 网卡:eth0    网关:10.0.0.1    攻击目标:10.0.0.128 2.启动监听 会弹出一个框 里面就会显示攻击目标通过浏览器访问的页面上的 ...

  3. JSSDK图像接口多张图片上传下载并将图片流写入本地

    <span style="font-size: 14px;"><!DOCTYPE html> <html lang="en"> ...

  4. 通过网络路径获取的图片 btye 图片流互转

    楼主有一个需要用户用的网站要上传图片,图片不保存到网站,而是要专门存放到一个图片服务器上面,于是需要通过byte的形式来传输 之前写的一个本地图片流转于byte互转 后来发现通过网络路径获取的图片这个 ...

  5. 根据URL获取图片

    背景:今天因为生产环境的系统界面图片无法显示被领导叼了一波,之前用Hutool工具类解析URL获取图片的,在生产环境上跑了一个多月都正常,嘣,今天突然发现周六下午后的图片统统显示异常,之后改为用jav ...

  6. 上传图片流到服务器(AFN方法) (多张图片)(图片流)

      上传图片流到服务器(AFN方法) (多张图片)(图片流) 第一步//获取图片 UIAlertController *actionSheet = [UIAlertController alertCo ...

  7. Android Bitmap和Drawable互转及使用BitmapFactory解析图片流

    一.Bitmap转Drawable Bitmap bmp=xxx; BitmapDrawable bd=new BitmapDrawable(bmp); 因为BtimapDrawable是Drawab ...

  8. vue显示后端传递的图片流

    一.显示部分(组件我使用的vuetify) <template> <v-container fluid> <v-card width="100%" m ...

  9. 解析URL 获取某一个参数值

    /** * 解析URL 获取某一个参数值 * * @param name 需要获取的字段 * @param webaddress URL * * @return 返回的参数对应的 value */ - ...

随机推荐

  1. [C++][数据结构]队列(queue)的实现

    对于队列的定义,前人之述备矣. 队列的实现方法与栈非常相似.我直接在我实现的那个栈的代码上加了一点东西,全局替换了一些标识符,就实现了这个队列. 我实现的是一个queue<value>容器 ...

  2. WebView

    WebView可以使得网页轻松的内嵌到app里,还可以直接跟js相互调用. webview有两个方法:setWebChromeClient 和 setWebClient setWebClient:主要 ...

  3. 打造自己的php动态连接库文件

    http://blog.163.com/weibin_li/blog/static/1901464172012325115517181/

  4. JavaScript(一)

    JavaScript 是脚本语言 JavaScript 是一种可插入 HTML 页面的轻量级的编程语言,它跟Java没有什么蛋关系. JavaScript使用: <script language ...

  5. What is a Database Trigger?

    Link: http://www.essentialsql.com/what-is-a-database-trigger/ Copy... What is a Database Trigger? A ...

  6. Linux 计划任务

    实例: 每5分钟定时访问一个url # crontab -e #*/5 * * * * /usr/bin/curl http://aa.com:8080/tools/sitemap.php >& ...

  7. react学习小结(生命周期- 实例化时期 - 存在期- 销毁时期)

    react学习小结   本文是我学习react的阶段性小结,如果看官你是react资深玩家,那么还请就此打住移步他处,如果你想给一些建议和指导,那么还请轻拍~ 目前团队内对react的使用非常普遍,之 ...

  8. mysql在线修改表结构大数据表的风险与解决办法归纳

    整理这篇文章的缘由: 互联网应用会频繁加功能,修改需求.那么表结构也会经常修改,加字段,加索引.在线直接在生产环境的表中修改表结构,对用户使用网站是有影响. 以前我一直为这个问题头痛.当然那个时候不需 ...

  9. Python实战:扫描key完整性

    之前在国际版本中,需要支持中英文切换功能,在如此繁多的源文件里要查找源文件里的key是语言资源包是否对应. 正好运用在之前学的python,写了个工具,支持自定义替换标签,批量处理源文件.现在看来,效 ...

  10. antmate.css

    本文各式各样的动画来源于http://daneden.github.io/animate.css/ ,若想看效果可复制运行下文,或到该网站自行查阅. <!DOCTYPE html> < ...