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 */ - ...
随机推荐
- alias实现命令别名
需要经常进入/srv/www/app/account这个目录,每次都得重复的输入这一长串路径进入该目录,显得麻烦而费时,因此可以将"cd /srv/www/app/account" ...
- BACKLOG
团队: 郭志豪:http://www.cnblogs.com/gzh13692021053/ 杨子健:http://www.cnblogs.com/yzj666/ 刘森松:http://www.cnb ...
- grunt 检测js配置
module.exports = function(grunt) { // 项目配置 grunt.initConfig({ pkg: grunt.file.readJSON('package.json ...
- 【JAVA】Calendar
Calendar是JDK 1.1增加的类 最近使用了下Calendar发现几个很让人抓狂的问题 源码: public final static int SUNDAY = 1; public final ...
- MongoDB-权限配置
启动 Mongo\bin\mongo.exe1.添加>use admin #切换到MongoDB数据库的用户表>db.addUser("Name","Pass ...
- 一些简单编程练习题P【持续更新】
Q1.写程序将“Hello World”打印到屏幕. A1. public class Test { public static void main(String[] args) { System.o ...
- [LintCode] Add Two Numbers 两个数字相加
You have two numbers represented by a linked list, where each node contains a single digit. The digi ...
- CI框架入门2
文件目录与布局 1.user_guide 用户手册,可删 2.readme.rst 说明,可删 3.license.txt 证书,可删 4..gitignore composer. ...
- apache 自带的ab测试
ab -c 20 -n 2000 http://192.168.1.110:8080/index.php
- Spring的IOC原理(转载)
在网上看到一篇文章,感觉写得挺不错的,转载一下,本文转载自:http://www.cnblogs.com/xdp-gacl/p/3707631.html 一. IoC理论的背景 我们都知道,在采用面向 ...