package com;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import oracle.sql.BLOB;
public class UpLoadPhoto implements FilenameFilter {
private String prefix = "", suffix = "";
public UpLoadPhoto(String filterstr) {
filterstr = filterstr.toLowerCase();
int i = filterstr.indexOf('*');
int j = filterstr.indexOf('.');
if (i > 0) {
prefix = filterstr.substring(0, i);
}
if (j > 0) {
suffix = filterstr.substring(j + 1);
}
}
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
boolean yes = true;
try {
name = name.toLowerCase();
yes = (name.startsWith(prefix)) & (name.endsWith(suffix));
} catch (NullPointerException e) {
}
return yes;
}
/**
* 得到目下文件的所有的照片
*
* @param file
* @return
*/
public static List getPhotoName(File file) {
List photoList = new ArrayList();
FilenameFilter filter = new UpLoadPhoto("*.jpg");
File curdir = new File(file.getAbsolutePath(), "");
String[] str = curdir.list(filter);
for (int i = 0; i < str.length; i++) {
String first = str[i].substring(0, str[i].length() - 4);
for(int m=0;m<str.length;m++){
String[] photoName = { "小", "大" };
String str1 = str[m].substring(0, str[m].length() - 4);
if(str1.indexOf("_")>-1){
String[] names = str1.split("_");
if(first.equals(names[0])){
photoName[0] = first;
photoName[1] = str1;
photoList.add(photoName);
}
}
}
}
for(int i=0;i<str.length;i++){
boolean ornot = false;
String first = str[i].substring(0, str[i].length() - 4);
for(int m=0;m<str.length;m++){
String str1 = str[m].substring(0, str[m].length() - 4);
if(str1.indexOf("_")>-1){
String[] names = str1.split("_");
if(first.equals(names[0])){
ornot=true;
break;
}
}
}
if(ornot==false){
if(first.indexOf("_")==-1){
String[] photoName={first,"大"};
photoList.add(photoName);
}
}
}
for(int i=0;i<str.length;i++){
boolean ornot = false;
String first = str[i].substring(0, str[i].length() - 4);
if(first.indexOf("_")>-1){
String[] name = first.split("_");
for(int m=0;m<str.length;m++){
String str1 = str[m].substring(0, str[m].length() - 4);
if(str1.indexOf("_")==-1){
if(name[0].equals(str1)){
ornot=true;
break;
}
}
}
if(ornot==false){
String[] photoName={"小",first};
photoList.add(photoName);
}
}
}
return photoList;
}
/**
* 处理业务 进行批量上传
*
* @param name
* @param filedir
*/
public static void inToPhoto(List namelist, File filedir) {
PreparedStatement pstmt = null;
ResultSet rs = null;
String workIdNo = null;
String workName = null;
BLOB blob = null;
Connection conn = null;
String name =null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@10.140.137.6:1521:fzjclk", "fzjc","fzjc");
conn.setAutoCommit(false);
for (int i = 0; i <= namelist.size(); i++) {
String[] photoName = (String[]) namelist.get(i);
pstmt = conn.prepareStatement("select WORK_IDNO,WORK_NAME from FZJC_HUM_INFO where WORK_NAME= ? ");
if("小".equals(photoName[0])){
name = photoName[1];
System.out.println(name + "的半身照片不存在!");
}else if("大".equals(photoName[1])){
name = photoName[0];
System.out.println(name + "的全身照片不存在!");
}else{
name = photoName[0];
}
pstmt.setString(1, name);
rs = pstmt.executeQuery();
if (rs.next()) {
workIdNo = rs.getString("WORK_IDNO");
workName = rs.getString("WORK_NAME");
} else {
System.out.println("数据库中不存在名为[" + name + "]的记录。");
continue;
}
rs.close();
pstmt.close();
pstmt = conn.prepareStatement("delete from fzjc_hum_photo where WORK_IDNO= ? ");
pstmt.setString(1, workIdNo);
pstmt.execute();
pstmt = conn.prepareStatement("insert into fzjc_hum_photo(WORK_IDNO,WORK_PHOTO,WORK_ALLPHOTO) values(?,empty_blob(),empty_blob())");
pstmt.setString(1, workIdNo);
pstmt.executeUpdate();
pstmt.close();
if (photoName[0] != null) {
File filePhto = new File(filedir + "//" + photoName[0]+ ".jpg");
pstmt = conn.prepareStatement("select WORK_PHOTO from fzjc_hum_photo where WORK_IDNO=? for update");
pstmt.setString(1, workIdNo);
rs = pstmt.executeQuery();
if (rs.next())
blob = (BLOB) rs.getBlob("WORK_PHOTO");
rs.close();
FileInputStream fin = new FileInputStream(filePhto);
InputStream streamIn = new BufferedInputStream(fin);
OutputStream streamOut = blob.getBinaryOutputStream();
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = streamIn.read(buffer, 0, 8192)) != -1) {
streamOut.write(buffer, 0, bytesRead);
}
streamOut.close();
streamIn.close();
/** 关闭 */
pstmt = conn.prepareStatement("update fzjc_hum_photo set WORK_PHOTO=? where WORK_IDNO=?");
pstmt.setBlob(1, blob);
pstmt.setString(2, workIdNo);
pstmt.executeUpdate();
pstmt.close();
conn.commit();
System.out.println(workName + "的半身照片上传成功!");
}
if (photoName[1] != null) {
File filePhto = new File(filedir + "//" + photoName[1]
+ ".jpg");
pstmt = conn
.prepareStatement("select WORK_ALLPHOTO from fzjc_hum_photo where WORK_IDNO=? for update");
pstmt.setString(1, workIdNo);
rs = pstmt.executeQuery();
if (rs.next())
blob = (BLOB) rs.getBlob("WORK_ALLPHOTO");
rs.close();
FileInputStream fin = new FileInputStream(filePhto);
InputStream streamInALL = new BufferedInputStream(fin);
OutputStream streamOutALL = blob.getBinaryOutputStream();
int bytesReadALL = 0;
byte[] bufferALL = new byte[8192];
while ((bytesReadALL = streamInALL.read(bufferALL, 0, 8192)) != -1) {
streamOutALL.write(bufferALL, 0, bytesReadALL);
}
streamOutALL.close();
streamInALL.close();
/** 关闭 */
pstmt = conn.prepareStatement("update fzjc_hum_photo set WORK_ALLPHOTO=? where WORK_IDNO=?");
pstmt.setBlob(1, blob);
pstmt.setString(2, workIdNo);
pstmt.executeUpdate();
pstmt.close();
conn.commit();
System.out.println(workName + "的全身照片上传成功!");
}
}
} catch (Exception e) {
System.out.println("上传完毕!");
} finally {
try {
if (rs!= null)
rs.close();
if (pstmt!= null)
pstmt.close();
if (conn!= null)
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**程序的入口*/
public static void main(String[] args) throws IOException {
System.out.print("请您输入照片存在的目录:");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String filename = in.readLine();
File filedir = new File(filename);
List namelist = getPhotoName(filedir);
inToPhoto(namelist, filedir);
System.out.println("可以关闭窗口了");
}
}