gpt4 book ai didi

java - 从mysql java下载后PDF文件损坏

转载 作者:行者123 更新时间:2023-11-30 22:35:39 25 4
gpt4 key购买 nike

我正在尝试将 pdf 文件上传到我的数据库并尝试下载相同的文件,但在下载文件后我无法打开该文件,我收到一条错误消息,指出文件类型不受支持。这是文件上传的代码。

    package itext;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.mysql.jdbc.PreparedStatement;

public class FileUpload {
public static void fileupload() throws FileNotFoundException{

String inFile="D:/Eclipse Java/myown.pdf";
FileInputStream io = new FileInputStream(inFile);
byte[] pdfData = new byte[(int) inFile.length()];
DataInputStream dis;
try {
dis = new DataInputStream(new FileInputStream(inFile));
dis.readFully(pdfData); // read from file into byte[] array
dis.close();

Connection dbConnection;

String myConnectionString =
"jdbc:mysql://******/*****";

dbConnection = DriverManager.getConnection(myConnectionString, "****", "****");
PreparedStatement ps = (PreparedStatement) dbConnection.prepareStatement("INSERT INTO Form_BL (AppID,Form) VALUES (?,?)");
ps.setString(1, "1");
ps.setBytes(2, pdfData); // byte[] array
ps.executeUpdate();
ps.setBinaryStream(1, (InputStream)dis,(int)inFile.length());
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

这是下载代码

    package itext;

import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

import com.mysql.jdbc.Blob;
import com.mysql.jdbc.PreparedStatement;

public class FileDownload {
public static void filedownload(){
Connection dbConnection;
ResultSet rs;

String myConnectionString =
"jdbc:mysql://*****/*****";
try {
dbConnection = DriverManager.getConnection(myConnectionString, "****", "****");

PreparedStatement ps=(PreparedStatement) dbConnection.prepareStatement("select AppID from Form_BL where AppID='1' ");
rs=ps.executeQuery();
rs.next();
java.sql.Blob b=rs.getBlob(1);
byte barr[]=new byte[(int)b.length()];//an array is created but contains no data
barr=b.getBytes(1,(int)b.length());

FileOutputStream fout=new FileOutputStream("D:/download.pdf");
fout.write(barr);

fout.close();
System.out.println("ok");

dbConnection.close();
ps.close();



}catch(Exception e){
e.printStackTrace();
}

}
}

我正在使用以下链接的引用进行此操作。 Upload PDF file to mysql BLOB by using java.sql.PreparedStatement without corruption .提前致谢

最佳答案

看起来将二进制内容(PDF 文件)保存到 BLOB 中的代码没问题。但是,当您尝试取回 PDF 内容时,您似乎遇到了一些麻烦。

你可能想试试这个:

Blob b = rs.getBlob(1);
InputStream is = b.getBinaryStream();
FileOutputStream fos = new FileOutputStream("D:/download.pdf");
int i = 0;

// IMPORTANT: Remember to handle/close all I/O properly
while ((i = is.read()) != -1) {
fos.write(b);
}

关于java - 从mysql java下载后PDF文件损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32617356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com