2012-04-19 49 views
0

我想下載視頻文件作爲blob存儲在Mysql中。該文件下載得很好,但它被損壞我猜。下載的格式是ogg,webm n mp3。問題是,我試圖使用ffmpeg轉換任何視頻,它說「在處理過程中發現無效數據」。從java中的數據庫下載視頻文件

我使用下面的代碼

Blob image = null; 
    Connection con = null; 
    ResultSet rs = null; 
    try{ 
    Class.forName("com.mysql.jdbc.Driver"); 
    con = MySqlConnect.getDBConnection(); 
    String sql = "select videos, filename from sun.videostore where id ='"+fileID+"'"; 
    Statement stmt = con.createStatement(); 
    rs = stmt.executeQuery(sql); 
    while (rs.next()) { 
    String filename = rs.getString("filename"); 
    Blob video = rs.getBlob("videos"); 
    File file1 = new File("C:\\DowloadFile\\" + filename); 
    FileOutputStream foStream = new FileOutputStream(file1); 
    if(video != null){ 
     int length = (int) video.length(); 
     InputStream is2 = video.getBinaryStream(); 
     int b = 0; 
     while(b!=-1){ 
     b=is2.read(); 
     foStream.write(b); 
     } 

    } 

    }   
    }catch(Exception e){ 
    System.out.println("Ecxeption in getting data from DB = "+e); 
    }' 
+0

我會檢查一下,如果視頻肯定沒有在數據庫中損壞。使用像MySQL Query Browser這樣的工具,將blob下載到文件中,然後運行ffmpeg。 – 0xCAFEBABE 2012-04-19 06:12:32

+0

Thanx的快速回復....我會檢查tat ...是否有需要設置編解碼器後,你寫入字節文件在Java中。我在ogg上試過這段代碼,視頻播放的是Firefox。但是不在chrome上,這是我面臨的另一個問題 – deepika 2012-04-19 06:15:48

+0

編解碼器通常包含在文件頭中。我已經將視頻,圖片和音樂存儲在mysql數據庫中,然後直接將字節轉儲爲文件和播放。一旦轉儲文件,應該不需要設置編解碼器信息。聽起來像文件在上傳到數據庫之前已損壞,如@ 0xCAFEBABE提及。 – Namphibian 2012-04-19 07:51:42

回答

1

人無我有檢查了我上傳的文件,它沒有損壞....我已經嘗試了不同的方式來將視頻上傳到數據庫。

File video = new File(filename);         
fis = new FileInputStream(video); 
ps.setBinaryStream(2, fis, (int) video.length());//preparedStatement 

如果我以上述方式上傳文件,我得到一個正確的文件下載。

+0

如果它解決了你的問題。點擊正確的標誌接受答案。 – 2012-04-20 05:53:37