package com.test;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import javax.imageio.ImageIO;
import com.healthmarketscience.jackcess.Column;
import com.healthmarketscience.jackcess.DataType;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.DatabaseBuilder;
import com.healthmarketscience.jackcess.Row;
import com.healthmarketscience.jackcess.Table;
/**
* 第3方包读取Access
*
* @author ellis.xie
*
*/
public class TestJackess {
/**
* @param args
* @throws IOException
*/
public static void main(
String[] args) throws IOException {
// Table table = DatabaseBuilder.open(new File("C:\\Documents and Settings\\ellis.xie\\Desktop\\Database1.accdb")).getTable("t1");
String pathname = "E:\\xxx.MDB";
Database open = DatabaseBuilder.open(new File(pathname));
Set<String> tableNames = open.getTableNames();
for (String string : tableNames) {
System.out.println("表名称:" + string);
Table table = open.getTable(string);
int rowCount = table.getRowCount();
System.out.println("rowCount=" + rowCount);
}
StringBuffer sb = new StringBuffer();
Table table = open.getTable("照片表");
List<? extends Column> columns = table.getColumns();
String photeName = null;
DataType type = null;
byte[] photeValue = null;
for (Row row : table) {
for (Column column : columns) {
int columnIndex = column.getColumnIndex();
if (0 == columnIndex) {
photeName = row.get(column.getName()).toString();
} else {
;
}
type = column.getType();
if (DataType.OLE.toString().equals(type.toString())) {
photeValue = (byte[]) row.get(column.getName());
} else {
;
}
sb.append(photeName + "@" + type + "\t\t");
}
System.out.println(sb);
sb.setLength(0);// 清空sb
TestJackess.createPhoteFile(photeValue, photeName);
TestJackess.createPhoteFile2(photeValue, photeName);
}
}
/**
* 创建图片文件
*
* @param buffer
* 传来的图片信息byte数组
* @throws IOException
*/
public static void createPhoteFile(
byte[] buffer,
String photeName) throws IOException {
String URL = "D:/001xxx/";// 创建本地图片URL
FileOutputStream fos = new FileOutputStream(new File(URL + photeName + ".jpg"));
fos.write(buffer, 0, buffer.length);
fos.flush();
fos.close();
}
/**
* 创建图片文件
*
* @param buffer
* 传来的图片信息byte数组
* @throws IOException
*/
public static void createPhoteFile2(
byte[] buffer,
String photeName) throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream(buffer); // 将b作为输入流;
BufferedImage image = ImageIO.read(in); // 将in作为输入流,读取图片存入image中,而这里in可以为ByteArrayInputStream();
ImageIO.write(image, "jpg", new File("D:\\002xxx\\" + photeName + ".jpg"));
}
}
第3方包读取Access
猜你喜欢
转载自vernonchen163.iteye.com/blog/2071736
今日推荐
周排行