deepika
deepika

Reputation: 23

Downloading video file from Mysql DB in java

I am trying to download video file stored as blob in Mysql. The file gets downloaded fine but it gets corrupted i guess. The formats to downloaded are ogg, webm n mp3. The problem is that wen i try to convert any video using ffmpeg, it says "invalid data found during processing".

I am using the following code

    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);
  }'

Upvotes: 0

Views: 3492

Answers (1)

deepika
deepika

Reputation: 23

No i have checked my uploaded file, it is not corrupted....I have tried a different way to upload the video into DB.

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

If i upload the file in above way, i get a correct file on downloading.

Upvotes: 1

Related Questions