如何在Java中将InputStream转换为File

如何在Java中将InputStream转换为File以下是一些将 InputStream 转换为 FileJava 示例手动将 InputStream 复制到 FileOutputSt FileUtils copyInputStr 7NIOFiles copy1 FileOutputSt 1 我们必须将数据从 InputStream 手动复

以下是一些将InputStream转换为File Java示例

  1. 手动将InputStream复制到FileOutputStream
  2. Apache Commons IO – FileUtils.copyInputStreamToFile
  3. Java 1.7 NIO Files.copy

1. FileOutputStream

1.1我们必须将数据从InputStream手动复制到OutputStream

InputStreamToFile.java
package com.mkyong; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URI; public class InputStreamToFile { private static final String FILE_TO = "d:\\download\\google.txt"; public static void main(String[] args) throws IOException { URI u = URI.create("https://www.google.com/"); try (InputStream inputStream = u.toURL().openStream()) { File file = new File(FILE_TO); copyInputStreamToFile(inputStream, file); } } // InputStream -> File private static void copyInputStreamToFile(InputStream inputStream, File file) throws IOException { try (FileOutputStream outputStream = new FileOutputStream(file)) { int read; byte[] bytes = new byte[1024]; while ((read = inputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, read); } // commons-io //IOUtils.copy(inputStream, outputStream); } } }

2. Apache Commons IO

2.1 FileUtils.copyInputStreamToFile在Apache Commons IO中可用

pom.xml
 
    
    
      commons-io 
     
    
      commons-io 
     
    
      2.6 
     
   
InputStreamToFile2.java
package com.mkyong; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URI; public class InputStreamToFile2 { private static final String FILE_TO = "d:\\download\\google.txt"; public static void main(String[] args) throws IOException { URI u = URI.create("https://www.google.com/"); try (InputStream inputStream = u.toURL().openStream()) { File file = new File(FILE_TO); // commons-io FileUtils.copyInputStreamToFile(inputStream, file); } } }

3. Java 1.7 NIO

3.1如果只想将inputStream保存到某个文件中,请尝试使用Java 1.7 NIO Files.copy

InputStreamToFile3.java
package com.mkyong; import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.nio.file.Files; import java.nio.file.Paths; public class InputStreamToFile3 { private static final String FILE_TO = "d:\\download\\google.txt"; public static void main(String[] args) throws IOException { URI u = URI.create("https://www.google.com/"); try (InputStream inputStream = u.toURL().openStream()) { //Java 1.7 Files.copy(inputStream, Paths.get(FILE_TO)); } } }

4.旧时光

4.1在过去的Java 1.7之前,我们必须手动关闭所有资源。

InputStreamToFile4.java
package com.mkyong; import java.io.*; public class InputStreamToFile4 { public static void main(String[] args) { InputStream inputStream = null; OutputStream outputStream = null; try { // read this file into InputStream inputStream = new FileInputStream("/Users/mkyong/holder.js"); // write the inputStream to a FileOutputStream outputStream = new FileOutputStream(new File("/Users/mkyong/holder-new.js")); int read = 0; byte[] bytes = new byte[1024]; while ((read = inputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, read); } System.out.println("Done!"); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

5.将文件转换为InputStream

这很容易:

File file = new File("d:\\download\\google.txt"); InputStream inputStream = new FileInputStream(file);

注意
您可能对此String的InputStream感兴趣

参考文献

翻译自: https://mkyong.com/java/how-to-convert-inputstream-to-file-in-java/


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/210518.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 上午7:29
下一篇 2026年3月19日 上午7:30


相关推荐

  • 预写式日志(Write-Ahead Logging (WAL))

    预写式日志(Write-Ahead Logging (WAL))

    2021年11月25日
    44
  • eXtremeDB简介

    eXtremeDB简介eXtremeDB 最短路径最快速的数据管理 eXtremeDB 是美国麦科捷科技有限公司 McObjectLLC 开发的一款专业的嵌入式实时内存数据库 它具有高性能 低开销 稳定可靠的极速实时数据管理能力 应用领域有军事 航空航天 网络和电信 工业控制 电子消费 远程信息处理 金融 企业 医疗 核心版本 eXtremeDB 内存版 eXtr

    2026年3月19日
    2
  • ​内嵌物理知识神经网络(PINN)是个坑吗?

    ​内嵌物理知识神经网络(PINN)是个坑吗?PaperWeekly 原创 作者 zwqwo 单位 某知名券商计算机行业研究员研究方向 关注国产 CAD CAE 等工业软件发展从无网格方法到内嵌物理知识的神经网络内嵌物理知

    2026年3月18日
    3
  • 确定手机imsi号码的方法

    确定手机imsi号码的方法目录 1 Imsi 概述 12 获得 imsi 的两种方法 22 1 读卡器读出 sim 卡上 imsi22 2 利用 wireshark 获得 imsi22 3wireshark 获得 imsi 的具体过程 22 3 1 拷贝抓包服务器软件 rpcapd 到 fcs 的服务器 linux 目录 usr bin 下 22 3 2Fcs 服务器里运行加载 rpcap 抓包服务器进程 52

    2026年3月19日
    1
  • mpvue还在维护吗_laravel

    mpvue还在维护吗_laravelmain.js//main.js//将fly实例挂在vue原型上,在然而你和组件中通过this使用flyvarFly=require("flyio/dist/npm/wx")varfly=newFlyfly.config.baseURL=’http://xx.xx.xx.xx:xxxx/api/v3/’//配置请求基地址Vue.prototype.$http=fly/…

    2025年10月2日
    4
  • 关于头文件中的 static inline函数

    关于头文件中的 static inline函数关于头文件中的 staticinline 函数 nbsp nbsp 头文件中常见 staticinline 函数 于是思考有可能遇到的问题 如头文件经常会被包含会不会产生很多副本 网上说法不一 于是自己验证 经过 arm none eabi gcc 下测试后得出结论 nbsp nbsp inline 关键字实际上仅是建议内联并不强制内联 gcc 中 O0 优化时是不内联的 即使是 O2 以上 如果该函数被作为函数指针赋值

    2026年3月18日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号