给你一个把图片存进去的
package image;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
/**
* 批量插入图片的程序
* 使用方法:java GoodsImageBatch param0
* param0 为图片所在目录绝对路径,如 /home/oracle/pic
* $CLASSPATH中需要Oracle驱动classes12.zip
* 若商品表不是goods_ext,图片表不是goods_image,需要修改63,65,67三行的代码
*/
public class GoodsImageBatch{
//以下四行视情况更改
private static String URI = "jdbc:oracle:thin:@192.168.168.5:1521:ORCL";
private static String USER = "jamada";
private static String PASSWD = "jamada";
private static String SEQUENCY = "s";
//列文件列表
public static FilenameFilter filter(final String afn){
return new FilenameFilter(){
String fn = afn;
public boolean accept(File dir,String n){
String f = new File(n).getName();
return true;
}
};
}
//得到图片输入流
public static InputStream getFileStream(File f){
try{
return new DataInputStream(new BufferedInputStream(new
FileInputStream(f.
getAbsolutePath())));
}catch(Exception e){
System.out.println("get file input stream error!File:"+f.getName());
return null;
}
} //清除字符串中的空格
private static String getTrimStr(String str){
str = str.trim();
for(int i=0;i<str.length();i++){
if(str.substring(i,i+1).equals(" "))str=str.substring(0,i)+str.substring(
i+1,str.length());
}
return str;
} public static void main(String[] args){
if(args.length!=1){
System.out.println("useage: java GoodsImageBatch <filepath>");
System.exit(0);
}System.out.println("*************************************************************");
System.out.println("* Insert a batch of images from localdisk.*");
System.out.println("* Create by Murphy, 2001-7-31,Modify by Murphy,2001-8-19 *");
System.out.println("* Copyright 2000-2002 SilkWay Co,. Ltd.Allrights reserved. *");
System.out.println("* Please wait... *");System.out.println("*************************************************************");
try{
batchImage(args);
}catch(SQLException se){
System.out.println("Insert a batch of image error!");
}
} /**
* 搜索目录下的所有图片并保存到数据库中
* @param args 执行批处理命令时入口参数,得到图片所在的目录
*/
private static void batchImage(String[] args) throws SQLException{
Connection con = null;
PreparedStatement ps = null;
PreparedStatement psUpdate = null;
ResultSet rs = null;
String[] fileList;
long picCount = 0;
long goodsPicCount = 0;
//得到sequercy的SQL
String seqSql = "select "+SEQUENCY+".nextval from dual";
//查找是否存在该商品的SQL
String goodsCodeSql = "select goods_code from goods_ext where goods_code=? ";
//修改商品表图片Id字段的SQL
String updateIdSql = "update goods_ext set goods_image = ? where goods_cod e=?";
//插商品图片表的SQL
String insPicSql = "insert into goods_image (ID,BIN_DATA,FILE_NAME) values (?,?,?)";
try{
//连接数据库
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(URI,USER,PASSWD);
con.setAutoCommit(false);
//取文件列表
File path = new File(args[0]);
fileList = path.list(filter(args[0]));
for(int i=0;i<fileList.length;i++){
File picFile = new
File(args[0]+System.getProperty("file.separator")+fileList[i]);
String extName =
fileList[i].substring(fileList[i].lastIndexOf(".")+1,fileList[i].length());
if(extName.equalsIgnoreCase("JPG") ||
extName.equalsIgnoreCase("JPEG") || extName.equalsIgnoreCase("GIF")){
picCount++;
System.out.println("Now Check File:"+picFile.getAbsolutePath());
String picFileUname =
fileList[i].substring(0,fileList[i].lastIndexOf(" .")).toUpperCase();
//查找商品
ps = con.prepareStatement(goodsCodeSql);
ps.setString(1,getTrimStr(picFileUname));
rs = ps.executeQuery();
//查找是否存在此商品
if(rs.next()){
goodsPicCount++;
ps = con.prepareStatement(seqSql);
rs = ps.executeQuery();
//得到图片Id
if(rs.next()){
long imageId = rs.getLong(1);
psUpdate = con.prepareStatement(updateIdSql);
psUpdate.setLong(1,imageId);
psUpdate.setString(2,getTrimStr(picFileUname));
int resCount = psUpdate.executeUpdate();
if (resCount == 0)
throw new SQLException("could not modify image id when goodsCode is "+picFileUname); InputStream picStream = getFileStream(picFile);
//插入商品图片表
psUpdate = con.prepareStatement(insPicSql);
psUpdate.setLong(1,imageId);
if (picStream != null)
psUpdate.setBinaryStream(2,picStream,picStream.available());
else
psUpdate.setBinaryStream(2,null,0); psUpdate.setString(3,fileList[i]);
resCount = psUpdate.executeUpdate();
if (resCount == 0)
throw new SQLException("could not insert image!");
}else //无法得到图片Id
throw new SQLException("could not get image id!");
}else //不存在此商品
System.out.println("could not insert image! Plz confirm the database e xist the goods_code:"+picFileUname); }
if(rs!=null)rs.close();
rs = null;
if(ps!=null)ps.close();
ps = null;
if(psUpdate!=null)psUpdate.close();
psUpdate = null;
}
con.commit();System.out.println("***************************************************** ***");
System.out.println("* Now insert a batch of images end!");
System.out.println("* There are "+fileList.length+" file in the directory ");
System.out.println("* JPG,JPEG,GIF File:"+picCount+", Insertcount:"+goodsPicCount);System.out.println("********************************************************");
}catch(SQLException se){
con.rollback();
System.out.println("GoodsImageBatch throw SQLException:"+se);
se.printStackTrace();
throw new SQLException("throw SQLException:"+se);
}catch(Exception e){
System.out.println("GoodsImageBatch throw Exception:"+e);
e.printStackTrace();
throw new SQLException("throw Exception:"+e);
}finally{
if(rs!=null)rs.close();
rs = null;
if(ps!=null)ps.close();
ps = null;
if(psUpdate!=null)psUpdate.close();
psUpdate = null;
if(con!=null && !con.isClosed())con.close();
con = null;
}
}
}
package image;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
/**
* 批量插入图片的程序
* 使用方法:java GoodsImageBatch param0
* param0 为图片所在目录绝对路径,如 /home/oracle/pic
* $CLASSPATH中需要Oracle驱动classes12.zip
* 若商品表不是goods_ext,图片表不是goods_image,需要修改63,65,67三行的代码
*/
public class GoodsImageBatch{
//以下四行视情况更改
private static String URI = "jdbc:oracle:thin:@192.168.168.5:1521:ORCL";
private static String USER = "jamada";
private static String PASSWD = "jamada";
private static String SEQUENCY = "s";
//列文件列表
public static FilenameFilter filter(final String afn){
return new FilenameFilter(){
String fn = afn;
public boolean accept(File dir,String n){
String f = new File(n).getName();
return true;
}
};
}
//得到图片输入流
public static InputStream getFileStream(File f){
try{
return new DataInputStream(new BufferedInputStream(new
FileInputStream(f.
getAbsolutePath())));
}catch(Exception e){
System.out.println("get file input stream error!File:"+f.getName());
return null;
}
} //清除字符串中的空格
private static String getTrimStr(String str){
str = str.trim();
for(int i=0;i<str.length();i++){
if(str.substring(i,i+1).equals(" "))str=str.substring(0,i)+str.substring(
i+1,str.length());
}
return str;
} public static void main(String[] args){
if(args.length!=1){
System.out.println("useage: java GoodsImageBatch <filepath>");
System.exit(0);
}System.out.println("*************************************************************");
System.out.println("* Insert a batch of images from localdisk.*");
System.out.println("* Create by Murphy, 2001-7-31,Modify by Murphy,2001-8-19 *");
System.out.println("* Copyright 2000-2002 SilkWay Co,. Ltd.Allrights reserved. *");
System.out.println("* Please wait... *");System.out.println("*************************************************************");
try{
batchImage(args);
}catch(SQLException se){
System.out.println("Insert a batch of image error!");
}
} /**
* 搜索目录下的所有图片并保存到数据库中
* @param args 执行批处理命令时入口参数,得到图片所在的目录
*/
private static void batchImage(String[] args) throws SQLException{
Connection con = null;
PreparedStatement ps = null;
PreparedStatement psUpdate = null;
ResultSet rs = null;
String[] fileList;
long picCount = 0;
long goodsPicCount = 0;
//得到sequercy的SQL
String seqSql = "select "+SEQUENCY+".nextval from dual";
//查找是否存在该商品的SQL
String goodsCodeSql = "select goods_code from goods_ext where goods_code=? ";
//修改商品表图片Id字段的SQL
String updateIdSql = "update goods_ext set goods_image = ? where goods_cod e=?";
//插商品图片表的SQL
String insPicSql = "insert into goods_image (ID,BIN_DATA,FILE_NAME) values (?,?,?)";
try{
//连接数据库
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(URI,USER,PASSWD);
con.setAutoCommit(false);
//取文件列表
File path = new File(args[0]);
fileList = path.list(filter(args[0]));
for(int i=0;i<fileList.length;i++){
File picFile = new
File(args[0]+System.getProperty("file.separator")+fileList[i]);
String extName =
fileList[i].substring(fileList[i].lastIndexOf(".")+1,fileList[i].length());
if(extName.equalsIgnoreCase("JPG") ||
extName.equalsIgnoreCase("JPEG") || extName.equalsIgnoreCase("GIF")){
picCount++;
System.out.println("Now Check File:"+picFile.getAbsolutePath());
String picFileUname =
fileList[i].substring(0,fileList[i].lastIndexOf(" .")).toUpperCase();
//查找商品
ps = con.prepareStatement(goodsCodeSql);
ps.setString(1,getTrimStr(picFileUname));
rs = ps.executeQuery();
//查找是否存在此商品
if(rs.next()){
goodsPicCount++;
ps = con.prepareStatement(seqSql);
rs = ps.executeQuery();
//得到图片Id
if(rs.next()){
long imageId = rs.getLong(1);
psUpdate = con.prepareStatement(updateIdSql);
psUpdate.setLong(1,imageId);
psUpdate.setString(2,getTrimStr(picFileUname));
int resCount = psUpdate.executeUpdate();
if (resCount == 0)
throw new SQLException("could not modify image id when goodsCode is "+picFileUname); InputStream picStream = getFileStream(picFile);
//插入商品图片表
psUpdate = con.prepareStatement(insPicSql);
psUpdate.setLong(1,imageId);
if (picStream != null)
psUpdate.setBinaryStream(2,picStream,picStream.available());
else
psUpdate.setBinaryStream(2,null,0); psUpdate.setString(3,fileList[i]);
resCount = psUpdate.executeUpdate();
if (resCount == 0)
throw new SQLException("could not insert image!");
}else //无法得到图片Id
throw new SQLException("could not get image id!");
}else //不存在此商品
System.out.println("could not insert image! Plz confirm the database e xist the goods_code:"+picFileUname); }
if(rs!=null)rs.close();
rs = null;
if(ps!=null)ps.close();
ps = null;
if(psUpdate!=null)psUpdate.close();
psUpdate = null;
}
con.commit();System.out.println("***************************************************** ***");
System.out.println("* Now insert a batch of images end!");
System.out.println("* There are "+fileList.length+" file in the directory ");
System.out.println("* JPG,JPEG,GIF File:"+picCount+", Insertcount:"+goodsPicCount);System.out.println("********************************************************");
}catch(SQLException se){
con.rollback();
System.out.println("GoodsImageBatch throw SQLException:"+se);
se.printStackTrace();
throw new SQLException("throw SQLException:"+se);
}catch(Exception e){
System.out.println("GoodsImageBatch throw Exception:"+e);
e.printStackTrace();
throw new SQLException("throw Exception:"+e);
}finally{
if(rs!=null)rs.close();
rs = null;
if(ps!=null)ps.close();
ps = null;
if(psUpdate!=null)psUpdate.close();
psUpdate = null;
if(con!=null && !con.isClosed())con.close();
con = null;
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货