I tried to uplad a pdf file using java.sql.PreparedStatement
to mysql
Blob field using the following code.
我尝试使用以下代码使用java.sql.PreparedStatement将pdf文件升级为mysql Blob字段。
File inFile = new File("Path+BLOCK.pdf");
byte[] b = new byte[(int)inFile.length()];
PreparedStatement psmnt = (PreparedStatement)
con.prepareStatement("INSERT INTO
2012DOC (SRNO,DOCUMENT)
VALUES (?,?)"
); //con is java.sql.Connection object
psmnt.setString(1, "1200021");
psmnt.setBytes(2, b);
psmnt.executeUpdate();
This code executes without error and database shows blob content, but when I try to retrieve the file using the below code it gives a corrupt file which doesn't open.
此代码执行时没有错误,数据库显示blob内容,但是当我尝试使用下面的代码检索文件时,它会提供一个无法打开的损坏文件。
ResultSet rs=con.Execute("SELECT DOCUMENT FROM 2012DOC");
rs.next();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=kjsahkjd.pdf");
java.sql.Blob blob = rs.getBlob("DOCUMENT");
ServletOutputStream servletOutputStream = response.getOutputStream();
InputStream in = blob.getBinaryStream();
int length = (int) blob.length();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
while ((length = in.read(buffer)) != -1) {
servletOutputStream.write(buffer, 0, length);
}
in.close();
servletOutputStream.flush();
servletOutputStream.close();
It outputs the file with same size as the original,but the file doesn't open. The pdf
reader is fired but cannot open the file and gives an error 'the file was either damaged or not supported file type'
它输出与原始文件大小相同的文件,但文件无法打开。 pdf阅读器被触发但无法打开文件并发出错误'文件已损坏或文件类型不受支持'
2 个解决方案
#1
2
Ahhh...After a little debugging I found the code that uploads is troublesome, and finally got the right way to do it.
啊...经过一些调试后我发现上传的代码很麻烦,最后得到了正确的方法。
Here is what I did...I'm posting it so that others with same problem can solve it
这就是我做的......我发布它,以便其他有相同问题的人可以解决它
After Converting the java.io.File
to java.io.FileInputStream
将java.io.File转换为java.io.FileInputStream之后
FileInputStream io = new FileInputStream(inFile);
Set the BLOB field using psmnt.setBinaryStream()
使用psmnt.setBinaryStream()设置BLOB字段
psmnt.setBinaryStream(3, (InputStream)io,(int)inFile.length());
#2
0
remove " java.sql.Blob blob = rs.getBlob("DOCUMENT");
"
删除“java.sql.Blob blob = rs.getBlob(”DOCUMENT“);”
and don't initialize length i.e instead of
并且不要初始化长度,而不是
int length = (int) blob.length();
just write
int length;
then it downloads file successfully .. enjoy :)
然后它成功下载文件..享受:)
#1
2
Ahhh...After a little debugging I found the code that uploads is troublesome, and finally got the right way to do it.
啊...经过一些调试后我发现上传的代码很麻烦,最后得到了正确的方法。
Here is what I did...I'm posting it so that others with same problem can solve it
这就是我做的......我发布它,以便其他有相同问题的人可以解决它
After Converting the java.io.File
to java.io.FileInputStream
将java.io.File转换为java.io.FileInputStream之后
FileInputStream io = new FileInputStream(inFile);
Set the BLOB field using psmnt.setBinaryStream()
使用psmnt.setBinaryStream()设置BLOB字段
psmnt.setBinaryStream(3, (InputStream)io,(int)inFile.length());
#2
0
remove " java.sql.Blob blob = rs.getBlob("DOCUMENT");
"
删除“java.sql.Blob blob = rs.getBlob(”DOCUMENT“);”
and don't initialize length i.e instead of
并且不要初始化长度,而不是
int length = (int) blob.length();
just write
int length;
then it downloads file successfully .. enjoy :)
然后它成功下载文件..享受:)