代码调整
This commit is contained in:
@@ -14,7 +14,9 @@ public enum OssResponseEnum {
|
||||
* A00550 ~ A00649
|
||||
*/
|
||||
UPLOAD_FILE_ERROR("A00551","上传文件服务器错误,请检查数据"),
|
||||
DOWNLOAD_FILE_ERROR("A00554","下载文件URL不存在,请检查数据")
|
||||
DOWNLOAD_FILE_URL_ERROR("A00554","下载文件URL不存在,请检查数据"),
|
||||
DOWNLOAD_FILE_STREAM_ERROR("A00555","文件服务器下载文件流异常"),
|
||||
DOWNLOAD_FILE_ERROR("A00556","文件服务器下载异常")
|
||||
|
||||
;
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.njcn.oss.utils;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.njcn.common.config.GeneralInfo;
|
||||
import com.njcn.common.pojo.exception.BusinessException;
|
||||
import com.njcn.huawei.obs.util.OBSUtil;
|
||||
@@ -10,10 +12,14 @@ import com.njcn.oss.constant.GeneralConstant;
|
||||
import com.njcn.oss.enums.OssResponseEnum;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.tomcat.util.http.fileupload.IOUtils;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
@@ -111,6 +117,79 @@ public class FileStorageUtil {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***
|
||||
* 根据文件路径获取文件流
|
||||
* @author hongawen
|
||||
* @date 2023/3/7 23:04
|
||||
* @param filePath 文件在服务器的路径
|
||||
*/
|
||||
public InputStream getFileStream(String filePath){
|
||||
InputStream inputStream;
|
||||
try {
|
||||
if (generalInfo.getBusinessFileStorage() == GeneralConstant.HUAWEI_OBS) {
|
||||
inputStream = obsUtil.downloadStream(filePath);
|
||||
} else {
|
||||
inputStream = minIoUtils.downloadStream(minIossProperties.getBucket(), filePath);
|
||||
}
|
||||
}catch (Exception exception){
|
||||
throw new BusinessException(OssResponseEnum.DOWNLOAD_FILE_STREAM_ERROR);
|
||||
}
|
||||
return inputStream;
|
||||
}
|
||||
|
||||
/***
|
||||
* 根据文件路径获取文件流并下载
|
||||
* @author hongawen
|
||||
* @date 2023/3/7 23:04
|
||||
* @param filePath 文件在服务器的路径
|
||||
*/
|
||||
public void downloadStream(HttpServletResponse response, String filePath) throws IOException {
|
||||
InputStream inputStream;
|
||||
OutputStream toClient = null;
|
||||
try{
|
||||
if (generalInfo.getBusinessFileStorage() == GeneralConstant.HUAWEI_OBS) {
|
||||
inputStream = obsUtil.downloadStream(filePath);
|
||||
} else {
|
||||
inputStream = minIoUtils.downloadStream(minIossProperties.getBucket(), filePath);
|
||||
}
|
||||
}catch (Exception exception){
|
||||
throw new BusinessException(OssResponseEnum.DOWNLOAD_FILE_STREAM_ERROR);
|
||||
}
|
||||
String fileType = filePath.substring(filePath.lastIndexOf('.')).toLowerCase();
|
||||
switch (fileType){
|
||||
case "jpg":
|
||||
case "jpeg":
|
||||
case "png":
|
||||
case "gif":
|
||||
response.setContentType(MediaType.IMAGE_PNG_VALUE);
|
||||
break;
|
||||
case "pdf":
|
||||
response.setContentType("application/pdf");
|
||||
response.setHeader("Content-Disposition", "attachment;filename=" + filePath);
|
||||
break;
|
||||
default:
|
||||
response.setContentType("application/octet-stream;charset=UTF-8");
|
||||
response.setHeader("Content-Disposition", "attachment;filename=" + filePath);
|
||||
break;
|
||||
}
|
||||
try {
|
||||
toClient = new BufferedOutputStream(response.getOutputStream());
|
||||
//通过IOUtils对接输入输出流,实现文件下载
|
||||
IOUtils.copy(inputStream, toClient);
|
||||
toClient.flush();
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException(OssResponseEnum.DOWNLOAD_FILE_STREAM_ERROR);
|
||||
} finally {
|
||||
IOUtils.closeQuietly(inputStream);
|
||||
IOUtils.closeQuietly(toClient);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/***
|
||||
* 根据文件路径删除指定文件对象
|
||||
* @author hongawen
|
||||
|
||||
Reference in New Issue
Block a user