网站首页 > java教程 正文
在Java中,你可以使用`java.nio.file`包和`java.io`包来实现文件的上传和下载。以下是一个简单的示例,展示了如何使用Java实现文件的上传和下载。
### 1. 文件上传
```java
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FileUploader {
public static void uploadFile(InputStream inputStream, String destinationPath) {
FileOutputStream outputStream = null;
try {
// 创建目标文件
File destinationFile = new File(destinationPath);
outputStream = new FileOutputStream(destinationFile);
// 从输入流读取数据并写入目标文件
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
System.out.println("文件上传成功: " + destinationPath);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// 假设有一个输入流,例如从网络或用户上传的文件
InputStream inputStream = FileUploader.class.getResourceAsStream("/example.txt");
String destinationPath = "uploads/example.txt";
// 确保目标目录存在
Path uploadPath = Paths.get("uploads");
if (!Files.exists(uploadPath)) {
try {
Files.createDirectories(uploadPath);
} catch (IOException e) {
e.printStackTrace();
}
}
// 上传文件
uploadFile(inputStream, destinationPath);
}
}
```
### 2. 文件下载
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FileDownloader {
public static void downloadFile(String sourcePath, OutputStream outputStream) {
FileInputStream inputStream = null;
try {
// 打开源文件
File sourceFile = new File(sourcePath);
inputStream = new FileInputStream(sourceFile);
// 从源文件读取数据并写入输出流
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
System.out.println("文件下载成功: " + sourcePath);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// 假设有一个输出流,例如发送到网络或用户下载的文件
String sourcePath = "uploads/example.txt";
OutputStream outputStream = System.out; // 这里使用System.out作为示例输出流
// 下载文件
downloadFile(sourcePath, outputStream);
}
}
```
### 说明:
1. **文件上传**:`uploadFile`方法接收一个`InputStream`和一个目标路径,将输入流中的数据写入目标文件。
2. **文件下载**:`downloadFile`方法接收一个源文件路径和一个`OutputStream`,将源文件中的数据写入输出流。
### 注意事项:
- 在实际应用中,`InputStream`和`OutputStream`可能来自网络请求、用户上传、或其他来源。
- 确保目标目录存在,否则需要先创建目录。
- 处理文件时要注意异常处理和资源释放,避免内存泄漏。
希望这个示例能帮助你实现文件的上传和下载功能!如果你有更多问题,欢迎继续提问。
猜你喜欢
- 2025-03-12 Java应用如何实现日志归集
- 2025-03-12 SpringBoot应用中下载文件的3种实现方式
- 2025-03-12 2020-12-11:多个线程同时写同一个日志文件,为...
- 2025-03-12 面试官, 你Java项目是如何实现读写分离的?Java代码实战
- 2025-03-12 用JAVA代码写一个JAVA代码文本
你 发表评论:
欢迎- 最近发表
-
- Java常量定义防暴指南:从"杀马特"到"高富帅"的华丽转身
- Java接口设计原则与实践:优雅编程的艺术
- java 包管理、访问修饰符、static/final关键字
- Java工程师的代码规范与最佳实践:优雅代码的艺术
- 编写一个java程序(编写一个Java程序计算并输出1到n的阶乘)
- Mycat的搭建以及配置与启动(mycat部署)
- Weblogic 安装 -“不是有效的 JDK Java 主目录”解决办法
- SpringBoot打包部署解析:jar包的生成和结构
- 《Servlet》第05节:创建第一个Servlet程序(HelloSevlet)
- 你认为最简单的单例模式,东西还挺多
- 标签列表
-
- java反编译工具 (77)
- java反射 (57)
- java接口 (61)
- java随机数 (63)
- java7下载 (59)
- java数据结构 (61)
- java 三目运算符 (65)
- java对象转map (63)
- Java继承 (69)
- java字符串替换 (60)
- 快速排序java (59)
- java并发编程 (58)
- java api文档 (60)
- centos安装java (57)
- java调用webservice接口 (61)
- java深拷贝 (61)
- 工厂模式java (59)
- java代理模式 (59)
- java.lang (57)
- java连接mysql数据库 (67)
- java重载 (68)
- java 循环语句 (66)
- java反序列化 (58)
- java时间函数 (60)
- java是值传递还是引用传递 (62)
本文暂时没有评论,来添加一个吧(●'◡'●)