EXIF项目官网
- https://drewnoakes.com/code/exif/
引用说明
- 首先介绍一下什么是EXIF,EXIF是Exchangeable Image File的缩写,这是一种专门为数码相机照片设定的格式。这种格式可以用来记录数字照片的属性信息,例如相机的品牌及型号、相片的拍摄时间、拍摄时所设置 的光圈大小、快门速度、ISO等等信息。除此之外它还能够记录拍摄数据,以及照片格式化方式,这样就可以输出到兼容EXIF格式的外设上,例如照片打印机 等。
- 目前最常见的支持EXIF信息的图片格式是JPG,很多的图像工具都可以直接显示图片的EXIF信息,包括现在的一些著名的相册网站也提供页面用于 显示照片的EXIF信息。本文主要介绍Java语言如何读取图像的EXIF信息,包括如何根据EXIF信息对图像进行调整以适合用户浏览。
- 目前最简单易用的EXIF信息处理的Java包是Drew Noakes写的metadata-extractor,该项目最新的版本是2.3.4,支持EXIF 2.2版本。你可以直接从http://www.drewnoakes.com/code/exif/ 下载该项目的最新版本包括其源码。
- 需要注意的是,并不是每个JPG图像文件都包含有EXIF信息,你可以在Windows资源管理器单击选中图片后,如果该图片包含EXIF信息,则会在属性->摘要中显示出来。
代码留档
测试代码模块
package Util;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import Model.Aim;
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifIFD0Directory;
public class Test {
public static final SimpleDateFormat sf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
public static String dir_path = "";
// public static void main(String[] args) {
// wonPicDir("D://照片", "D://1.gpx");
// }
/**
* 获取照片目录,遍历文件
*
* @param PIC_path
* -照片目录地址
* @param KML_path
* -KML文件地址
*/
public static List<Aim> wonPicDir(String PIC_path, String KML_path) {
List<Aim> AIM_list = new ArrayList<Aim>();
System.out.println(PIC_path + KML_path);
File PIC_dirfile = new File(PIC_path);
if (PIC_dirfile.isDirectory()) {
File PIC_file[] = PIC_dirfile.listFiles();
for (File file : PIC_file){
AIM_list = printImageTime(file, KML_path);// 调用printImageTime
}
}
return AIM_list;
}
/**
* 获取照片时间,同时加载KML文件路径
*
* @param file
* -照片文件
* @param KML_path
* -KML文件
*/
public static List<Aim> printImageTime(File file, String KML_path) {
Metadata metadata;
List<Aim> AIM_list = new ArrayList<Aim>();
try {
metadata = ImageMetadataReader.readMetadata(file);
ExifIFD0Directory e = metadata
.getDirectory(ExifIFD0Directory.class);
try {
// 调用Match
AIM_list = Match(file, file.getName(), sf.format(e
.getDate(ExifIFD0Directory.TAG_DATETIME)), KML_path);
} catch (ParseException e1) {
e1.printStackTrace();
}
} catch (ImageProcessingException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return AIM_list;
}
/**
* 开始对图片时间和KML时间比较
*
* @param PIC_name
* 照片名字
* @param PIC_time
* 照片时间
* @param KML_file
* KML文件地址
* @throws ParseException
*/
@SuppressWarnings("finally")
public static List<Aim> Match(File PIC_file, String PIC_name, String PIC_time, String KML_file)
throws ParseException {
List<Aim> AIM_list = new ArrayList<Aim>();
Aim AIM_temp = new Aim();
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
try {
db = dbf.newDocumentBuilder();
doc = db.parse(new File(KML_file));
NodeList WPT_list = doc.getElementsByTagName("wpt");
for (int i = 0; i < WPT_list.getLength(); i++) {
Node wptNode = WPT_list.item(i);
// 获取wpt的lat属性值
Node latNode = wptNode.getAttributes().getNamedItem("lat");
// 获取wpt的lon属性值
Node lonNode = wptNode.getAttributes().getNamedItem("lon");
String latNode_val = latNode.getNodeValue();
String lonNode_val = lonNode.getNodeValue();
NodeList SWPT_list = wptNode.getChildNodes();
for (int j = 0; j < SWPT_list.getLength(); j++) {
if ("time".equals(SWPT_list.item(j).getNodeName())) {
// 如果匹配time节点名字则获取节点值
Node timeNode = SWPT_list.item(j);
String timeNode_val = timeNode.getTextContent();
// System.out.println("----------------------------------------");
// System.out.println("lat为:" + latNode_val);
// System.out.println("lon为:" + lonNode_val);
// System.out.println("time为:" + timeNode_val);
// System.out.println("----------------------------------------");
long XML_time_temp = sf.parse(create_KML_time(timeNode_val)).getTime();
long PIC_time_temp = sf.parse(PIC_time).getTime();
if (((PIC_time_temp - XML_time_temp) > 0 && (PIC_time_temp - XML_time_temp) < 120000)
|| ((XML_time_temp - PIC_time_temp) > 0 && (XML_time_temp - PIC_time_temp) < 120000)) {
System.out.println("---------------PIC时间为---------------");
System.out.println(PIC_time);
System.out.println("---------------XML时间为---------------");
System.out.println(timeNode_val);
System.out.println("---------------时间结果----------------");
System.out.println("时间小于2分钟,已经匹配!");
System.out.println("---------------坐标已经获取为---------------");
System.out.println(latNode_val+ "," + lonNode_val + ",0");
String STR_TEMP = latNode_val+ "," + lonNode_val + ",0";
//对PIC_name图片名字去除后缀
String FILE_name_split[] = PIC_name.split("[.]");
AIM_temp.setPIC_name(FILE_name_split[0]);
AIM_temp.setPIC_time(PIC_time);
AIM_temp.setKML_address(STR_TEMP);
AIM_list.add(AIM_temp);
// 开始读取一个kml文件并创建-返回创建的文件夹路径
openKML(PIC_file,AIM_list);
break;
}
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
System.out.println("test测试" + AIM_list.size());
return AIM_list;
}
}
/**
* 分割KML时间进行格式调整
* @param KML_time
* @return
*/
public static String create_KML_time(String KML_time) {
String KML_time_temp = KML_time;
// 第一次对T切割1990-01-01T08:00:00Z-切割后STR_temp[0]为1990-01-01
// STR_temp[1]为08:00:00Z
String STR_temp[] = KML_time_temp.split("T");
// 第二次对Z切割STR_temp1[0]为08:00:00 STR_temp1[1]为""
String STR_temp1[] = STR_temp[1].split("Z");
// 第三次组合时间字符串1990-01-01 08:00:00
String TIME_temp = STR_temp[0] + " " + STR_temp1[0];
return TIME_temp;
}
/**
* 循环坐标ZuoBiao对象
* @param LIST_temp
*/
public static void openKML(File PIC_file,List<Aim> LIST_temp){
Aim AIM_temp = new Aim();
for(int i=0;i<LIST_temp.size();i++){
AIM_temp = LIST_temp.get(i);
String KML_file = createdKML(AIM_temp);
System.out.println("---------------文件开始初始化---------------");
System.out.println(KML_file);
outputFile(PIC_file,KML_file,AIM_temp);
}
}
/**
* 创建新的XML文件
* @param AIM_temp
* @return
*/
public static String createdKML(Aim AIM_temp){
StringBuffer KML = new StringBuffer();
KML.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
KML.append("<kml xmlns=\"http://www.opengis.net/kml/2.2\" xmlns:gx=\"http://www.google.com/kml/ext/2.2\" xmlns:kml=\"http://www.opengis.net/kml/2.2\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\r\n");
KML.append("<Placemark>\r\n");
KML.append("<name>1</name>\r\n");
KML.append("<description><![CDATA[<div align='left'><b>2011-12-04 10:43:27</b><br/><br/></div><div align='center'><img src='files/" + AIM_temp.getPIC_name() + ".jpg' width='320' height='240' /><br/><span style='font-size:10px; line-height:150%; color:#999999'>" + AIM_temp.getPIC_name() + "</span></div>]]></description>\r\n");
KML.append("<StyleMap>\r\n");
KML.append("<Pair>\r\n");
KML.append("<key>normal</key>\r\n");
KML.append("<styleUrl>#normalPhotoSpot</styleUrl>\r\n");
KML.append("</Pair>\r\n");
KML.append("<Pair>\r\n");
KML.append("<key>highlight</key>\r\n");
KML.append("<Style>\r\n");
KML.append("<IconStyle>\r\n");
KML.append("<scale>1.1</scale>\r\n");
KML.append("<heading>-9819</heading>\r\n");
KML.append("<Icon>\r\n");
KML.append("<href>http://maps.google.com/mapfiles/kml/shapes/arrow.png</href>\r\n");
KML.append("</Icon>\r\n");
KML.append("<hotSpot x=\"32\" y=\"32\" xunits=\"pixels\" yunits=\"pixels\"/>\r\n");
KML.append("</IconStyle>\r\n");
KML.append("<LabelStyle>\r\n");
KML.append("<scale>1</scale>\r\n");
KML.append("</LabelStyle>\r\n");
KML.append("</Style>\r\n");
KML.append("</Pair>\r\n");
KML.append("</StyleMap>\r\n");
KML.append("<gx:balloonVisibility>1</gx:balloonVisibility>\r\n");
KML.append("<Point>\r\n");
KML.append("<coordinates>" + AIM_temp.getKML_address() + "</coordinates>\r\n");
KML.append("</Point>\r\n");
KML.append("</Placemark>\r\n");
KML.append("</kml>");
return KML.toString();
}
/**
* 输出KML文件
* @param KML_file
* @param ZB_temp
*/
public static void outputFile(File PIC_file,String KML_file,Aim AIM_temp){
File dir_path = createDir("D://" + AIM_temp.getPIC_name());
try {
FileOutputStream fos = new FileOutputStream(dir_path + "//" + AIM_temp.getPIC_name() + ".kml");
PrintWriter pw = new PrintWriter(fos);
pw.write(KML_file);
pw.flush();
pw.close();
System.out.println("---------------文件初始化完毕---------------");
File second_dir_path = createDir(dir_path.toString() + "//file");
System.out.println(second_dir_path);
System.out.println("---------------目录初始化完毕---------------");
try {
outputPic(PIC_file,second_dir_path.toString(),AIM_temp);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("----------------图片输出完毕---------------");
System.out.println("压缩文件目录:" + dir_path);
try {
zip(dir_path, new File("D:/" + AIM_temp.getPIC_name() + ".zip"));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("----------------压缩输出完毕---------------");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 输出JPG图片
* @param PIC_file
* @param second_dir_path
* @param AIM_temp
* @throws IOException
*/
public static void outputPic(File PIC_file, String second_dir_path,Aim AIM_temp) throws IOException{
System.out.println("JPG图片名为:" + AIM_temp.getPIC_name());
FileInputStream fis = new FileInputStream(PIC_file);
BufferedImage buff = ImageIO.read(fis);
FileOutputStream fos = new FileOutputStream(new File(second_dir_path + "//" + AIM_temp.getPIC_name() + ".jpg"));
ImageIO.write(buff, "jpg", fos);
}
/**
* 创建文件夹
* @param path
* @return
*/
public static File createDir(String path) {
File dirFile = null;
try {
dirFile = new File(path);
if (!(dirFile.exists()) && !(dirFile.isDirectory())) {
dirFile.mkdirs();
}
} catch (Exception e) {
e.printStackTrace();
}
return dirFile;
}
/**
* 压缩文件file成zip文件zipFile
*
* @param file
* 要压缩的文件
* @param zipFile
* 压缩文件存放地方
* @throws Exception
*/
public static void zip(File file, File zipFile) throws Exception {
ZipOutputStream output = null;
try {
output = new ZipOutputStream(new FileOutputStream(zipFile));
// 顶层目录开始
zipFile(output, file, "");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// 关闭流
if (output != null) {
output.flush();
output.close();
}
}
}
/**
* 压缩文件为zip格式
*
* @param output
* ZipOutputStream对象
* @param file
* 要压缩的文件或文件夹
* @param basePath
* 条目根目录
* @throws IOException
*/
public static void zipFile(ZipOutputStream output, File file,
String basePath) throws IOException {
FileInputStream input = null;
try {
// 文件为目录
if (file.isDirectory()) {
// 得到当前目录里面的文件列表
File list[] = file.listFiles();
basePath = basePath + (basePath.length() == 0 ? "" : "/")
+ file.getName();
// 循环递归压缩每个文件
for (File f : list)
zipFile(output, f, basePath);
} else {
// 压缩文件
basePath = (basePath.length() == 0 ? "" : basePath + "/")
+ file.getName();
System.out.println("子文件路径初始化:" + basePath);
output.putNextEntry(new ZipEntry(basePath));
input = new FileInputStream(file);
int readLen = 0;
byte[] buffer = new byte[1024 * 8];
while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1)
output.write(buffer, 0, readLen);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// 关闭流
if (input != null)
input.close();
}
}
}
正式代码模块
package Util;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import Model.Aim;
import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifIFD0Directory;
public class Test1 {
public static final SimpleDateFormat sf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
public static final SimpleDateFormat sf1 = new SimpleDateFormat(
"yyyy-MM-dd");
public static String dir_path = "";
public static boolean createdir = false;
public static void main(String[] args) {
wonPicDir("D://照片", "D://1.gpx");
}
public static List<Aim> wonPicDir(String PIC_path, String KML_path) {
Metadata metadata;
SAXBuilder builder = new SAXBuilder();
File PIC_dirfile = new File(PIC_path);
List<Aim> AIM_list = new ArrayList<Aim>();
String PIC_time = "";
//判断当前文件是否为文件夹
if (PIC_dirfile.isDirectory()) {
//获取文件集合
File PIC_file[] = PIC_dirfile.listFiles();
//开始遍历
for (File file : PIC_file){
//添加状态初始化
boolean add_state = false;
//临时坐标初始化
String ll = "";
try {
//取得图片拍摄时间
metadata = ImageMetadataReader.readMetadata(file);
ExifIFD0Directory e = metadata.getDirectory(ExifIFD0Directory.class);
PIC_time = sf.format(e.getDate(ExifIFD0Directory.TAG_DATETIME));
//获取gpx文件树形结构
Document doc = builder.build(new File(KML_path));
Element rootE = doc.getRootElement();
// 获得所有子元素
List<Element> root_list = rootE.getChildren();
for (Element el : root_list) {
// 获取el下的全部节点
List<Element> children_list = el.getChildren();
//判断当前节点名是否为wpt
if("wpt".equals(el.getName())){
//遍历所有子节点
for(Element ell : children_list){
//判断子节点名是否为time
if("time".equals(ell.getName())){
String time = ell.getText();
String lat = el.getAttributeValue("lat");
String lon = el.getAttributeValue("lon");
ll = lon + "," + lat + ",0";
long XML_time_temp = sf.parse(create_KML_time(time)).getTime();
//得到图片拍摄时间
long PIC_time_temp = sf.parse(PIC_time).getTime();
//判断gis和pic时间是否在时间差2分钟之内
if (((PIC_time_temp - XML_time_temp) > 0 && (PIC_time_temp - XML_time_temp) < 120000)
|| ((XML_time_temp - PIC_time_temp) > 0 && (XML_time_temp - PIC_time_temp) < 120000)) {
add_state = true;
System.out.println("---------------PIC时间为---------------");
System.out.println(PIC_time);
System.out.println("---------------GPX时间为---------------");
System.out.println(time);
System.out.println("---------------时间结果----------------");
System.out.println("时间小于2分钟,已经匹配!");
System.out.println("---------------坐标已经获取为---------------");
System.out.println(ll);
//对PIC_name图片名字去除后缀
String FILE_name_split[] = file.getName().split("[.]");
//对AIM_temp临时对象放置值
Aim AIM_temp = new Aim();
AIM_temp.setPIC_name(FILE_name_split[0]);
AIM_temp.setPIC_time(PIC_time);
AIM_temp.setKML_address(ll);
AIM_temp.setState("1");
//将AIM_temp临时对象存储到list集合中
AIM_list.add(AIM_temp);
// 开始读取一个kml文件并创建-返回创建的文件夹路径
openKML(file,AIM_temp);
//一旦有时间匹配了,将不再进行循环对比时间,直接退出for循环操作
break;
}
}
}
}
}
} catch (ImageProcessingException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
}
if(!add_state){
//对PIC_name图片名字去除后缀
String FILE_name_split[] = file.getName().split("[.]");
//对AIM_temp临时对象放置值
Aim AIM_temp = new Aim();
AIM_temp.setPIC_name(FILE_name_split[0]);
AIM_temp.setPIC_time(PIC_time);
AIM_temp.setKML_address("null");
AIM_temp.setState("0");
//将AIM_temp临时对象存储到list集合中
AIM_list.add(AIM_temp);
}
}
}
return AIM_list;
}
/**
* 分割KML时间进行格式调整
* @param KML_time
* @return
*/
public static String create_KML_time(String KML_time) {
String KML_time_temp = KML_time;
// 第一次对T切割1990-01-01T08:00:00Z-切割后STR_temp[0]为1990-01-01
// STR_temp[1]为08:00:00Z
String STR_temp[] = KML_time_temp.split("T");
// 第二次对Z切割STR_temp1[0]为08:00:00 STR_temp1[1]为""
String STR_temp1[] = STR_temp[1].split("Z");
// 第三次组合时间字符串1990-01-01 08:00:00
String TIME_temp = STR_temp[0] + " " + STR_temp1[0];
return TIME_temp;
}
/**
* KML文件操作
* @param PIC_file 图片文件
* @param AIM_temp Aim临时对象
*/
public static void openKML(File PIC_file,Aim AIM_temp){
//创建KML文件
String KML_file = createdKML(AIM_temp);
System.out.println("---------------文件开始初始化---------------");
System.out.println(KML_file);
//输出KML文件
outputFile(PIC_file,KML_file,AIM_temp);
}
/**
* 创建新的KML文件
* @param AIM_temp Aim临时对象
* @return String 返回KML文件String类型数据
*/
public static String createdKML(Aim AIM_temp){
StringBuffer KML = new StringBuffer();
KML.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
KML.append("<kml xmlns=\"http://www.opengis.net/kml/2.2\" xmlns:gx=\"http://www.google.com/kml/ext/2.2\" xmlns:kml=\"http://www.opengis.net/kml/2.2\" xmlns:atom=\"http://www.w3.org/2005/Atom\">\r\n");
KML.append("<Placemark>\r\n");
KML.append("<name>" + AIM_temp.getPIC_name() + "</name>\r\n");
KML.append("<description><![CDATA[<div align='left'><b>" + AIM_temp.getPIC_time() + "</b><br/><br/></div><div align='center'><img src='files/" + AIM_temp.getPIC_name() + ".jpg' width='320' height='240' /><br/><span style='font-size:10px; line-height:150%; color:#999999'>" + AIM_temp.getPIC_name() + "</span></div>]]></description>\r\n");
KML.append("<StyleMap>\r\n");
KML.append("<Pair>\r\n");
KML.append("<key>normal</key>\r\n");
KML.append("<styleUrl>#normalPhotoSpot</styleUrl>\r\n");
KML.append("</Pair>\r\n");
KML.append("<Pair>\r\n");
KML.append("<key>highlight</key>\r\n");
KML.append("<Style>\r\n");
KML.append("<IconStyle>\r\n");
KML.append("<scale>1.1</scale>\r\n");
KML.append("<heading>-9819</heading>\r\n");
KML.append("<Icon>\r\n");
KML.append("<href>http://maps.google.com/mapfiles/kml/shapes/arrow.png</href>\r\n");
KML.append("</Icon>\r\n");
KML.append("<hotSpot x=\"32\" y=\"32\" xunits=\"pixels\" yunits=\"pixels\"/>\r\n");
KML.append("</IconStyle>\r\n");
KML.append("<LabelStyle>\r\n");
KML.append("<scale>1</scale>\r\n");
KML.append("</LabelStyle>\r\n");
KML.append("</Style>\r\n");
KML.append("</Pair>\r\n");
KML.append("</StyleMap>\r\n");
KML.append("<gx:balloonVisibility>1</gx:balloonVisibility>\r\n");
KML.append("<Point>\r\n");
KML.append("<coordinates>" + AIM_temp.getKML_address() + "</coordinates>\r\n");
KML.append("</Point>\r\n");
KML.append("</Placemark>\r\n");
KML.append("</kml>");
return KML.toString();
}
/**
* 输出KML文件
* @param PIC_file 图片文件
* @param KML_file KML字符串
* @param AIM_temp Aim临时对象
*/
public static void outputFile(File PIC_file,String KML_file,Aim AIM_temp){
//创建输出目录
File dir_path = createDir("D://" + sf1.format(new Date()) + "//" + AIM_temp.getPIC_name());
try {
FileOutputStream fos = new FileOutputStream(dir_path + "//" + AIM_temp.getPIC_name() + ".kml");
PrintWriter pw = new PrintWriter(fos);
pw.write(KML_file);
pw.flush();
pw.close();
System.out.println("---------------文件初始化完毕---------------");
//创建次级目录files
File second_dir_path = createDir(dir_path.toString() + "//files");
System.out.println(second_dir_path);
System.out.println("---------------目录初始化完毕---------------");
try {
outputPic(PIC_file,second_dir_path.toString(),AIM_temp);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("----------------图片输出完毕---------------");
System.out.println("压缩文件目录:" + dir_path);
try {
zip(dir_path, new File(dir_path + ".kmz"));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("----------------压缩输出完毕---------------");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 输出JPG图片
* @param PIC_file 图片文件
* @param second_dir_path 生成的次级目录files
* @param AIM_temp Aim临时对象
* @throws IOException 直接声明异常
*/
public static void outputPic(File PIC_file, String second_dir_path,Aim AIM_temp) throws IOException{
System.out.println("JPG图片名为:" + AIM_temp.getPIC_name());
FileInputStream fis = new FileInputStream(PIC_file);
BufferedImage buff = ImageIO.read(fis);
FileOutputStream fos = new FileOutputStream(new File(second_dir_path + "//" + AIM_temp.getPIC_name() + ".jpg"));
ImageIO.write(buff, "jpg", fos);
}
/**
* 创建文件夹
* @param path 文件夹创建路径
* @return File 返回创建好的文件夹路径
*/
public static File createDir(String path) {
File dirFile = null;
try {
dirFile = new File(path);
if (!(dirFile.exists()) && !(dirFile.isDirectory())) {
dirFile.mkdirs();
}
} catch (Exception e) {
e.printStackTrace();
}
return dirFile;
}
/**
* 压缩文件file成zip文件-这里直接压缩后成为KMZ文件
*
* @param file
* 要压缩的文件
* @param zipFile
* 压缩文件存放地方-文件夹
* @throws Exception
*/
public static void zip(File file, File zipFile) throws Exception {
ZipOutputStream output = null;
try {
output = new ZipOutputStream(new FileOutputStream(zipFile));
//每次压缩完一个文件后就初始化createdir-该操作目的为控制顶级文件夹的生成
createdir = false;
// 顶层目录开始
zipFile(output, file, "");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// 关闭流
if (output != null) {
output.flush();
output.close();
}
}
}
/**
* 压缩文件为zip格式
*
* @param output
* ZipOutputStream对象
* @param file
* 要压缩的文件或文件夹
* @param basePath
* 条目根目录
* @throws IOException
*/
public static void zipFile(ZipOutputStream output, File file,
String basePath) throws IOException {
FileInputStream input = null;
System.out.println("文件路径初始化:" + basePath);
try {
// 文件为目录
if (file.isDirectory()) {
// 得到当前目录里面的文件列表
File list[] = file.listFiles();
basePath = basePath + (basePath.length() == 0 ? "" : "/")
+ file.getName();
if(!createdir){
//使第一次创建的目录级别为空,第二次创建的目录则为files
basePath = "";
createdir = true;
}
System.out.println("子文件路径初始化0:" + basePath);
// 循环递归压缩每个文件
for (File f : list)
zipFile(output, f, basePath);
} else {
// 压缩文件
basePath = (basePath.length() == 0 ? "" : basePath + "/")
+ file.getName();
System.out.println("子文件路径初始化1:" + basePath);
output.putNextEntry(new ZipEntry(basePath));
input = new FileInputStream(file);
int readLen = 0;
byte[] buffer = new byte[1024 * 8];
while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1)
output.write(buffer, 0, readLen);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// 关闭流
if (input != null)
input.close();
}
}
}
参考案例
- http://www.oschina.net/code/snippet_12_1414(EXIF解析)
- http://www.2cto.com/kf/201301/181312.html (EXIF解析)
- http://yuncode.net/code/c_51768e701184093(EXIF解析)
- http://www.blogjava.net/mrcmd/archive/2007/08/24/138963.html (文件压缩)
- http://www.cnblogs.com/ldcsaa/archive/2012/02/23/2364036.html (文件上传和下载)
- http://www.cnblogs.com/ungshow/archive/2009/01/12/1374491.html (文件下载)
- http://blog.csdn.net/error_case/article/details/7623879 (多文件打包下载)
- http://www.blogjava.net/baizhihui19870626/articles/372872.html (文件夹遍历)