用工作流实现文件上传下载的Java代码示例
时间: 2024-04-21 13:26:33 浏览: 123
Java文件上传与下载的例子
以下是一个简单的文件上传和下载的 Java 代码示例,使用了工作流(Workflow)实现:
文件上传:
```java
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
import com.amazonaws.services.s3.transfer.Upload;
public class FileUpload {
public static void main(String[] args) throws Exception {
// 1. 读取配置文件
Properties props = new Properties();
InputStream in = new FileInputStream("config.properties");
props.load(in);
// 2. 创建 Amazon S3 客户端
String accessKey = props.getProperty("accessKey");
String secretKey = props.getProperty("secretKey");
String region = props.getProperty("region");
BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secretKey);
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.withRegion(region)
.build();
// 3. 创建 TransferManager
TransferManager transferManager = TransferManagerBuilder.standard()
.withS3Client(s3Client)
.build();
// 4. 从 HTTP 请求中获取文件
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
FileItem item = upload.parseRequest(request).get(0);
String fileName = item.getName();
// 5. 上传文件到 Amazon S3
PutObjectRequest putRequest = new PutObjectRequest(
props.getProperty("bucketName"), fileName, item.getInputStream(), new ObjectMetadata());
Upload upload = transferManager.upload(putRequest);
upload.waitForCompletion();
// 6. 删除临时文件
FileUtils.deleteQuietly(new File(item.getName()));
}
}
```
文件下载:
```java
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
import com.amazonaws.services.s3.transfer.Download;
public class FileDownload {
public static void main(String[] args) throws Exception {
// 1. 读取配置文件
Properties props = new Properties();
InputStream in = new FileInputStream("config.properties");
props.load(in);
// 2. 创建 Amazon S3 客户端
String accessKey = props.getProperty("accessKey");
String secretKey = props.getProperty("secretKey");
String region = props.getProperty("region");
BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secretKey);
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.withRegion(region)
.build();
// 3. 创建 TransferManager
TransferManager transferManager = TransferManagerBuilder.standard()
.withS3Client(s3Client)
.build();
// 4. 从 Amazon S3 下载文件
S3Object object = s3Client.getObject(props.getProperty("bucketName"), props.getProperty("fileName"));
Download download = transferManager.download(
props.getProperty("bucketName"), props.getProperty("fileName"), new File(props.getProperty("localPath")));
download.waitForCompletion();
// 5. 关闭 Amazon S3 客户端和 TransferManager
object.close();
transferManager.shutdownNow();
}
}
```
以上代码示例使用了 Amazon S3 作为文件存储和下载服务,需要提供以下配置信息:
- `accessKey`: AWS 访问密钥 ID
- `secretKey`: AWS 秘密访问密钥
- `region`: AWS 区域
- `bucketName`: 存储桶名称
- `fileName`: 文件名称
- `localPath`: 下载到本地的路径
阅读全文