使用Java,我上传了一张图片到postgres数据库大小是4000。但当我下载它时,它的大小是8000
这是最新的代码。我想尝试一些不同的东西,但我仍然得到同样的结果 我正在使用postgres DB,我创建了一个名为file的列,数据类型为bytea。 图像确实被上传到数据库(当我查看表的数据时,我可以在那里看到它) 我尝试了几个图像。当我上传它时,它的大小看起来是正确的,但当我下载它时,它的大小会翻倍。(这会破坏图像) 我们非常感谢您提供的任何帮助,以解释为什么这不起作用使用Java,我上传了一张图片到postgres数据库大小是4000。但当我下载它时,它的大小是8000,java,database,postgresql,Java,Database,Postgresql,这是最新的代码。我想尝试一些不同的东西,但我仍然得到同样的结果 我正在使用postgres DB,我创建了一个名为file的列,数据类型为bytea。 图像确实被上传到数据库(当我查看表的数据时,我可以在那里看到它) 我尝试了几个图像。当我上传它时,它的大小看起来是正确的,但当我下载它时,它的大小会翻倍。(这会破坏图像) 我们非常感谢您提供的任何帮助,以解释为什么这不起作用 public void uploadImage(File image, String imageName) throws
public void uploadImage(File image, String imageName) throws SQLException {
try {
conn = connectionManager.ConnectToDb();
String sql = "INSERT INTO images (name, file) "+ "VALUES(?,?)";
System.out.println("your image name is " + imageName);
System.out.println("Uploaded image name is " + image);
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1,imageName);
fis = new FileInputStream(image);
stmt.setBinaryStream(2, fis, (int) image.length());
System.out.println("Image SIZE = " +image.length());
stmt.execute();
conn.commit();
} catch (SQLException e) {
System.out.println("Could not insert into DB");
e.printStackTrace();
} catch (FileNotFoundException e) {
System.out.println("Could not insert into DB");
e.printStackTrace();
}
finally {
//close DB Objects
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
conn.close();
}
}
public void downloadImageFromDB(String imagename) throws SQLException
{
ConnectionManager connectionManager = new ConnectionManager();
try {
conn = connectionManager.ConnectToDb();
Statement downloadImageStatement = conn.createStatement();
downloadImageStatement.executeQuery("SELECT file FROM images WHERE name = '"+imagename+"'");
ResultSet rs = downloadImageStatement.getResultSet();
int i=1;
InputStream in = null;
int returnValue = 0;
if(rs.next())
{
String len1 = rs.getString("file");
int len = len1.length();
byte [] b = new byte[len];
in = rs.getBinaryStream("file");
int index = in.read(b, 0, len);
OutputStream outImej = new FileOutputStream("C:\\EclipseProjects\\StrutsHelloWorld\\"+imagename+".JPG");
while (index != -1)
{
outImej.write(b, 0, index);
index = in.read(b, 0, len);
System.out.println("==========================");
System.out.println(index);
System.out.println(outImej);
System.out.println("==========================");
}
outImej.close();
}else
{
returnValue = 1;
}
downloadImageStatement.close();
conn.close();
} catch (SQLException e) {
System.out.println("Could not get image from DB");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
// close db objects.
conn.close();
}
//return imgData;
}
您正在将二进制数据存储到Unicode文本字段中。将
文件
列更改为二进制,它应该可以工作。您不想使用它来测量其大小。这描述了available()
获取实际大小的一种(正确)方法是在写入文件后,使用检查该文件的大小。我使用了最新版本的JDBC驱动程序,问题得到了解决。我正在使用PostgreSQL 9.0.2和JDBC4驱动程序9.0-801。图像表的DDL是什么样子的?写入的文件有多大?(InputStream.available(…)
有时返回估计值)是由Is.available()
报告的大小始终是图像第一个大小的两倍,还是始终为8000?我会用另一个图像来测试这一点,以确定在这种情况下,错误的图像大小是否恰好是预期大小的两倍。首先,为什么要将图像存储在字符串中?String
是文本数据的数据类型,如果在其中存储二进制数据,那么此时您已经犯了一个错误。下面是最新的代码:我想尝试一些稍微不同的东西……但我仍然得到相同的结果:我使用的是postgresql。列文件数据类型为byteaBiggs:您是否查看了由fos
生成的文件?多长时间?其内容与原始文件相比如何?