Hello,
I insert images into access database table in an OLE object field. What I'm not able to do is read them back and show them in my GUI (size > 60 Kb). what I have noticed is that the binary representation of two different images with different size is the same and doesn't seems to be an image content at all.
My question is how can I store them and get them back correctly ?
Thanks.
|
Pasted test with a file with 199,055 byte.
Please email support At hxtt.com your sample. The simple test code is pasted following:
package test.jdbc.access;
import java.io.*;
import java.sql.*;
import java.util.Properties;
public class testOLEPicture1 {
public static void main(String argv[]) {
try {
Class.forName("com.hxtt.sql.access.AccessDriver");
String jdbcConnection = "jdbc:access:////test/access/issue/testhxtt.mdb";
Connection con = DriverManager.getConnection(jdbcConnection);
{
//init db
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate("create table if not exists hxtttest (fileno int,img LONGVARBINARY)");
}
int fileNo=1;
String srcfilePath="c:/temp/sample.png";
String dstfilePath="c:/temp/sampleout.png";
{
//Insert an image file
PreparedStatement stmt = con.prepareStatement(
"insert into hxtttest (fileno,img) values(?,?);",
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
stmt.setInt(1,fileNo);
FileInputStream fin = new FileInputStream(srcfilePath);
stmt.setBinaryStream(2,fin);
stmt.executeUpdate();
stmt.close();
fin.close();
}
PreparedStatement stmt = con.prepareStatement(
"select * from hxtttest where fileno="+fileNo,
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery();
rs.next();
Blob photo = rs.getBlob("img");
InputStream in = photo.getBinaryStream();
FileOutputStream fout = new FileOutputStream(dstfilePath);
int b;
int count = 0;
while((b = in.read())!=-1){
count++;
fout.write(b);
}
fout.close();
rs.close();
stmt.close();
con.close();
System.out.println("Writed byte:"+count);
} catch (SQLException sqle) {
do {
System.out.println(sqle.getMessage());
System.out.println("Error Code:" + sqle.getErrorCode());
System.out.println("SQL State:" + sqle.getSQLState());
sqle.printStackTrace();
} while ((sqle = sqle.getNextException()) != null);
} catch (Exception e) {
// System.out.println(e.getMessage());
System.out.println(e.toString());
// e.printStackTrace();
}
}
}
|
Sample app sent to support
|
Whether your upload servlet is cracking your data? According to my simple test for your hibernate bean, no issue to read/write local file.
package test.jdbc.hibernate;
import java.io.*;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class testAccessOLE2 {
public static void writeImage(EntityManager em,String srcfilePath) throws Throwable {
Portrait portrait = new Portrait();
File file=new File(srcfilePath);
FileInputStream fin = new FileInputStream(srcfilePath);
byte[] content=new byte[(int)file.length()];
fin.read(content);
fin.close();
portrait.setPortrait(content);
final String[] tokens = file.getName().split("\\.");
portrait.setImageFormat(tokens[tokens.length - 1]);
User user = em.getReference(User.class, 10L);
portrait.setUser(user);
em.getTransaction().begin();
em.persist(portrait);
em.getTransaction().commit();
}
public static void readImage(EntityManager em,String outDirPath) throws Throwable {
Query query = null;
query = em.createQuery("from test.jdbc.hibernate.Portrait");
List portraits = query.getResultList();
for(int i=0;i Portrait portrait=(Portrait)portraits.get(i);
FileOutputStream fout = new FileOutputStream(outDirPath+"\\"+portrait.getId()+"."+portrait.getImageFormat());
fout.write(portrait.getPortrait());
fout.close();
}
System.out.println("Output "+ portraits.size()+" image fles.");
}
public static void main(String[] args) throws Throwable {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("testAccessOLE");
EntityManager em = emf.createEntityManager();
// writeImage(em,"c:/temp/Portrait.PNG");
// writeImage(em,"c:/temp/sample.png");
readImage(em,"c:/temp/output");
em.close();
}
}
|
I have tested your sample, and that issue should be in our engine for blobl object cache.
|