簡體   English   中英

從mysql java下載后PDF文件損壞

[英]PDF file corrupted after downloading from mysql java

我正在嘗試將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();
  }

}
}

我正在使用以下鏈接的參考進行此操作。 通過使用java.sql.PreparedStatement將PDF文件上載到mysql BLOB,而不會損壞 提前致謝

將二進制內容(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); 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM