|
@@ -1,12 +1,14 @@
|
|
package com.github.jfcloud.gene.file.service.impl;
|
|
package com.github.jfcloud.gene.file.service.impl;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
|
+import cn.hutool.core.io.IoUtil;
|
|
import cn.hutool.core.io.unit.DataSizeUtil;
|
|
import cn.hutool.core.io.unit.DataSizeUtil;
|
|
import cn.hutool.core.lang.Assert;
|
|
import cn.hutool.core.lang.Assert;
|
|
import cn.hutool.core.util.ReflectUtil;
|
|
import cn.hutool.core.util.ReflectUtil;
|
|
import cn.hutool.crypto.digest.DigestUtil;
|
|
import cn.hutool.crypto.digest.DigestUtil;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.amazonaws.services.s3.AmazonS3;
|
|
import com.amazonaws.services.s3.AmazonS3;
|
|
|
|
+import com.amazonaws.services.s3.model.S3Object;
|
|
import com.amazonaws.services.s3.model.S3ObjectSummary;
|
|
import com.amazonaws.services.s3.model.S3ObjectSummary;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
@@ -53,20 +55,17 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> i
|
|
public FileVo uploadFileWithFileName(InputStream fileInputStream, String fileName) {
|
|
public FileVo uploadFileWithFileName(InputStream fileInputStream, String fileName) {
|
|
Assert.notNull(fileInputStream, "FileInputStream can not be null");
|
|
Assert.notNull(fileInputStream, "FileInputStream can not be null");
|
|
|
|
|
|
- FileVo vo = new FileVo();
|
|
|
|
- vo.setName(fileName);
|
|
|
|
|
|
+ String bucketName = ossProperties.getBucketName();
|
|
|
|
+ FileVo vo = new FileVo(bucketName, fileName);
|
|
|
|
|
|
try {
|
|
try {
|
|
- String bucketName = ossProperties.getBucketName();
|
|
|
|
fileTemplate.removeObject(bucketName, fileName);
|
|
fileTemplate.removeObject(bucketName, fileName);
|
|
fileTemplate.putObject(bucketName, fileName, fileInputStream);
|
|
fileTemplate.putObject(bucketName, fileName, fileInputStream);
|
|
-
|
|
|
|
- vo.setUrl(String.format("/admin/sys-file/%s/%s", bucketName, fileName));
|
|
|
|
|
|
+ return vo;
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("上传失败", e);
|
|
log.error("上传失败", e);
|
|
- Assert.isTrue(false, "附件生成上传失败");
|
|
|
|
|
|
+ throw new RuntimeException("附件生成上传失败");
|
|
}
|
|
}
|
|
- return vo;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -124,4 +123,20 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> i
|
|
}
|
|
}
|
|
return fileDetail;
|
|
return fileDetail;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public InputStream getStreamByUrl(String url) {
|
|
|
|
+ url = url.replace("/admin/sys-file/", "");
|
|
|
|
+ String bucketName = url.substring(0, url.indexOf("/"));
|
|
|
|
+ String fileName = url.substring(url.indexOf("/") + 1);
|
|
|
|
+
|
|
|
|
+ //s3Object会自动关闭,所以需要复制流
|
|
|
|
+ try (S3Object s3Object = fileTemplate.getObject(bucketName, fileName);
|
|
|
|
+ ByteArrayOutputStream out = new ByteArrayOutputStream()) {
|
|
|
|
+ IoUtil.copy(s3Object.getObjectContent(), out);
|
|
|
|
+ return new ByteArrayInputStream(out.toByteArray());
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("文件读取异常: {}", e.getLocalizedMessage());
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
}
|
|
}
|