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 */ - ...
随机推荐
- 转--脉络清晰的BP神经网络讲解,赞
http://www.cnblogs.com/wengzilin/archive/2013/04/24/3041019.html 学 习是神经网络一种最重要也最令人注目的特点.在神经网络的发展进程中, ...
- sd卡脱机烧写系统的方法(测试成功)
一.sd卡烧写系统的基本思路: (1)把uboot.bin烧写到sd卡 (2)把image整个文件夹复制到sd卡 (3)开发板从sd卡启动,就开始自动烧写到nandflash中了. 二.烧写uboot ...
- 对LR关联的一些理解
从接触LR关联的迷茫,到现在略有感悟,小记期间的一些理解. 一开始认识关联是在LR自带学习例子,需要关联session,也知道了自动关联,手动关联以及边录制边关联. 在使用关联的过程中,也学习了web ...
- jquery Ajax 案例
html <div class="data"><ul></ul></div> <div id="load" ...
- 数据结构之KMP算法next数组
我们要找到一个短字符串(模式串)在另一个长字符串(原始串)中的起始位置,也就是模式匹配,最关键的是找到next数组.最简单的算法就是用双层循环来解决,但是这种算法效率低,kmp算法是针对模式串自身的特 ...
- 安装dede显示dir的解决办法
1.遇见安装页面出现dir 2.你这个肯定是已经安装过了的程序,把install文件夹下的index.html删掉,,你再看看install文件夹下有没有install_lock.txt 和index ...
- 如何使用ArcGIS Online的地图数据作为底图
1.添加数据-GIS服务器 2.选择添加ArcGIS Server 3.使用GIS服务,下一步 4.输入url为http://cache1.arcgisonline.cn/ArcGIS/service ...
- 将一个Asp.Net网站改为MVC
背景: 网站已经开发的一部分 主要是Web服务和API 现在要做一些给人看的页面 转载请注明出处 http://www.cnblogs.com/zaiyuzhong/p/add-mvc-in-w ...
- css3折叠效果
在开发过程中,经常会遇到一些交互效果,今天所联系的便是一个类似折纸的折叠效果,查看效果. 说到折纸,我们先看下图 这是我第一时间想到的大体思路,如果能让这6个面连续的变化角度到0不就可以了吗,运用cs ...
- Android -- FlipViewController的使用(jar文件)
1. 此控件的作用 类似于翻书一样,可以上下翻页的控件, 2. 效果图