编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

Java写上传下载(javaweb文件上传下载)

wxchong 2025-03-25 18:34:02 开源技术 7 ℃ 0 评论

在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`可能来自网络请求、用户上传、或其他来源。

- 确保目标目录存在,否则需要先创建目录。

- 处理文件时要注意异常处理和资源释放,避免内存泄漏。


希望这个示例能帮助你实现文件的上传和下载功能!如果你有更多问题,欢迎继续提问。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表