Inputstream对象转为fileinputstream
时间: 2023-07-07 17:45:26 浏览: 343
InputStream与OutputStream及File间互转
3星 · 编辑精心推荐
要将一个InputStream对象转换为FileInputStream对象,需要按以下步骤进行操作:
1. 创建一个临时文件
```
File tempFile = File.createTempFile("temp", ".tmp");
```
2. 将InputStream对象写入临时文件中
```
FileOutputStream outputStream = new FileOutputStream(tempFile);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
outputStream.close();
```
3. 创建一个FileInputStream对象
```
FileInputStream fileInputStream = new FileInputStream(tempFile);
```
注意:使用完毕后,需要删除临时文件
```
tempFile.delete();
```
阅读全文