iT邦幫忙

0

[Java] 實作基本的檔案存入資料庫並取出另存新檔

  • 分享至 

  • xImage
  •  

以下分享實作基本的檔案存入資料庫並取出另存新檔功能範例和說明

需用到的import

import java.io.*; 
import java.sql.*;
import org.apache.commons.codec.binary.Base64;

範例是使用MSSQL,所以還需要下載使用JDBC驅動程式 sqljdbc4-4.0.jar

public class IoDemo { 
    public static void main(String[] args) { 
        String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; 
        String url = "jdbc:sqlserver://0.0.0.1:1433;databaseName=Demo" ;
        String user = "sa"; 
        String password = "sa123"; 
      
        try { 
            Class.forName(driver); 
            Connection conn = null;
            PreparedStatement pstmt = null;

            try {
                conn = DriverManager.getConnection( url, user, password); 

             // 取得檔案 
                String filePath = "D:\\tmp\\Test.zip";
    	        String base64Str = "";
    	        try {
    	            base64Str = fileToBase64(filePath);
    	        } catch (IOException e) {
    	            e.printStackTrace();
    	        }
    	        System.out.println("结果:"+ base64Str);
    	    
                // 寫入資料庫 
                pstmt = conn.prepareStatement( "INSERT INTO FILETEMP VALUES(?, ?)"); 
                pstmt.setString(1, "Test.zip"); 
                pstmt.setString(2, base64Str); 
                pstmt.executeUpdate(); 
                pstmt.clearParameters(); 

            }
            catch(SQLException e) { 
                e.printStackTrace(); 
            }  
            finally {
                if(pstmt != null) {
                    try {
                        pstmt.close();
                    }   
                    catch(SQLException e) {
                        e.printStackTrace();
                    }
                }
            } 

            Statement stmt = null;
            try {
                // 從資料庫取出檔案 
                stmt = conn.createStatement(); 
                ResultSet result = stmt.executeQuery("SELECT TOP 1 * FROM FILETEMP"); 
                result.next(); 
                String FILENAME = result.getString(1);                
                String FILEBYTE = result.getString(2); 

                // 寫入檔案 
                System.out.println("檔案名稱:" + FILENAME); 
      
                String targetFilePath = "D:\\tmp\\NewTest.zip";
    	        base64ToFile(FILEBYTE, targetFilePath);     
            } 
            catch(SQLException e) { 
                e.printStackTrace(); 
            } 
            finally {
                if(stmt != null) {
                    try {
                        stmt.close();
                    }   
                    catch(SQLException e) {
                        e.printStackTrace();
                    }
                }
                if(conn != null) {
                    try {
                        conn.close();
                    }
                    catch(SQLException e) {
                        e.printStackTrace();
                    }
                }
            } 
        }
        catch(ClassNotFoundException e) { 
            System.out.println("找不到驅動程式"); 
            e.printStackTrace(); 
        } 
    } 
       
    
    public static String fileToBase64(String filePath) throws IOException {
        File file = new File(filePath);
        FileInputStream inputFile = null;
        byte[] buffer = null;
        try {
            inputFile = new FileInputStream(file);
            BufferedInputStream bin = new BufferedInputStream(inputFile);
            buffer = new byte[(int) file.length()];
            inputFile.read(buffer);            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != inputFile) {
                inputFile.close();
            }
        }
        byte[] bs = Base64.encodeBase64(buffer);
        return new String(bs);
    }

    
    public static void base64ToFile(String base64Str, String targetFilePath) {
        byte[] buffer = Base64.decodeBase64(base64Str);
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(targetFilePath);
            out.write(buffer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }  
}  

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言