• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BLOB shows smaller size than original file and when I retrieve it says that file is corrupt.

 
crazysam Ray
Greenhorn
Posts: 13
Java ME Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I am a newbie in Java.I am currently making a project in which I want to store pdf file in BLOB and then retrieve it to show to the user.Basically a database that manages pdfs.
But the problem I am facing is that pdf gets inserted in BLOB but each file shows same size which is very small than original size.Also I cant retrieve because it says that the file is corrupt.

Here is the code for inserting a pdf on a button click:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

try{
File newpdf = new File(path);
FileInputStream fis = new FileInputStream(newpdf);
ByteArrayOutputStream baos= new ByteArrayOutputStream();
byte[] buff = new byte[2048000];
for(int readNum; (readNum=fis.read(buff)) !=-1 ; ){
baos.write(buff,0,readNum);
}
userpdf=baos.toByteArray();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}


try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ppl","root","");
Statement pst = (Statement) con.createStatement();
String proj_name = JOptionPane.showInputDialog("Please enter name of the file");

String insert = "INSERT INTO project VALUES ('" + login.admission + "','" + login.yr + "','" + proj_name + "','" + userpdf + "')";
pst.executeUpdate(insert);

// String sql="insert into db (username,image) values ('"+user+"','"+userimage+"')";
// pst=con.prepareStatement(sql);
// pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Saved");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}




System.out.println(path);
new subORview().setVisible(true);
setVisible(false);
JOptionPane.showMessageDialog(null, "File stored successfully!!!");

}


This code runs and I get the message file stored successfully and I can see it in BLOB too but its size is too small.

This is the code for retrieval:


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ppl","root","");
Statement pst = (Statement) con.createStatement();
String sql = "select proj from project";

ResultSet resultSet = pst.executeQuery(sql);
while (resultSet.next()) {
java.sql.Blob myBlob = resultSet.getBlob("proj");
java.io.InputStream myInputStream = myBlob.getBinaryStream();
FileOutputStream fos = new FileOutputStream("F:\\BLOB_PDF.pdf");
byte[] buffer = new byte[2048];
int bytesRead = 0;
while ((bytesRead = myInputStream.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
System.out.println("Inside While:");
fos.close();
}
myInputStream.close();
fos.close();
}



} catch (Exception e) {
//throw new exception("insertSchCorpDoc :" + e);
JOptionPane.showMessageDialog(null, e);
} finally {
//release();

}
}


I am using long blob and attaching a screenshot of the TABLE.


Please help me out and bear as this is the first time i am doing a project in JAVA.

Hoping to learn a lot from all of you!!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you retrieve the file from the DB, you close the FileOutputStream inside your for loop.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you print out "userpdf.toString()" (that is what the INSERT statement does); it is not what you think it is.

I would also advise to get into the habit of using PreparedStatement so you can avoid having to get the quotes around strings right in your DB statements.
 
crazysam Ray
Greenhorn
Posts: 13
Java ME Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:When you retrieve the file from the DB, you close the FileOutputStream inside your for loop.


Yes i did that and i am also using userpdf.toString().

Still I have the same problem.As an output file I am getting the pdf but it says it is corrupt which is always of 1 kb in size.Also whichever file I upload to BLOB field is shown as size of 11 bytes in mysql. Please help me out.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So now you know why the way you're trying to insert a byte[] via an INSERT statement doesn't work. Which brings us back to using a PreparedStatement, because that has methods to insert a byte[].
 
crazysam Ray
Greenhorn
Posts: 13
Java ME Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
crazysam Ray wrote:
Joanne Neal wrote:When you retrieve the file from the DB, you close the FileOutputStream inside your for loop.


Yes i did that and i am also using userpdf.toString().

Still I have the same problem.As an output file I am getting the pdf but it says it is corrupt which is always of 1 kb in size.Also whichever file I upload to BLOB field is shown as size of 11 bytes in mysql. Please help me out.



This is the changed code as you all suggested:

This is for inserting pdf into BLOB field:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

try{
File newpdf = new File(path);
FileInputStream fis = new FileInputStream(newpdf);
ByteArrayOutputStream baos= new ByteArrayOutputStream();
byte[] buff = new byte[2048000];
for(int readNum; (readNum=fis.read(buff)) !=-1 ; ){
baos.write(buff,0,readNum);
}

userpdf=baos.toByteArray();
userpdf.toString();

}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}


try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ppl","root","");
Statement pst = (Statement) con.createStatement();
String proj_name = JOptionPane.showInputDialog("Please enter name of the file");

String insert = "INSERT INTO project VALUES ('" + login.admission + "','" + login.yr + "','" + proj_name + "','" + userpdf + "')";
pst.executeUpdate(insert);

// String sql="insert into db (username,image) values ('"+user+"','"+userimage+"')";
// pst=con.prepareStatement(sql);
// pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Saved");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}




System.out.println(path);
new subORview().setVisible(true);
setVisible(false);
JOptionPane.showMessageDialog(null, "File stored successfully!!!");

}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String filename = f.getAbsolutePath();
path = filename;
String new
}


and this is for retrieval of pdf:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {


try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ppl","root","");
Statement pst = (Statement) con.createStatement();
String sql = "select proj from project";

ResultSet resultSet = pst.executeQuery(sql);
while (resultSet.next()) {
java.sql.Blob myBlob = resultSet.getBlob("proj");
java.io.InputStream myInputStream = myBlob.getBinaryStream();
FileOutputStream fos = new FileOutputStream("F:\\BLOB_PDF.pdf");
byte[] buffer = new byte[2048];
int bytesRead = 0;
while ((bytesRead = myInputStream.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
System.out.println("Inside While:");
fos.close();
}
myInputStream.close();

}



} catch (Exception e) {
//throw new exception("insertSchCorpDoc :" + e);
JOptionPane.showMessageDialog(null, e);
} finally {
//release();

}
}


Here I have closed the output stream fos in while loop.

I still have two problems:

1) I have taken longBLOB and I upload pdf of size 175 kb but in the BLOB field I see their sizes as 4B or 11B.

2)As you can see that I try to generate a pdf in F: drive while retrieving the pdf.A pdf file is created with size 1 kb always for any file I retrieve and when I open it,it says this can't be opened.

Please help!!!

 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think you have changed? That looks pretty much the same as your earlier code.
 
crazysam Ray
Greenhorn
Posts: 13
Java ME Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:What do you think you have changed? That looks pretty much the same as your earlier code.


Sir I have used userpdf.toString

Also I have shown the jFilechooser code so that you all can tell me if something is wrong there.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have used userpdf.toString

Which is what you were doing before, and which, as I mentioned, and as you have found out already by printing out its value, is wrong. (As an aside, even if it were the right thing to do, the line "userpdf.toString();" does precisely nothing. But going into that is a tangent that does not help with the problem.)

Start by changing your code to use a PreparedStatement; you won't get anywhere without it.
 
crazysam Ray
Greenhorn
Posts: 13
Java ME Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
I have used userpdf.toString

Which is what you were doing before, and which, as I mentioned, and as you have found out already by printing out its value, is wrong. (As an aside, even if it were the right thing to do, the line "userpdf.toString();" does precisely nothing. But going into that is a tangent that does not help with the problem.)

Start by changing your code to use a PreparedStatement; you won't get anywhere without it.


Thanks a lot sir!! I will surely do that and ask you for help if problem persists.
 
crazysam Ray
Greenhorn
Posts: 13
Java ME Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
crazysam Ray wrote:
Ulf Dittmer wrote:
I have used userpdf.toString

Which is what you were doing before, and which, as I mentioned, and as you have found out already by printing out its value, is wrong. (As an aside, even if it were the right thing to do, the line "userpdf.toString();" does precisely nothing. But going into that is a tangent that does not help with the problem.)

Start by changing your code to use a PreparedStatement; you won't get anywhere without it.


Thanks a lot sir!! I will surely do that and ask you for help if problem persists.



Here is the changed code.Now I am using Preparedstatement. But still the same problem persists.

for inserting into blob:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

try{
File newpdf = new File(newpath);
FileInputStream fis = new FileInputStream(newpdf);
ByteArrayOutputStream baos= new ByteArrayOutputStream();
byte[] buff = new byte[2048000];
for(int readNum; (readNum=fis.read(buff)) !=-1 ; ){
baos.write(buff,0,readNum);
}

userpdf=baos.toByteArray();


}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}


try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ppl","root","");
String proj_name = JOptionPane.showInputDialog("Please enter name of the file");
String insert = "INSERT INTO project VALUES ('" + login.admission + "','" + login.yr + "','" + proj_name + "','" + userpdf + "')";

java.sql.PreparedStatement pst = con.prepareStatement(insert);




pst.executeUpdate(insert);


JOptionPane.showMessageDialog(null, "Saved");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}




System.out.println(path);
new subORview().setVisible(true);
setVisible(false);
JOptionPane.showMessageDialog(null, "File stored successfully!!!");

}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String filename = f.getAbsolutePath();
path = filename;
newpath = path.replace('\\', '/');
}


code for retrieval of pdf:


try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ppl","root","");
String sql = "select proj from project";
java.sql.PreparedStatement pst = con.prepareStatement(sql);


ResultSet resultSet = pst.executeQuery(sql);
while (resultSet.next()) {
java.sql.Blob myBlob = resultSet.getBlob("proj");
java.io.InputStream myInputStream = myBlob.getBinaryStream();
FileOutputStream fos = new FileOutputStream("F:\\BLOB_PDF.pdf");
byte[] buffer = new byte[20480000];
int bytesRead = 0;
while ((bytesRead = myInputStream.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
System.out.println("Inside While:");
fos.close();
}
myInputStream.close();

}



} catch (Exception e) {
//throw new exception("insertSchCorpDoc :" + e);
JOptionPane.showMessageDialog(null, e);
} finally {
//release();

}
}

I am sorry but I am very new to Java so please bear my questions.Thanks for your support.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a PreparedStatement means having ? for parameters, and then calling the various setXYZ methods. Read http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html and the javadocs of the class to learn more.

Also, please get into the habit of surrounding any lengthy code section of CODE tags: http://www.coderanch.com/how-to/java/UseCodeTags


Why are you sticking your tongue out at those who are trying to help you? Do you imagine that's a nice gesture?
 
crazysam Ray
Greenhorn
Posts: 13
Java ME Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am really very sorry for that sir. I have edited my post.Thanks for providing the links.Once again I apologize.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
crazysam Ray wrote:
Joanne Neal wrote:When you retrieve the file from the DB, you close the FileOutputStream inside your for loop.


Yes i did that

I know you did. That's what I was saying - you write the first buffer full of bytes to the file stream and then you close it and never reopen it. Any bytes you write after closing the stream will be lost.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic