From 5611620592c97cd6324fcd257361f803a4d308fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=E5=98=89=E9=98=B3?= Date: Mon, 7 May 2018 12:10:22 +0800 Subject: [PATCH] =?UTF-8?q?FTP=E4=B8=8B=E8=BD=BD=EF=BC=8C=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E6=B5=8B=E8=AF=95=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 17 +- .../spring/controller/DownLoadController.java | 120 ++++- .../spring/controller/UpLoadController.java | 5 + .../java/com/fjy/spring/untils/FtpUtils.java | 434 ++++++++++++++++++ src/main/resources/application.yml | 2 +- src/main/resources/logback-spring.xml | 8 +- .../com/fjy/spring/untils/FtpUtilsTest.java | 83 ++++ 7 files changed, 658 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/fjy/spring/untils/FtpUtils.java create mode 100644 src/test/java/com/fjy/spring/untils/FtpUtilsTest.java diff --git a/pom.xml b/pom.xml index 52cef4d..0220ad8 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.fjy spring - V2.9.9 + V2.10 jar spring @@ -73,7 +73,20 @@ commons-io 2.6 - + + + + commons-net + commons-net + 3.6 + + + + ch.ethz.ganymed + ganymed-ssh2 + build210 + + diff --git a/src/main/java/com/fjy/spring/controller/DownLoadController.java b/src/main/java/com/fjy/spring/controller/DownLoadController.java index 7a4d760..3076c39 100644 --- a/src/main/java/com/fjy/spring/controller/DownLoadController.java +++ b/src/main/java/com/fjy/spring/controller/DownLoadController.java @@ -8,8 +8,12 @@ import com.fjy.spring.exception.UserException; import com.fjy.spring.properties.ServerProperties; import com.fjy.spring.service.FileService; import com.fjy.spring.service.LogService; +import com.fjy.spring.untils.FtpUtils; import com.fjy.spring.untils.LogUtil; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.net.ftp.FTPClient; +import org.apache.commons.net.ftp.FTPFile; +import org.apache.commons.net.ftp.FTPReply; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @@ -84,8 +88,8 @@ public class DownLoadController { } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } - java.io.BufferedInputStream bis = null; - java.io.BufferedOutputStream bos = null; + BufferedInputStream bis = null; + BufferedOutputStream bos = null; TbFile file = new TbFile(); file.setColfileid(fileId); @@ -138,6 +142,72 @@ public class DownLoadController { } + @RequestMapping("/home/download/dodownloadftp") + public String downloadFromFTP(@RequestParam Integer fileId, HttpServletRequest request, HttpServletResponse response) { + + response.setContentType("text/html;charset=utf-8"); + try { + request.setCharacterEncoding("UTF-8"); + } catch (UnsupportedEncodingException e1) { + e1.printStackTrace(); + } + BufferedInputStream bis = null; + BufferedOutputStream bos = null; + + TbFile file = new TbFile(); + file.setColfileid(fileId); + TbFile tbFile = fileService.findFileById(file); + //TbFile tbFile = fileService.findFile(file); + + //System.out.println(tbFile.getColfilepath()); + + String ctxPath = tbFile.getColfilepath(); + String downLoadPath = ctxPath; + //String downLoadPath = ctxPath + tbFile.getColfilename(); + //System.out.println(downLoadPath); + + // FtpUtils.downFtpFile(ctxPath,tbFile.getColfilename(),) + + try { + long fileLength = new File(downLoadPath).length(); + response.setContentType("application/x-msdownload;"); + response.setHeader("Content-disposition", "attachment; filename=" + new String(tbFile.getColfilename().getBytes("utf-8"), "ISO8859-1")); + response.setHeader("Content-Length", String.valueOf(fileLength)); + bis = new BufferedInputStream(new FileInputStream(downLoadPath)); + bos = new BufferedOutputStream(response.getOutputStream()); + byte[] buff = new byte[2048]; + int bytesRead; + while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { + bos.write(buff, 0, bytesRead); + } + //记录下载日志 + addVisitLog("下载文件"+tbFile.getColrealname()+" "+tbFile.getColfilename()); + + + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (bis != null) { + try { + bis.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + if (bos != null) { + try { + bos.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + return null; + + } + /** * 传入课程名和文件夹名称,打包下载目录下所有文件 * @@ -217,8 +287,8 @@ public class DownLoadController { } } //将打包好的文件输出到客户端 - java.io.BufferedInputStream bis = null; - java.io.BufferedOutputStream bos = null; + BufferedInputStream bis = null; + BufferedOutputStream bos = null; try { long fileLength = new File(desPath).length(); response.setContentType("application/x-msdownload;"); @@ -299,6 +369,48 @@ public class DownLoadController { } }*/ + public void downloadConfigFile(HttpServletResponse response,@RequestParam("fileName")String fileName) { + + response.setCharacterEncoding("UTF-8"); + response.setContentType("multipart/form-data"); + + FTPClient ftpClient = new FTPClient(); + try { + int reply; + ftpClient.connect("120.120.120.156",21); + ftpClient.login("user1", "user1"); + reply = ftpClient.getReplyCode(); + if (!FTPReply.isPositiveCompletion(reply)) { + ftpClient.disconnect(); + return; + } + ftpClient.changeWorkingDirectory("/GOS_CAS/BACKUP/cas_config_backup");//转移到FTP服务器目录 + FTPFile[] fs = ftpClient.listFiles(); + for(int i=0;i - - /www/cmsfile/%d/info.%d.log + F:\JAVA Workspace\Temp\log\%d\info.%d.log + @@ -44,8 +44,8 @@ - - /www/cmsfile/%d/error.%d.log + F:\JAVA Workspace\Temp\log\error.%d.log + diff --git a/src/test/java/com/fjy/spring/untils/FtpUtilsTest.java b/src/test/java/com/fjy/spring/untils/FtpUtilsTest.java new file mode 100644 index 0000000..f4b7d23 --- /dev/null +++ b/src/test/java/com/fjy/spring/untils/FtpUtilsTest.java @@ -0,0 +1,83 @@ +package com.fjy.spring.untils; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class FtpUtilsTest { + private static final String destName = "ERP实验1:销售预测与SOP.doc"; + private static final String directory = "/www/cmsftp/"; + + @Test + public void upload() throws Exception{ + String srcFileName = "F:\\JAVA Workspace\\Temp\\upload\\ERP实验1:销售预测与SOP.doc"; + FtpUtils.upload(directory,srcFileName,destName); + list(); + } + + /** + * WIN10下载文件为0kb,请勿使用 + * @throws Exception + */ + @Test + public void download() throws Exception{ + String downloadName = "F:\\JAVA Workspace\\Temp\\ERP实验1:销售预测与SOP.doc"; + + FtpUtils.download(directory,destName,downloadName); + } + + @Test + public void downFtpFile() throws Exception{ + String downloadName = "F:\\JAVA Workspace\\Temp\\"; + boolean res = FtpUtils.downFtpFile(directory,destName,downloadName); + System.out.println(res); + } + + @Test + public void rename() throws Exception{ + String oldFileName = destName; + String newFileName = "新名称.doc"; + FtpUtils.rename(directory,oldFileName, newFileName); + list(); + } + + @Test + public void remove() throws Exception{ + FtpUtils.remove(directory,destName); + list(); + } + + @Test + public void makeDirecotory() throws Exception{ + String newDirectory = "test"; + String newDirectoryZH = "新目录"; + FtpUtils.makeDirecotory(directory,newDirectory); + FtpUtils.makeDirecotory(directory,newDirectoryZH); + list(); + } + + @Test + public void renameDirecotory() throws Exception{ + String oldDirectoryZH = "新目录"; + String newDirectoryZH = "重命名目录"; + FtpUtils.renameDirecotory(directory,oldDirectoryZH,newDirectoryZH); + list(); + } + + @Test + public void removeDirecotory() throws Exception{ + String newDirectory = "test"; + String newDirectoryZH = "重命名目录"; + FtpUtils.removeDirecotory(directory,newDirectory); + FtpUtils.removeDirecotory(directory,newDirectoryZH); + list(); + } + + @Test + public void list() throws Exception{ + String[] lists = FtpUtils.list(directory); + for (String list : lists){ + System.out.println(list); + } + } +} \ No newline at end of file