文件下载,从服务器,输入流
/**
*
* @param inputStream 输入流
* @param filepath 文件输出路径包含完整文件名
*/
public static void downloadFile(InputStream inputStream,String filepath){
try {
FileOutputStream file = new FileOutputStream(filepath);
int len = (int) inputStream.available();
byte[] buffer = new byte[len]; // 建立缓冲区
while ((len = inputStream.read(buffer)) != -1) {
file.write(buffer, 0, len);
}
file.close();
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
文件下载,从数据库,blob
其实同理,只是多了一步操作,将blob转为输入流
/**
*blob数据存储到本地
* @param fileBlob blob数据
* @param filepath 文件输出路径包含完整文件名
*/
public static void downloadBlob(Blob fileBlob,String filepath){
try {
InputStream binaryStream = fileBlob.getBinaryStream();
FileOutputStream file = new FileOutputStream(filepath);
int len = (int) fileBlob.length();
byte[] buffer = new byte[len]; // 建立缓冲区
while ((len = binaryStream.read(buffer)) != -1) {
file.write(buffer, 0, len);
}
file.close();
binaryStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}