这是从数据库提交和检索图像的代码,但图像在数据库中既不可见,也不在jpg文件中(当我提取它们时)。谁能告诉我错误是什么?
public void actionPerformed(ActionEvent ae) {
String sw = "";
if(ae.getSource() == b1) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.showOpenDialog(null);
File selectedFile = fileChooser.getSelectedFile();
try {
// Insert image
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1","root","kanishkag");
String INSERT_PICTURE = "insert into image values (?)";
conn.setAutoCommit(false);
FileInputStream fis = new FileInputStream(selectedFile);
byte blob[] = new byte[(byte)selectedFile.length()];
fis.read(blob);
PreparedStatement ps = conn.prepareStatement(INSERT_PICTURE);
ps.setBinaryStream(1, (InputStream)fis, (int) selectedFile.length());
ps.executeUpdate();
conn.commit();
ps.close();
fis.close();
conn.close();
}
catch(Exception e) {
System.out.println(""+e);
}
}
else {
try{
// Retrieve image
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1","root","kanishkag");
Statement smt = conn.createStatement();
ResultSet rs = smt.executeQuery("select pictures from image ");
int i = 0;
while(rs.next()) {
OutputStream os = null;
InputStream is = null;
System.out.println("hiiiii");
BufferedImage bi = new BufferedImage(800,600,
BufferedImage.TYPE_INT_RGB);
is = rs.getBinaryStream(1);
ImageInputStream iis = ImageIO.createImageInputStream(is);
os = new FileOutputStream(new File("test1"+i+".jpg"));
ImageOutputStream oss = ImageIO.createImageOutputStream(os);
i++;
int c = 0;
while((c=iis.read())> -1) {
os.write(c);
}
ImageIO.write(bi, "test1"+i+".jpg", oss);
os.close();
is.close();
}
}
catch(Exception e1) {
e1.printStackTrace();
}
}
}