From 5702d3f521013c6a335ac95c7dee6cc5525807cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E7=A3=8A?= <45566618@qq.com> Date: Wed, 29 May 2024 16:18:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=A6=E7=94=9F=E4=BD=9C=E4=B8=9A=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=A2=9E=E5=8A=A0SFTP=E4=B8=8A=E4=BC=A0=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ZyInfoStudentController.java | 4 +- .../service/IZyInfoStudentService.java | 4 +- .../impl/ZyInfoStudentServiceImpl.java | 125 +++++++++++++++--- 3 files changed, 112 insertions(+), 21 deletions(-) diff --git a/jeecg-module-main/src/main/java/org/jeecg/modules/kc/zyInfoStudent/controller/ZyInfoStudentController.java b/jeecg-module-main/src/main/java/org/jeecg/modules/kc/zyInfoStudent/controller/ZyInfoStudentController.java index c02998d4..f9f3a283 100644 --- a/jeecg-module-main/src/main/java/org/jeecg/modules/kc/zyInfoStudent/controller/ZyInfoStudentController.java +++ b/jeecg-module-main/src/main/java/org/jeecg/modules/kc/zyInfoStudent/controller/ZyInfoStudentController.java @@ -275,7 +275,7 @@ public class ZyInfoStudentController extends JeecgController zyscStu(@RequestBody ZyInfoStudent zyInfoStudent) { + public Result zyscStu(@RequestBody ZyInfoStudent zyInfoStudent, HttpServletResponse response) { ZyInfoStudent stuPar = zyInfoStudentService.getById(zyInfoStudent.getId()); ZyInfo zyInfo = zyInfoService.getById(stuPar.getMainId()); @@ -310,7 +310,7 @@ public class ZyInfoStudentController extends JeecgController { void deleteMainId(String id); - public boolean updateById(ZyInfoStudent zyInfoStudent); + public boolean updateById(ZyInfoStudent zyInfoStudent, HttpServletResponse response); void editFabu(ZyInfoStudent zyInfoStudent); diff --git a/jeecg-module-main/src/main/java/org/jeecg/modules/kc/zyInfoStudent/service/impl/ZyInfoStudentServiceImpl.java b/jeecg-module-main/src/main/java/org/jeecg/modules/kc/zyInfoStudent/service/impl/ZyInfoStudentServiceImpl.java index 06102047..d7228c77 100644 --- a/jeecg-module-main/src/main/java/org/jeecg/modules/kc/zyInfoStudent/service/impl/ZyInfoStudentServiceImpl.java +++ b/jeecg-module-main/src/main/java/org/jeecg/modules/kc/zyInfoStudent/service/impl/ZyInfoStudentServiceImpl.java @@ -8,10 +8,10 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; +import org.jeecg.common.constant.CommonConstant; import org.jeecg.common.system.vo.LoginUser; -import org.jeecg.modules.kc.blZycc.entity.BlZycc; -import org.jeecg.modules.kc.kcSysConfig.entity.KcSysConfig; -import org.jeecg.modules.kc.kcSysConfig.service.IKcSysConfigService; +import org.jeecg.common.util.SFTPUtil; +import org.jeecg.common.util.SftpConfig; import org.jeecg.modules.kc.ktgl.entity.KcKechengbiao; import org.jeecg.modules.kc.ktgl.service.IKcKechengbiaoService; import org.jeecg.modules.kc.zyCcjg.entity.ZyCcjg; @@ -28,6 +28,8 @@ import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import javax.activation.MimetypesFileTypeMap; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; @@ -53,6 +55,9 @@ public class ZyInfoStudentServiceImpl extends ServiceImpl textMap = new HashMap(); //可以设置多个input的name,value String sign = getSign(); @@ -214,10 +225,11 @@ public class ZyInfoStudentServiceImpl extends ServiceImpl textMap = new HashMap(); String titlePar =fileName.substring(fileName.lastIndexOf("/")+1,fileName.length()); String title = titlePar.split("_")[0]; @@ -259,10 +271,11 @@ public class ZyInfoStudentServiceImpl extends ServiceImpl textMap = new HashMap(); String titlePar =fileName.substring(fileName.lastIndexOf("/")+1,fileName.length()); String title = titlePar.split("_")[0]; @@ -327,11 +340,12 @@ public class ZyInfoStudentServiceImpl extends ServiceImpl textMap = new HashMap(); //可以设置多个input的name,value String sign = getSign(); @@ -373,8 +387,6 @@ public class ZyInfoStudentServiceImpl extends ServiceImpl map = SFTPUtil.download(sftpConfig,imgPath,getDownloadPath(path)); + 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(); + fileName = localFilePath; + }catch (Exception e){ +// e.printStackTrace(); + }finally { + SFTPUtil.disChannel(); + SFTPUtil.disSession(); + 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 fileName; + } + /** + * 获取文件真实路径 + * @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; + } //生成sign