From c6358646f7488fcd2875a390c1e1b7de537e67c8 Mon Sep 17 00:00:00 2001 From: yangjun <1173114630@qq.com> Date: Fri, 29 Nov 2024 13:33:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/controller/CommonController.java | 109 ++++++++++++------ .../org/jeecg/modules/utils/SFTPUtil.java | 74 ++++++++++++ jeecgboot-vue3/.env | 2 +- .../views/bl/lwKhclXz/LwKhclXzListIndex.vue | 31 +++-- .../src/views/bl/lwKhclXz/Xzlwkhcl.vue | 72 +++++++++--- 5 files changed, 227 insertions(+), 61 deletions(-) diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/CommonController.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/CommonController.java index ea63306..2bfa94c 100644 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/CommonController.java +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/CommonController.java @@ -499,6 +499,7 @@ public class CommonController { if (imgPath.endsWith(SymbolConstant.COMMA)) { imgPath = imgPath.substring(0, imgPath.length() - 1); } + //update-begin---author:liusq ---date:20230912 for:检查下载文件类型-------------- SsrfFileTypeFilter.checkDownloadFileType(imgPath); //update-end---author:liusq ---date:20230912 for:检查下载文件类型-------------- @@ -511,6 +512,7 @@ public class CommonController { if(index != -1){ path = imgPath.substring(0,index); } + System.out.println("------------------------path:"+path); //TODO 不确定是否有问题, Map map = SFTPUtil.download(sftpConfig,imgPath,getDownloadPath(path)); System.out.println("msg-->"+map.get("msg")); @@ -518,23 +520,6 @@ public class CommonController { 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 { @@ -544,25 +529,77 @@ public class CommonController { }else { } -// String filePath = uploadpath + File.separator + imgPath; -// filePath.replaceAll(" ",""); -// File file = new File(filePath); -// if(!file.exists()){ -// response.setStatus(404); -// log.error("文件["+imgPath+"]不存在.."); -// //throw new RuntimeException(); -// } -// // 设置强制下载不打开 -// 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(filePath)); -// 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); + } + } + } + } + + return Result.OK(null); + } + + @RequestMapping(value = "/ycxz2", method = {RequestMethod.PUT,RequestMethod.POST}) + public Result ycxz2(@RequestBody JSONObject jsonObject, HttpServletResponse response) { + String downpath = jsonObject.getString("filename"); + String xsxh = jsonObject.getString("xsxh"); + System.out.println("downPath---->"+downpath); + for(String imgPath: downpath.split(",")){ + System.out.println("imgPath---->"+imgPath); + // 其余处理略 + InputStream inputStream = null; + OutputStream outputStream = null; + try { + imgPath = imgPath.replace("..", "").replace("../",""); + if (imgPath.endsWith(SymbolConstant.COMMA)) { + imgPath = imgPath.substring(0, imgPath.length() - 1); + } + +// imgPath = imgPath.substring(0, imgPath.lastIndexOf("."))+xsxh+imgPath.substring(imgPath.lastIndexOf(".")+1,imgPath.length()); + //update-begin---author:liusq ---date:20230912 for:检查下载文件类型-------------- + SsrfFileTypeFilter.checkDownloadFileType(imgPath); + //update-end---author:liusq ---date:20230912 for:检查下载文件类型-------------- +// if(CommonConstant.UPLOAD_TYPE_SFTP.equals(uploadType)) { + if(1==1) { + try{ +// SFTPUtil.writeFileToRes(sftpConfig,imgPath,response); + int index = imgPath.lastIndexOf("/"); + String path = "temp"; + if(index != -1){ + path = imgPath.substring(0,index); + } + System.out.println("------------------------path:"+path); + //TODO 不确定是否有问题, + Map map = SFTPUtil.downloadReplacename(sftpConfig,imgPath,getDownloadPath(path),xsxh); + System.out.println("msg-->"+map.get("msg")); + if(!map.get("code").equals("0")){ + response.setStatus(404); + throw new RuntimeException(map.get("msg")); + } + }catch (Exception e){ +// e.printStackTrace(); + }finally { + SFTPUtil.disChannel(); + SFTPUtil.disSession(); + } + }else { + + } } catch (IOException e) { log.error("预览文件失败" + e.getMessage()); response.setStatus(404); diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/utils/SFTPUtil.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/utils/SFTPUtil.java index 8f9e857..3947818 100644 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/utils/SFTPUtil.java +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/utils/SFTPUtil.java @@ -803,4 +803,78 @@ public class SFTPUtil { } return map; } + + + /** + * 下载文件 + * @param directory 下载目录 + * @param saveFile 存在本地的路径 + */ + public static Map downloadReplacename(SftpConfig sftpConfig, String directory, String saveFile,String xsxh) { + Map map = new HashMap<>(); + map.put("code","0"); + map.put("msg","下载成功"); + directory = sftpConfig.getFullpath()+"/"+ directory; + String[] df = getDirectoryAndFileName(directory); + + File localDirFile = new File(saveFile); + // 判断本地目录是否存在,不存在需要新建各级目录 + if (!localDirFile.exists()) { + localDirFile.mkdirs(); + } + saveFile = saveFile.replace("\\","/"); + String newName = CommonUtils.getFileName(df[1]); + if(!newName.startsWith("/")){ + newName = "/"+newName; + newName = newName.substring(0,newName.lastIndexOf(".")) + xsxh + newName.substring(newName.lastIndexOf("."),newName.length()); + } + String newDownloadFile = saveFile.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; + } + }else{ + sftp.cd(sftpConfig.getFullpath()); + } + OutputStream output = null; + try { + if (logger.isInfoEnabled()) { + logger.info("开始获取远程文件:[{}]---->[{}]", new Object[]{directory, newDownloadFile}); + } +// 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; + } } diff --git a/jeecgboot-vue3/.env b/jeecgboot-vue3/.env index 839ac52..67c8f25 100644 --- a/jeecgboot-vue3/.env +++ b/jeecgboot-vue3/.env @@ -1,5 +1,5 @@ # port -VITE_PORT = 3100 +VITE_PORT = 3101 # 网站标题 VITE_GLOB_APP_TITLE = 教学督导平台 diff --git a/jeecgboot-vue3/src/views/bl/lwKhclXz/LwKhclXzListIndex.vue b/jeecgboot-vue3/src/views/bl/lwKhclXz/LwKhclXzListIndex.vue index 1cf4c4b..8825842 100644 --- a/jeecgboot-vue3/src/views/bl/lwKhclXz/LwKhclXzListIndex.vue +++ b/jeecgboot-vue3/src/views/bl/lwKhclXz/LwKhclXzListIndex.vue @@ -432,6 +432,7 @@ async function handleChakan(record) { lwinfo.value = record; console.log("lwinfo--->",lwinfo) sfxk.value = 2; + const xsxh = record.xsxh; var filename = ""; @@ -450,20 +451,27 @@ async function handleChakan(record) { if(filename){ console.log("filename--->",filename) - defHttp.post({ url: '/sys/common/ycxz',params:{filename} }).then((res) => {}); + await defHttp.post({ url: '/sys/common/ycxz2',params:{filename,xsxh} }).then((res) => {}); } + ktbgUrl.value = ''; if (record.ktbg) { - var file1 = getFileAccessHttpUrl(record.ktbg.replaceAll(" ","")); - console.log("file1---->",file1); - console.log("file11111---->",encodeURIComponent(encryptByBase64(file1))); + var file = record.ktbg; + file = file.substring(0,file.lastIndexOf(".")) + xsxh + file.substring(file.lastIndexOf("."),file.length); + console.log("🚀 ~ handleChakan ~ record.ktbg:", file) + var file1 = getFileAccessHttpUrl(file.replaceAll(" ","")); ktbgUrl.value = 'https://jxdd.nenu.edu.cn/onlinePreview/onlinePreview?url=' + encodeURIComponent(encryptByBase64(file1)); + console.log("🚀 ~ handleChakan ~ ktbgUrl.value:", ktbgUrl.value) } else { ktbgUrl.value = ''; } + ktbgshyjUrl.value = ''; if (record.ktbgshyj) { - var file1 = getFileAccessHttpUrl(record.ktbgshyj.replaceAll(" ","")); + var file = record.ktbgshyj; + file = file.substring(0,file.lastIndexOf(".")) + xsxh + file.substring(file.lastIndexOf("."),file.length); + console.log("🚀 ~ handleChakan ~ record.ktbgshyj:", file) + var file1 = getFileAccessHttpUrl(file.replaceAll(" ","")); ktbgshyjUrl.value = 'https://jxdd.nenu.edu.cn/onlinePreview/onlinePreview?url=' + encodeURIComponent(encryptByBase64(file1)); } else { ktbgshyjUrl.value = ''; @@ -476,15 +484,22 @@ async function handleChakan(record) { // zqjcUrl.value = ''; // } + lwzgUrl.value = ''; if (record.lwzg) { - var file1 = getFileAccessHttpUrl(record.lwzg.replaceAll(" ","")); + var file = record.lwzg; + file = file.substring(0,file.lastIndexOf(".")) + xsxh + file.substring(file.lastIndexOf("."),file.length); + console.log("🚀 ~ handleChakan ~ record.lwzg:", file) + var file1 = getFileAccessHttpUrl(file.replaceAll(" ","")); lwzgUrl.value = 'https://jxdd.nenu.edu.cn/onlinePreview/onlinePreview?url=' + encodeURIComponent(encryptByBase64(file1)); } else { lwzgUrl.value = ''; } - + zdjldUrl.value = ''; if (record.zdjld) { - var file1 = getFileAccessHttpUrl(record.zdjld.replaceAll(" ","")); + var file = record.zdjld; + file = file.substring(0,file.lastIndexOf(".")) + xsxh + file.substring(file.lastIndexOf("."),file.length); + console.log("🚀 ~ handleChakan ~ record.zdjld:", file) + var file1 = getFileAccessHttpUrl(file.replaceAll(" ","")); zdjldUrl.value = 'https://jxdd.nenu.edu.cn/onlinePreview/onlinePreview?url=' + encodeURIComponent(encryptByBase64(file1)); } else { zdjldUrl.value = ''; diff --git a/jeecgboot-vue3/src/views/bl/lwKhclXz/Xzlwkhcl.vue b/jeecgboot-vue3/src/views/bl/lwKhclXz/Xzlwkhcl.vue index 447b2f6..739cce6 100644 --- a/jeecgboot-vue3/src/views/bl/lwKhclXz/Xzlwkhcl.vue +++ b/jeecgboot-vue3/src/views/bl/lwKhclXz/Xzlwkhcl.vue @@ -427,49 +427,89 @@ function handleYulan(record) { } //查看论文材料 async function handleChakan(record) { + console.log("record--->",record) lwinfo.value = record; + console.log("lwinfo--->",lwinfo) sfxk.value = 2; + const xsxh = record.xsxh; - if (lwinfo.value.ktbg) { - var file1 = getFileAccessHttpUrl(record.ktbg); + var filename = ""; + + if (record.ktbg) { + filename = filename+record.ktbg+","; + } + if (record.ktbgshyj) { + filename = filename+record.ktbgshyj+","; + } + if (record.lwzg) { + filename = filename+record.lwzg+","; + } + if (record.zdjld) { + filename = filename+record.zdjld+","; + } + + if(filename){ + console.log("filename--->",filename) + await defHttp.post({ url: '/sys/common/ycxz2',params:{filename,xsxh} }).then((res) => {}); + } + + ktbgUrl.value = ''; + if (record.ktbg) { + var file = record.ktbg; + file = file.substring(0,file.lastIndexOf(".")) + xsxh + file.substring(file.lastIndexOf("."),file.length); + console.log("🚀 ~ handleChakan ~ record.ktbg:", file) + var file1 = getFileAccessHttpUrl(file.replaceAll(" ","")); ktbgUrl.value = 'https://jxdd.nenu.edu.cn/onlinePreview/onlinePreview?url=' + encodeURIComponent(encryptByBase64(file1)); + console.log("🚀 ~ handleChakan ~ ktbgUrl.value:", ktbgUrl.value) } else { ktbgUrl.value = ''; } - if (lwinfo.value.ktbgshyj) { - var file1 = getFileAccessHttpUrl(record.ktbgshyj); + ktbgshyjUrl.value = ''; + if (record.ktbgshyj) { + var file = record.ktbgshyj; + file = file.substring(0,file.lastIndexOf(".")) + xsxh + file.substring(file.lastIndexOf("."),file.length); + console.log("🚀 ~ handleChakan ~ record.ktbgshyj:", file) + var file1 = getFileAccessHttpUrl(file.replaceAll(" ","")); ktbgshyjUrl.value = 'https://jxdd.nenu.edu.cn/onlinePreview/onlinePreview?url=' + encodeURIComponent(encryptByBase64(file1)); } else { ktbgshyjUrl.value = ''; } - if (lwinfo.value.zqjc) { - var file1 = getFileAccessHttpUrl(record.zqjc); - zqjcUrl.value = 'https://jxdd.nenu.edu.cn/onlinePreview/onlinePreview?url=' + encodeURIComponent(encryptByBase64(file1)); - } else { - zqjcUrl.value = ''; - } + // if (lwinfo.value.zqjc) { + // var file1 = getFileAccessHttpUrl(record.zqjc.replace(" ","")); + // zqjcUrl.value = 'https://jxdd.nenu.edu.cn/onlinePreview/onlinePreview?url=' + encodeURIComponent(encryptByBase64(file1)); + // } else { + // zqjcUrl.value = ''; + // } - if (lwinfo.value.lwzg) { - var file1 = getFileAccessHttpUrl(record.lwzg); + lwzgUrl.value = ''; + if (record.lwzg) { + var file = record.lwzg; + file = file.substring(0,file.lastIndexOf(".")) + xsxh + file.substring(file.lastIndexOf("."),file.length); + console.log("🚀 ~ handleChakan ~ record.lwzg:", file) + var file1 = getFileAccessHttpUrl(file.replaceAll(" ","")); lwzgUrl.value = 'https://jxdd.nenu.edu.cn/onlinePreview/onlinePreview?url=' + encodeURIComponent(encryptByBase64(file1)); } else { lwzgUrl.value = ''; } - - if (lwinfo.value.zdjld) { - var file1 = getFileAccessHttpUrl(record.zdjld); + zdjldUrl.value = ''; + if (record.zdjld) { + var file = record.zdjld; + file = file.substring(0,file.lastIndexOf(".")) + xsxh + file.substring(file.lastIndexOf("."),file.length); + console.log("🚀 ~ handleChakan ~ record.zdjld:", file) + var file1 = getFileAccessHttpUrl(file.replaceAll(" ","")); zdjldUrl.value = 'https://jxdd.nenu.edu.cn/onlinePreview/onlinePreview?url=' + encodeURIComponent(encryptByBase64(file1)); } else { zdjldUrl.value = ''; } - if (lwinfo.value.ccjg) { + if (record.ccjg) { jcbgUrl.value = record.ccjg; } else { jcbgUrl.value = ''; } + } const getViewFileDomain = () => defHttp.get({ url: '/sys/comment/getFileViewDomain' });