因SFTP流下载文件下载不全;改成SFTP下载到本地,再从本地流下载到页面

This commit is contained in:
曹磊 2024-05-27 19:43:12 +08:00
parent 1f767c3479
commit 873d80fe19
6 changed files with 233 additions and 123 deletions

View File

@ -19,7 +19,7 @@ import java.util.Properties;
public class SFTPUtil {
private static long count = 3;
private static long count1 = 0;
private static long sleepTime = 1800000;//30分钟
private static long sleepTime = 1000;//1秒
private static final Logger logger = LoggerFactory.getLogger(SFTPUtil.class);
private static Session sshSession = null;
private static ChannelSftp sftp = null;
@ -228,7 +228,7 @@ public class SFTPUtil {
public static Map<String,String> download(SftpConfig sftpConfig, String directory, String downloadFile, String saveFile) {
Map<String,String> map = new HashMap<String,String>();
map.put("code","0");
map.put("msg","删除成功");
map.put("msg","下载成功");
try{
if(sftp == null){
getChannelSftp(sftpConfig);
@ -270,6 +270,72 @@ public class SFTPUtil {
return map;
}
/**
* 下载文件
* @param directory 下载目录
* @param saveFile 存在本地的路径
*/
public static Map<String,String> download(SftpConfig sftpConfig, String directory, String saveFile) {
Map<String,String> map = new HashMap<String,String>();
map.put("code","0");
map.put("msg","下载成功");
String[] df = getDirectoryAndFileName(directory);
File localDirFile = new File(saveFile);
// 判断本地目录是否存在不存在需要新建各级目录
if (!localDirFile.exists()) {
localDirFile.mkdirs();
}
String newName = CommonUtils.getFileName(df[1]);
String newDownloadFile = saveFile.concat(File.separator).concat(newName);
File localFile = new File(newDownloadFile);
// 判断本地目录是否存在不存在需要新建各级目录
if (localFile.exists()) {
map.put("fileName",newDownloadFile);
return map;
}
try{
if(sftp == null){
getChannelSftp(sftpConfig);
if(sftp == null){
map.put("code","1");
map.put("msg","sftp链接异常");
return map;
}
}
OutputStream output = null;
try {
if (logger.isInfoEnabled()) {
logger.info("开始获取远程文件:[{}]---->[{}]", new Object[]{directory, saveFile});
}
// sftp.cd(directory);
if (logger.isInfoEnabled()) {
logger.info("打开远程文件:[{}]", new Object[]{directory});
}
output = new FileOutputStream(localFile);
sftp.get(directory, output);
if (logger.isInfoEnabled()) {
logger.info("文件下载成功");
}
map.put("fileName",newDownloadFile);
} catch (Exception e) {
if (logger.isInfoEnabled()) {
logger.info("文件下载出现异常,[{}]", e);
}
throw new RuntimeException("文件下载出现异常,[{}]", e);
} finally {
closeStream(null,output);
}
}catch (Exception e) {
map.put("code","1");
map.put("msg","sftp异常" + e.getMessage());
}
return map;
}
/**
* 网页下载文件
* @param response
@ -301,7 +367,7 @@ public class SFTPUtil {
// e1.printStackTrace();
// throw new Exception(StringUtils.format("sftp exception,sftp exception。 ", e1.getMessage()));
} finally {
closeStream(inputStream,outputStream);
closeStream(null,outputStream);
}
}catch (Exception e) {
e.printStackTrace();
@ -371,7 +437,7 @@ public class SFTPUtil {
break;
}
try {
Thread.sleep(1000);
Thread.sleep(sleepTime);
} catch (Exception ee) {
}
}
@ -467,6 +533,7 @@ public class SFTPUtil {
String path = sb.toString();
path = path.substring(0,path.length()-1);
map.put("data",path);
logger.info("移动文件成功....");
} catch (Exception e1) {
map.put("code","1");
map.put("msg",e1.getMessage());

View File

@ -111,7 +111,6 @@ public class ZyJxdgController extends JeecgController<ZyJxdg, IZyJxdgService> {
if(StringUtils.equals("pdf",nameLast)){
zyJxdg.setPdfPath(zyJxdg.getFilePath());
}else{
if(StringUtils.equals("local",uploadType)){
Global global = SpringContextHolder.getBean(Global.class);
FileUtil.mkdir(global.getContractDickPath());
@ -121,12 +120,10 @@ public class ZyJxdgController extends JeecgController<ZyJxdg, IZyJxdgService> {
String pdfPath = global.getContractDickPath() + namePath;
PDFUtil.office2PDF(docPath,pdfPath);
zyJxdg.setPdfPath("contract/"+namePath);
}else{
}else if(StringUtils.equals("sftp",uploadType)){
}
}
}else if(StringUtils.isNotEmpty(zyJxdg.getJxrlFilePath())){
String nameLast = zyJxdg.getJxrlFilePath().substring(zyJxdg.getJxrlFilePath().lastIndexOf(".")+1);
@ -143,7 +140,7 @@ public class ZyJxdgController extends JeecgController<ZyJxdg, IZyJxdgService> {
String pdfPath = global.getContractDickPath() + namePath;
PDFUtil.office2PDF(docPath,pdfPath);
zyJxdg.setJxrlPdfPath("contract/"+namePath);
}else{
}else if(StringUtils.equals("sftp",uploadType)){
}
@ -183,7 +180,6 @@ public class ZyJxdgController extends JeecgController<ZyJxdg, IZyJxdgService> {
update.set("file_path",zyJxdg.getFilePath());
update.set("pdf_path",zyJxdg.getFilePath());
}else{
if(StringUtils.equals("local",uploadType)) {
Global global = SpringContextHolder.getBean(Global.class);
FileUtil.mkdir(global.getContractDickPath());
@ -196,10 +192,9 @@ public class ZyJxdgController extends JeecgController<ZyJxdg, IZyJxdgService> {
System.out.println("pdfPath:" + pdfPath);
PDFUtil.office2PDF(docPath, pdfPath);
zyJxdg.setPdfPath("contract/" + namePath);
}else {
}else if(StringUtils.equals("sftp",uploadType)){
}
update.set("file_path",zyJxdg.getFilePath());
update.set("pdf_path",zyJxdg.getPdfPath());
}
@ -223,7 +218,6 @@ public class ZyJxdgController extends JeecgController<ZyJxdg, IZyJxdgService> {
}else {
}
update.set("jxrl_file_path",zyJxdg.getJxrlFilePath());
update.set("jxrl_pdf_path",zyJxdg.getJxrlPdfPath());
}
@ -357,7 +351,7 @@ public class ZyJxdgController extends JeecgController<ZyJxdg, IZyJxdgService> {
// Map<String, String> map = zykService.downloadRemoteFile(docPath);
PDFUtil.office2PDF(docPath, pdfPath);
zyJxdg.setPdfPath("contract/" + namePath);
}else {
}else if(StringUtils.equals("sftp",uploadType)){
ZykInfo zykInfo = new ZykInfo();
zykInfo.setBizId(zyJxdg.getId());
zykInfo.setBizTable("zy_jxdg");
@ -369,14 +363,11 @@ public class ZyJxdgController extends JeecgController<ZyJxdg, IZyJxdgService> {
String pdfName = map.get("pdfData");
zyJxdg.setFilePath(fileName);
zyJxdg.setPdfPath(pdfName);
}
}
}else{
zyJxdg.setPdfPath(null);
}
if(isAdd){
KcKechengbiao kcKechengbiao = new KcKechengbiao();
kcKechengbiao.setRwbh(zyJxdg.getRwbh());
@ -419,7 +410,7 @@ public class ZyJxdgController extends JeecgController<ZyJxdg, IZyJxdgService> {
String pdfPath = global.getContractDickPath() + namePath;
PDFUtil.office2PDF(docPath, pdfPath);
zyJxdg.setJxrlPdfPath("contract/" + namePath);
}else{
}else if(StringUtils.equals("sftp",uploadType)){
ZykInfo zykInfo = new ZykInfo();
zykInfo.setBizId(zyJxdg.getId());
zykInfo.setBizTable("zy_jxdg");
@ -432,18 +423,10 @@ public class ZyJxdgController extends JeecgController<ZyJxdg, IZyJxdgService> {
zyJxdg.setJxrlFilePath(fileName);
zyJxdg.setJxrlPdfPath(pdfName);
}
}
}else{
zyJxdg.setJxrlPdfPath(null);
}
if(StringUtils.isEmpty(zyJxdg.getId())){
zyJxdgService.save(zyJxdg);
}else{

View File

@ -5,15 +5,28 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.util.AntPathMatcher;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.SymbolConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.DySmsHelper;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.zyk.entity.ZykInfo;
import org.jeecg.modules.zyk.service.IZykService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.HandlerMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
/**
* @Description: 资源库信息
@ -27,6 +40,8 @@ import java.util.Arrays;
@Slf4j
public class ZykController extends JeecgController<ZykInfo, IZykService> {
private final static Logger logger= LoggerFactory.getLogger(ZykController.class);
@Autowired
private IZykService zykService;
@ -89,71 +104,74 @@ public class ZykController extends JeecgController<ZykInfo, IZykService> {
return Result.OK(res);
}
// /**
// * 预览图片&下载文件
// * 请求地址http://localhost:8080/common/static/{user/20190119/e1fe9925bc315c60addea1b98eb1cb1349547719_1547866868179.jpg}
// * @param request
// * @param response
// */
// @GetMapping(value = "/static/**")
// public void view(HttpServletRequest request, HttpServletResponse response) {
// // ISO-8859-1 ==> UTF-8 进行编码转换
// String filePath = extractPathFromPattern(request);
// if(oConvertUtils.isEmpty(filePath) || CommonConstant.STRING_NULL.equals(filePath)){
// return;
// }
// // 其余处理略
// InputStream inputStream = null;
// OutputStream outputStream = null;
// try {
// filePath = filePath.replace("..", "").replace("../","");
// if (filePath.endsWith(SymbolConstant.COMMA)) {
// filePath = filePath.substring(0, filePath.length() - 1);
// }
// Map<String,String> map = zykService.downloadRemoteFile(filePath);
// if(!map.get("code").equals("0")){
// throw new RuntimeException(map.get("msg"));
// }
// String localFilePath = map.get("data");
// File file = new File(localFilePath);
// if(!file.exists()){
// response.setStatus(404);
// throw new RuntimeException("文件["+filePath+"]不存在..");
// }
// // 设置强制下载不打开
// response.setContentType("application/force-download");
// response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes("UTF-8"),"iso-8859-1"));
// inputStream = new BufferedInputStream(new FileInputStream(localFilePath));
// outputStream = response.getOutputStream();
// byte[] buf = new byte[1024];
// int len;
// while ((len = inputStream.read(buf)) > 0) {
// outputStream.write(buf, 0, len);
// }
// response.flushBuffer();
// } catch (IOException e) {
// log.error("预览文件失败" + e.getMessage());
// response.setStatus(404);
// e.printStackTrace();
// } finally {
// if (inputStream != null) {
// try {
// inputStream.close();
// } catch (IOException e) {
// log.error(e.getMessage(), e);
// }
// }
// if (outputStream != null) {
// try {
// outputStream.close();
// } catch (IOException e) {
// log.error(e.getMessage(), e);
// }
// }
// }
// }
/**
* 预览图片&下载文件
* 请求地址http://localhost:8080/common/static/{user/20190119/e1fe9925bc315c60addea1b98eb1cb1349547719_1547866868179.jpg}
* @param request
* @param response
*/
@GetMapping(value = "/static/**")
public void view(HttpServletRequest request, HttpServletResponse response) {
logger.info("zyk/zykInfo/static/SFTP下载 ..."+new Date());
// ISO-8859-1 ==> UTF-8 进行编码转换
String filePath = extractPathFromPattern(request);
if(oConvertUtils.isEmpty(filePath) || CommonConstant.STRING_NULL.equals(filePath)){
return;
}
// 其余处理略
InputStream inputStream = null;
OutputStream outputStream = null;
try {
filePath = filePath.replace("..", "").replace("../","");
if (filePath.endsWith(SymbolConstant.COMMA)) {
filePath = filePath.substring(0, filePath.length() - 1);
}
Map<String,String> map = zykService.downloadRemoteFile(filePath);
if(!map.get("code").equals("0")){
throw new RuntimeException(map.get("msg"));
}
String localFilePath = map.get("data");
File file = new File(localFilePath);
if(!file.exists()){
response.setStatus(404);
throw new RuntimeException("文件["+filePath+"]不存在..");
}
// 设置强制下载不打开
response.setContentType("application/force-download");
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes("UTF-8"),"iso-8859-1"));
inputStream = new BufferedInputStream(new FileInputStream(localFilePath));
outputStream = response.getOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len);
}
response.flushBuffer();
} catch (IOException e) {
log.error("预览文件失败" + e.getMessage());
response.setStatus(404);
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
}
// @GetMapping(value = "/static/**")
// public void view(HttpServletRequest request, HttpServletResponse response) {
// logger.info("SFTP下载 ..."+new Date());
// // ISO-8859-1 ==> UTF-8 进行编码转换
// String filePath = extractPathFromPattern(request);
// if(oConvertUtils.isEmpty(filePath) || CommonConstant.STRING_NULL.equals(filePath)){
@ -171,16 +189,16 @@ public class ZykController extends JeecgController<ZykInfo, IZykService> {
// }
// }
// /**
// * 把指定URL后的字符串全部截断当成参数
// * 这么做是为了防止URL中包含中文或者特殊字符/匹配不了的问题
// * @param request
// * @return
// */
// private static String extractPathFromPattern(final HttpServletRequest request) {
// String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
// String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
// return new AntPathMatcher().extractPathWithinPattern(bestMatchPattern, path);
// }
/**
* 把指定URL后的字符串全部截断当成参数
* 这么做是为了防止URL中包含中文或者特殊字符/匹配不了的问题
* @param request
* @return
*/
private static String extractPathFromPattern(final HttpServletRequest request) {
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
return new AntPathMatcher().extractPathWithinPattern(bestMatchPattern, path);
}
}

View File

@ -58,7 +58,7 @@ public class ZykServiceImpl extends ServiceImpl<ZykMapper, ZykInfo> implements I
// Map<String,String> map = fileHandleUtil.uploadFile(zykInfo);
Map<String,String> map = fileHandleUtil.moveFiles(zykInfo);
if(!map.get("code").equals("0")){
return map.get("msg").toString();
return map.get("msg");
}
if(StringUtils.isNotNull(map.get("data"))){
String fileName = map.get("data");
@ -250,12 +250,12 @@ public class ZykServiceImpl extends ServiceImpl<ZykMapper, ZykInfo> implements I
String namePath = dd + ".pdf";
String pdfPath = global.getContractDickPath() + namePath;
PDFUtil.office2PDF(downloadMap.get("data"),pdfPath);
Map<String,String> uploadMap = uploadFileToSftp("contract/"+namePath);
if(!uploadMap.get("code").equals("0")){
return uploadMap;
}
zykInfo.setPdfName(uploadMap.get("data"));
// PDFUtil.office2PDF(downloadMap.get("data"),pdfPath);
// Map<String,String> uploadMap = uploadFileToSftp("contract/"+namePath);
// if(!uploadMap.get("code").equals("0")){
// return uploadMap;
// }
// zykInfo.setPdfName(uploadMap.get("data"));
Map<String,String> map = fileHandleUtil.moveFiles(zykInfo);
if(!map.get("code").equals("0")){

View File

@ -102,15 +102,15 @@ public class FileHandleUtil {
public Map<String,String> download(String directory, String downloadFile){
Map<String,String> map = SFTPUtil.download(sftpConfig,directory,downloadFile,getDownloadPath("temp"));
// SFTPUtil.disChannel();
// SFTPUtil.disSession();
SFTPUtil.disChannel();
SFTPUtil.disSession();
return map;
}
public void downloadRes(String downloadFile,HttpServletResponse response) throws Exception{
SFTPUtil.writeFileToRes(sftpConfig,downloadFile,response);
SFTPUtil.disChannel();
SFTPUtil.disSession();
// SFTPUtil.disChannel();
// SFTPUtil.disSession();
}
public Map<String,String> moveFile(ZykInfo zykInfo){

View File

@ -1,10 +1,8 @@
package org.jeecg.modules.system.controller;
import cn.hutool.core.io.FastByteArrayOutputStream;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem;
@ -17,13 +15,11 @@ import org.jeecg.common.constant.SymbolConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.*;
import org.jeecg.common.util.filter.FileTypeFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
@ -36,7 +32,6 @@ import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.nio.file.Files;
import java.util.*;
import java.util.stream.Collectors;
@ -54,9 +49,14 @@ import java.util.stream.Collectors;
@RequestMapping("/sys/common")
public class CommonController {
private final static Logger logger= LoggerFactory.getLogger(CommonController.class);
@Value(value = "${jeecg.path.upload}")
private String uploadpath;
@Value(value = "${jeecg.path.webapp}")
private String downloadpath;
/**
* 本地local miniominio 阿里alioss
*/
@ -110,8 +110,8 @@ public class CommonController {
}
/**
* 文件上传统一方法
* @param request
* @param response
* @param bizPath
* @param file
* @return
*/
public String upload(String bizPath, MultipartFile file) throws Exception {
@ -469,7 +469,30 @@ public class CommonController {
}
if(CommonConstant.UPLOAD_TYPE_SFTP.equals(uploadType)) {
try{
SFTPUtil.writeFileToRes(sftpConfig,imgPath,response);
logger.info("/sys/common/static/SFTP下载 ..."+new Date());
// SFTPUtil.writeFileToRes(sftpConfig,imgPath,response);
Map<String,String> map = SFTPUtil.download(sftpConfig,imgPath,getDownloadPath("temp"));
if(!map.get("code").equals("0")){
response.setStatus(404);
throw new RuntimeException(map.get("msg"));
}
String localFilePath = map.get("fileName");
File file = new File(localFilePath);
if(!file.exists()){
response.setStatus(404);
throw new RuntimeException("文件["+imgPath+"]不存在..");
}
// 设置强制下载不打开
response.setContentType("application/force-download");
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes("UTF-8"),"iso-8859-1"));
inputStream = new BufferedInputStream(new FileInputStream(localFilePath));
outputStream = response.getOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len);
}
response.flushBuffer();
}catch (Exception e){
// e.printStackTrace();
}finally {
@ -511,7 +534,7 @@ public class CommonController {
try {
outputStream.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
// log.error(e.getMessage(), e);
}
}
}
@ -599,4 +622,23 @@ public class CommonController {
return new AntPathMatcher().extractPathWithinPattern(bestMatchPattern, path);
}
/**
* 获取文件真实路径
* @param path
* @return
*/
private String getDownloadPath(String path){
String filePath = "";
if(org.jeecg.common.util.text.StringUtils.isEmpty(path)){
return "";
}
int idx = path.indexOf(downloadpath);
if(idx==-1){
filePath = downloadpath + File.separator + path;
}else{
filePath = path;
}
return filePath;
}
}