Procházet zdrojové kódy

下载文档添加文件名和文件大小

陈长荣 před 3 měsíci
rodič
revize
0dfdcccb03

+ 5 - 0
src/main/java/com/github/jfcloud/excel/editor/docdeal/controller/FileController.java

@@ -4,6 +4,7 @@ package com.github.jfcloud.excel.editor.docdeal.controller;
 import cn.hutool.core.io.IoUtil;
 import cn.hutool.core.io.file.FileNameUtil;
 import cn.hutool.core.lang.Pair;
+import cn.hutool.core.net.URLEncodeUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.http.HttpResponse;
 import cn.hutool.http.HttpUtil;
@@ -106,6 +107,10 @@ public class FileController {
         }
         log.info("==> 下载文档: name={}, bucket={}", name, bucket);
         try {
+            response.setContentType("application/octet-stream");
+            response.setHeader("Content-Disposition", "attachment;filename*=utf8''" + URLEncodeUtil.encode(name));
+            response.setHeader("Content-Length", String.valueOf(ossTemplate.getSize(bucket, name)));
+
             S3Object s3Object = ossTemplate.getObject(bucket, name);
             IoUtil.copy(s3Object.getObjectContent(), response.getOutputStream());
         } catch (IOException e) {

+ 14 - 6
src/main/java/com/github/jfcloud/excel/editor/docdeal/oss/service/OssTemplate.java

@@ -27,6 +27,10 @@ public class OssTemplate implements InitializingBean {
     private final OssProperties ossProperties;
     private AmazonS3 amazonS3;
 
+    public OssTemplate(OssProperties ossProperties) {
+        this.ossProperties = ossProperties;
+    }
+
     public void createBucket(String bucketName) {
         try {
             if (!this.amazonS3.doesBucketExistV2(bucketName)) {
@@ -94,8 +98,16 @@ public class OssTemplate implements InitializingBean {
         }
     }
 
+    /**
+     * 获取文件大小
+     */
+    public long getSize(String bucketName, String objectName) {
+        List<S3ObjectSummary> apks = amazonS3.listObjects(bucketName, objectName).getObjectSummaries();
+        return apks.isEmpty() ? 0 : apks.get(0).getSize();
+    }
+
     public void putObject(String bucketName, String objectName, InputStream stream) throws Exception {
-        this.putObject(bucketName, objectName, stream, (long)stream.available(), "application/octet-stream");
+        this.putObject(bucketName, objectName, stream, (long) stream.available(), "application/octet-stream");
     }
 
     public PutObjectResult putObject(String bucketName, String objectName, InputStream stream, long size, String contextType) throws Exception {
@@ -137,10 +149,6 @@ public class OssTemplate implements InitializingBean {
         AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(this.ossProperties.getEndpoint(), this.ossProperties.getRegion());
         AWSCredentials awsCredentials = new BasicAWSCredentials(this.ossProperties.getAccessKey(), this.ossProperties.getSecretKey());
         AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
-        this.amazonS3 = (AmazonS3)((AmazonS3ClientBuilder)((AmazonS3ClientBuilder)((AmazonS3ClientBuilder)((AmazonS3ClientBuilder)((AmazonS3ClientBuilder) AmazonS3Client.builder().withEndpointConfiguration(endpointConfiguration)).withClientConfiguration(clientConfiguration)).withCredentials(awsCredentialsProvider)).disableChunkedEncoding()).withPathStyleAccessEnabled(this.ossProperties.getPathStyleAccess())).build();
-    }
-
-    public OssTemplate(OssProperties ossProperties) {
-        this.ossProperties = ossProperties;
+        this.amazonS3 = (AmazonS3) ((AmazonS3ClientBuilder) ((AmazonS3ClientBuilder) ((AmazonS3ClientBuilder) ((AmazonS3ClientBuilder) ((AmazonS3ClientBuilder) AmazonS3Client.builder().withEndpointConfiguration(endpointConfiguration)).withClientConfiguration(clientConfiguration)).withCredentials(awsCredentialsProvider)).disableChunkedEncoding()).withPathStyleAccessEnabled(this.ossProperties.getPathStyleAccess())).build();
     }
 }