Save the blob file retrieved from the database to a folder on the hard disk

package test.unit.channelinfo;


import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Blob;

public class BlobUtil{
	
	 public static void ExportBlob(String path,String fileName, Blob myBlob) throws Exception  
	  {      
		File file=new File(path+"\\"+fileName);
		if(!file.exists()){
		    File binaryFile = new File(path);
		    if(!binaryFile.exists()){
		    	binaryFile.mkdirs();
		    }
		    FileOutputStream outStream = new FileOutputStream(binaryFile+"\\"+fileName);  
		    InputStream inStream = myBlob.getBinaryStream();  
		    int size=(int) myBlob.length();
		    byte[] buffer = new byte[size];  
		    int length = -1;  
		  
		    while ((length = inStream.read(buffer)) != -1)  
		    {  
		      outStream.write(buffer, 0, length);  
		      outStream.flush();  
		    }  
		  
		    inStream.close();  
		    outStream.close();  
		  }   
	  }
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324777737&siteId=291194637