使用 java.sql.PreparedStatement 将 PDF 文件上传到 mysql BLOB 而不损坏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11238927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:21:22  来源:igfitidea点击:

Upload PDF file to mysql BLOB by using java.sql.PreparedStatement without corruption

javamysqlsqlblobprepared-statement

提问by Sangeet Menon

I tried to uplad a pdf file using java.sql.PreparedStatementto mysqlBlob field using the following code.

我尝试使用以下代码将 pdf 文件上传java.sql.PreparedStatementmysqlBlob 字段。

        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 pdfreader is fired but cannot open the file and gives an error 'the file was either damaged or not supported file type'

它输出与原始文件大小相同的文件,但文件没有打开。该pdf阅读器被触发,但不能打开该文件,并给出了一个错误“的文件被损坏或不支持的文件类型”

采纳答案by Sangeet Menon

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.Fileto java.io.FileInputStream

转换为java.io.Filejava.io.FileInputStream

FileInputStream io = new FileInputStream(inFile);

Set the BLOB field using psmnt.setBinaryStream()

使用设置 BLOB 字段 psmnt.setBinaryStream()

psmnt.setBinaryStream(3,  (InputStream)io,(int)inFile.length());

回答by shivam

remove " java.sql.Blob blob = rs.getBlob("DOCUMENT");"

删除“ java.sql.Blob blob = rs.getBlob("DOCUMENT");

and don't initialize length i.e instead of

并且不要初始化长度 ie 而不是

int length = (int) blob.length();

just write

写就好了

int length;

then it downloads file successfully .. enjoy :)

然后它成功下载文件..享受:)