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 */ - ...
随机推荐
- 【Mybatis架构】输入、输出映射
前言综述: 其实在我们分析Mybatis的查询缓存或者是一些简介的时候,我们就不难看到有关于Mybatis输入输出映射的东西,比如说: 但是一直没有想起来系统的来总结一下这方面的相关知识,偶然看到 ...
- File类基础
File类的作用: Java的io包中定义了File类,用于对文件或文件夹的管理操作. File类只能够用于表示文件或文件夹的信息(属性)和对该文件或文件夹的删除创建操作 (不能对内容进行访问) 通过 ...
- hdu1561 The more, The Better (树形dp+背包)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1561 思路:树形dp+01背包 //看注释可以懂 用vector建树更简单. 代码: #i ...
- ZZULI 1876: 蛤玮的项链 Hash + 二分
Time Limit: 6 Sec Memory Limit: 128 MBSubmit: 153 Solved: 11 SubmitStatusWeb Board Description 蛤玮向 ...
- android解析json
android2.3提供的json解析类 android的json解析部分都在包org.json下,主要有以下几个类: JSONObject:可以看作是一个json对象 JSONStringer:js ...
- SOAPUI使用教程-验证SOAP服务
当soapUI创建一个功能性TestCase 一个很常见的场景是你想一些SOAP / WSDL服务验证响应检查返回正确的结果. 一旦你导入了您想要测试的WSDL服务这样做很容易: 添加一个新的SOAP ...
- Prototypes in Javascript 收集.__proto__
It’s important to understand that a function’s prototype property has nothing to do with it’s actual ...
- NOI 题库 6264
6264 走出迷宫 描述 当你站在一个迷宫里的时候,往往会被错综复杂的道路弄得失去方向感,如果你能得到迷宫地图,事情就会变得非常简单. 假设你已经得到了一个n*m的迷宫的图纸,请你找出从起点到出口的 ...
- Python中输出格式化的字符串
在Python中,采用的格式化方式和C语言是一致的,用%实现,举例如下: >>> 'Hello, %s' % 'world' 'Hello, world' >>> ...
- CF Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)
1. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort 暴力枚举,水 1.题意:n*m的数组, ...