我想将所有图像保存在已保存在数据库中的文件夹中 字节数组。
成功保存200+图像后,我的代码显示错误。
我的代码和错误的详细信息如下所述:
数据库字段类型
photograph bytea
域类
private Byte[] photograph
到目前为止我尝试过:
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.color.CMMException;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.imageio.ImageIO;
import org.postgresql.largeobject.LargeObjectManager;
public class Test {
public static void main(String[] argv) throws SQLException, IOException {
System.out.println("-------- PostgreSQL "
+ "JDBC Connection Testing ------------");
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your PostgreSQL JDBC Driver? "
+ "Include in your library path!");
e.printStackTrace();
return;
}
System.out.println("PostgreSQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:postgresql://127.0.0.1:5432/an?currentSchema=an", "postgres"
+ "",
"123456");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
//start
String query = "my_query";
PreparedStatement pst = connection.prepareStatement(query);
ResultSet rs = pst.executeQuery();
int i=0;
while (rs.next()) {
i++;
System.out.println("Saving ... : "+i);
// convert byte array back to BufferedImage
if(null == rs.getString(1)){
continue;
}
InputStream in = new ByteArrayInputStream(rs.getBytes(1));
BufferedImage bImageFromConvert = ImageIO.read(in);
if(null == bImageFromConvert){
continue;
}
OutputStream out = new FileOutputStream("e:/images/"+rs.getString(2)+".jpg");
ImageIO.write(bImageFromConvert, "jpg", out);
}
//end
} else {
System.out.println("Failed to make connection!");
}
}
}
在错误发生之前一切正常。如何克服这个问题。 我想保存所有图像,如果我不能然后只是跳到下一个图像 这就是我想要的。
错误:
Exception in thread "main" javax.imageio.IIOException: Unsupported Image Type
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1068)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1039)
at javax.imageio.ImageIO.read(ImageIO.java:1448)
at javax.imageio.ImageIO.read(ImageIO.java:1352)
at test.Test.main(Test.java:84)
C:\Users\Shifat\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 20 seconds)