|
@@ -5,6 +5,7 @@ import org.apache.commons.codec.digest.DigestUtils;
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.*;
|
|
@@ -14,6 +15,7 @@ import java.io.*;
|
|
|
* @Author: CaoZixuan
|
|
|
* @Date: 2021-08-17
|
|
|
*/
|
|
|
+@Component
|
|
|
public class FileStoreUtil {
|
|
|
private final static Logger log = LoggerFactory.getLogger(FileStoreUtil.class);
|
|
|
|
|
@@ -23,13 +25,13 @@ public class FileStoreUtil {
|
|
|
/**
|
|
|
* 上传文件
|
|
|
*
|
|
|
- * @param dirName 上传到地址
|
|
|
+ * @param dirName 上传到地址
|
|
|
* @param inputStream 流
|
|
|
- * @param type fileStore类型
|
|
|
+ * @param type fileStore类型
|
|
|
*/
|
|
|
public void ossUpload(String dirName, InputStream inputStream, String type) throws Exception {
|
|
|
log.info("ossUpload is come in");
|
|
|
- fileService.getFileStore(type).write(dirName,inputStream,DigestUtils.md5Hex(inputStream));
|
|
|
+ fileService.getFileStore(type).write(dirName, inputStream, DigestUtils.md5Hex(inputStream));
|
|
|
log.info("dirName:{}", dirName);
|
|
|
}
|
|
|
|
|
@@ -37,12 +39,12 @@ public class FileStoreUtil {
|
|
|
* 上传文件
|
|
|
*
|
|
|
* @param dirName 上传到地址
|
|
|
- * @param file 文件
|
|
|
- * @param type fileStore类型
|
|
|
+ * @param file 文件
|
|
|
+ * @param type fileStore类型
|
|
|
*/
|
|
|
- public void ossUpload(String dirName, File file,String type) throws Exception {
|
|
|
+ public void ossUpload(String dirName, File file, String type) throws Exception {
|
|
|
log.info("ossUpload is come in");
|
|
|
- fileService.getFileStore(type).write(dirName,new FileInputStream(file), DigestUtils.md5Hex(new FileInputStream(file)));
|
|
|
+ fileService.getFileStore(type).write(dirName, new FileInputStream(file), DigestUtils.md5Hex(new FileInputStream(file)));
|
|
|
log.info("dirName:{}", dirName);
|
|
|
}
|
|
|
|
|
@@ -51,25 +53,25 @@ public class FileStoreUtil {
|
|
|
*
|
|
|
* @param dirName 上传到地址
|
|
|
* @param content 文件
|
|
|
- * @param type fileStore类型
|
|
|
+ * @param type fileStore类型
|
|
|
*/
|
|
|
- public void ossUpload(String dirName, String content,String type) throws Exception {
|
|
|
+ public void ossUpload(String dirName, String content, String type) throws Exception {
|
|
|
log.info("ossUpload is come in");
|
|
|
- fileService.getFileStore(type).write(dirName,new ByteArrayInputStream(content.getBytes()), DigestUtils.md5Hex(new ByteArrayInputStream(content.getBytes())));
|
|
|
+ fileService.getFileStore(type).write(dirName, new ByteArrayInputStream(content.getBytes()), DigestUtils.md5Hex(new ByteArrayInputStream(content.getBytes())));
|
|
|
log.info("dirName:{}", dirName);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 从文件存储上下载文件到本地
|
|
|
*
|
|
|
- * @param dirName 文件地址
|
|
|
+ * @param dirName 文件地址
|
|
|
* @param localPath 本地路径
|
|
|
- * @param type fileStore类型
|
|
|
- * @throws IOException
|
|
|
+ * @param type fileStore类型
|
|
|
+ * @throws Exception 异常
|
|
|
*/
|
|
|
- public File ossDownload(String dirName, String localPath ,String type) throws Exception {
|
|
|
+ public File ossDownload(String dirName, String localPath, String type) throws Exception {
|
|
|
log.info("ossDownload is come in");
|
|
|
- return this.saveLocal(fileService.getFileStore(type).read(dirName),localPath);
|
|
|
+ return this.saveLocal(fileService.getFileStore(type).read(dirName), localPath);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -77,20 +79,33 @@ public class FileStoreUtil {
|
|
|
* 从文件存储上下载文件到byte[]
|
|
|
*
|
|
|
* @param objectName 文件地址
|
|
|
- * @param type fileStore类型
|
|
|
- * @throws Exception
|
|
|
+ * @param type fileStore类型
|
|
|
+ * @throws Exception 异常
|
|
|
*/
|
|
|
- public byte[] ossDownload(String objectName,String type) throws Exception {
|
|
|
+ public byte[] ossDownload(String objectName, String type) throws Exception {
|
|
|
log.info("oss Download is come in");
|
|
|
return IOUtils.toByteArray(fileService.getFileStore(type).read(objectName));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 从文件存储上下载文件到InputStream
|
|
|
+ *
|
|
|
+ * @param objectName 文件地址
|
|
|
+ * @param type fileStore类型
|
|
|
+ * @throws Exception 异常
|
|
|
+ */
|
|
|
+ public InputStream ossDownloadIs(String objectName, String type) throws Exception {
|
|
|
+ log.info("oss Download is come in");
|
|
|
+ return fileService.getFileStore(type).read(objectName);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 文件存在某本地路径
|
|
|
+ *
|
|
|
* @param inputStream 输入流
|
|
|
- * @param dirPath 本地路径
|
|
|
+ * @param dirPath 本地路径
|
|
|
* @return 存好的文件
|
|
|
- * @throws IOException
|
|
|
+ * @throws IOException 异常
|
|
|
*/
|
|
|
private File saveLocal(InputStream inputStream, String dirPath) throws IOException {
|
|
|
File desFile = new File(dirPath);
|
|
@@ -98,23 +113,16 @@ public class FileStoreUtil {
|
|
|
desFile.getParentFile().mkdirs(); //目标文件目录不存在的话需要创建目录
|
|
|
}
|
|
|
byte[] bytes = new byte[1024]; // 开辟一个拷贝缓冲区
|
|
|
- OutputStream outputStream = null;
|
|
|
- try {
|
|
|
- outputStream = new FileOutputStream(desFile);
|
|
|
+ try (OutputStream outputStream = new FileOutputStream(desFile)) {
|
|
|
int length;
|
|
|
while ((length = inputStream.read(bytes)) != -1) { //当读到尽头后,返回值为-1这个时候停止输出,拷贝结束
|
|
|
outputStream.write(bytes, 0, length);
|
|
|
}
|
|
|
return desFile;
|
|
|
- } catch (Exception e) {
|
|
|
- throw e;
|
|
|
} finally {
|
|
|
if (inputStream != null) {
|
|
|
inputStream.close();
|
|
|
}
|
|
|
- if (outputStream != null) {
|
|
|
- outputStream.close();
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
}
|