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();
}
}
Url获取图片流并打包~的更多相关文章
- 通过http URL 获取图片流 转为字节数组
通过http URL 获取图片流 转为字节数组 读取本地文件转为数组 /** * 获取 文件 流 * @param url * @return * @throws IOException */ pri ...
- kali之ARP欺骗获取图片流
其实很简单,就两步: 1. 后接三个参数: 网卡:eth0 网关:10.0.0.1 攻击目标:10.0.0.128 2.启动监听 会弹出一个框 里面就会显示攻击目标通过浏览器访问的页面上的 ...
- JSSDK图像接口多张图片上传下载并将图片流写入本地
<span style="font-size: 14px;"><!DOCTYPE html> <html lang="en"> ...
- 通过网络路径获取的图片 btye 图片流互转
楼主有一个需要用户用的网站要上传图片,图片不保存到网站,而是要专门存放到一个图片服务器上面,于是需要通过byte的形式来传输 之前写的一个本地图片流转于byte互转 后来发现通过网络路径获取的图片这个 ...
- 根据URL获取图片
背景:今天因为生产环境的系统界面图片无法显示被领导叼了一波,之前用Hutool工具类解析URL获取图片的,在生产环境上跑了一个多月都正常,嘣,今天突然发现周六下午后的图片统统显示异常,之后改为用jav ...
- 上传图片流到服务器(AFN方法) (多张图片)(图片流)
上传图片流到服务器(AFN方法) (多张图片)(图片流) 第一步//获取图片 UIAlertController *actionSheet = [UIAlertController alertCo ...
- Android Bitmap和Drawable互转及使用BitmapFactory解析图片流
一.Bitmap转Drawable Bitmap bmp=xxx; BitmapDrawable bd=new BitmapDrawable(bmp); 因为BtimapDrawable是Drawab ...
- vue显示后端传递的图片流
一.显示部分(组件我使用的vuetify) <template> <v-container fluid> <v-card width="100%" m ...
- 解析URL 获取某一个参数值
/** * 解析URL 获取某一个参数值 * * @param name 需要获取的字段 * @param webaddress URL * * @return 返回的参数对应的 value */ - ...
随机推荐
- 移动端横屏(beta)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java数组技巧攻略
Java数组技巧攻略 0. 声明一个数组(Declare an array) String[] aArray = new String[5]; String[] bArray = {" ...
- 【随笔】MQTT简介
我们知道,将用户使用的设备称为客户端,将提供给用户信息的端口称为服务器端.两个端口之间可以通过多种通信协议进行交互,比如HTTP(同步)或者基于消息传递的异步. HTTP是一种同步无状态的协议,不支持 ...
- Listener refused the connection with the following error 错误解决
原文地址 :http://blog.csdn.net/zajin/article/details/17753351 做个备份: 查询数据库当前进程的连接数: select count(*) from ...
- js获取?后面具体参数的值
function getURLParam(name) { return decodeURIComponent((new RegExp('[?|&]' + name + '=' ...
- 基于FS4412的DS18B20温度采集编程实现(1-时序分析)
作者:秦老师,华清远见嵌入式学院讲师. 一.DS18B20简介 DS18B20是常用的数字温度计.DS18B20数字温度计提供9至12位(可配置)温度读数,表明该设备的温度. 信息通过单总线接口被发送 ...
- MongoDB命令及SQL语法对比
mongodb与mysql命令对比 传统的关系数据库一般由数据库(database).表(table).记录(record)三个层次概念组成,MongoDB是由数据库(database).集合(col ...
- Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- java抽象类
Java 抽象类 在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类. 抽象类除了不 ...
- java中接口的定义和接口的实现
1.接口的定义 使用interface来定义一个接口.接口定义同类的定义类似,也是分为接口的声明和接口体,其中接口体由常量定义和方法定义两部分组成.定义接口的基本格式如下: [修饰符] interfa ...