java如何实现保存文件到本地
发布时间:2020-04-30 11:20:24
来源:亿速云
阅读:773
作者:小新
java如何实现保存文件到本地?相信有很多人都不太了解,今天小编为了让大家更加了解java,所以给大家总结了以下内容,一起往下看吧。
具体实现过程请看如下实例:private void savePic(InputStream inputStream, String fileName) {
OutputStream os = null;
try {
String path = "D:\\testFile\\";
// 2、保存到临时文件
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流保存到本地文件
File tempFile = new File(path);
if (!tempFile.exists()) {
tempFile.mkdirs();
}
os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
// 开始读取
while ((len = inputStream.read(bs)) != -1) {
os.write(bs, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 完毕,关闭所有链接
try {
os.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
关于java如何实现保存文件到本地就分享到这里了,希望以上内容可以对大家有一定的参考价值,可以学以致用。如果喜欢本篇文章,不妨把它分享出去让更多的人看到。