要将InputStream转换为File,您可以使用FileOutputStream来实现。以下是一个示例代码:

public static File convertInputStreamToFile(InputStream inputStream, String filePath) throws IOException {
    File file = new File(filePath);
    try (FileOutputStream outputStream = new FileOutputStream(file)) {
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
    }
    return file;
}

您可以调用这个方法并传入您的InputStream和要保存文件的路径。该方法会将InputStream中的数据写入到指定的文件中,并返回该文件对象。请注意,如果文件已存在,它将被覆盖。

Java InputStream 转 File 文件 - 完整代码示例

原文地址: https://www.cveoy.top/t/topic/qnjt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录