feat: 本地下载功能
增加本地文件下载功能
This commit is contained in:
@@ -361,7 +361,7 @@ public class ReportManageConroller {
|
||||
}else if ("alioss".equals(uploadType)){
|
||||
ossClientUtil.downloadFileFromOSS(entity.getSavePath(),response);
|
||||
}else if ("local".equals(uploadType)){
|
||||
//do nothing
|
||||
localFileUtil.downFile(entity.getSavePath(), response);
|
||||
}
|
||||
|
||||
} catch (Exception ex) {
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
package com.adc.da.report.util;
|
||||
|
||||
import com.adc.da.util.exception.AdcDaBaseException;
|
||||
import com.obs.services.exception.ObsException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
/**
|
||||
* @author Caihaohan
|
||||
@@ -56,4 +61,26 @@ public class LocalFileUtil {
|
||||
inputStream.close();
|
||||
outputStream.close();
|
||||
}
|
||||
|
||||
public void downFile(String savePath, HttpServletResponse response) throws IOException {
|
||||
String filePath = uploadFile + savePath;
|
||||
|
||||
//复制到本地
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
|
||||
getFileFromLocal(savePath, fileOutputStream);
|
||||
|
||||
//将文件写到response中
|
||||
try (InputStream objectContent = Files.newInputStream(Paths.get(filePath))) {
|
||||
byte[] bt = new byte[1024 * 1024];
|
||||
int len = 0;
|
||||
while ((len = objectContent.read(bt)) != -1) {
|
||||
response.getOutputStream().write(bt, 0, len);
|
||||
}
|
||||
} catch (ObsException | IOException oe) {
|
||||
throw new AdcDaBaseException("获取本地文件失败");
|
||||
} finally {
|
||||
// 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
|
||||
response.getOutputStream().close();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user