diff --git a/src/main/java/com/yutou/tools/nas/SFTPManager.java b/src/main/java/com/yutou/tools/nas/SFTPManager.java index 03c3011..4158f14 100644 --- a/src/main/java/com/yutou/tools/nas/SFTPManager.java +++ b/src/main/java/com/yutou/tools/nas/SFTPManager.java @@ -1,4 +1,20 @@ package com.yutou.tools.nas; +import com.yutou.tools.utils.Tools; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.multipart.MultipartFile; + +@Controller public class SFTPManager { + + public void upload(@RequestParam("file") MultipartFile file){ + try { + String localFile= Tools.createFile("tmp",file,null); + + } catch (Exception e) { + e.printStackTrace(); + } + + } } diff --git a/src/main/java/com/yutou/tools/utils/SFTPTools.java b/src/main/java/com/yutou/tools/utils/SFTPTools.java index 46052b4..71bc089 100644 --- a/src/main/java/com/yutou/tools/utils/SFTPTools.java +++ b/src/main/java/com/yutou/tools/utils/SFTPTools.java @@ -10,12 +10,23 @@ import java.util.ListIterator; import java.util.Properties; public class SFTPTools { + private static SFTPTools tools; Session sshSession = null; ChannelSftp sftp = null; + public static SFTPTools getInstance(){ + if(tools==null){ + try { + tools=new SFTPTools(); + } catch (Exception e) { + e.printStackTrace(); + } + } + return tools; + } - public SFTPTools() throws Exception { + private SFTPTools() throws Exception { JSch jsch = new JSch(); - sshSession = jsch.getSession("yutou", "113.65.152.143", 23); + sshSession = jsch.getSession("yutou", "113.109.20.210", 23); sshSession.setPassword("34864394"); Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); @@ -24,6 +35,7 @@ public class SFTPTools { Channel channel = sshSession.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; + sftp.cd("/media/yutou/4t/public/"); } /** * 删除 @@ -63,22 +75,26 @@ public class SFTPTools { //https://www.cnblogs.com/nlpvv/articles/11244553.html public void getPath() { try { - sftp.cd("/home"); - ListIterator listIterator=sftp.ls("/media/yutou/4t/public/").listIterator(); - while (listIterator.hasNext()){ - ChannelSftp.LsEntry entry= (ChannelSftp.LsEntry) listIterator.next(); - System.out.println(entry.getFilename()+" ="+entry.getAttrs().getSize()); + for (Object o : sftp.ls("/media/yutou/4t/public/")) { + ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) o; + System.out.println(entry.getFilename() + " size=" + entry.getAttrs().getSize()+" "+entry.getAttrs().isDir()); } - sftp.disconnect(); } catch (SftpException e) { e.printStackTrace(); } } + public void close(){ + sftp.disconnect(); + sftp.exit(); + sftp.quit(); + sshSession.disconnect(); + } public static void main(String[] args) { try { SFTPTools tools=new SFTPTools(); tools.getPath(); + tools.close(); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/com/yutou/tools/utils/Tools.java b/src/main/java/com/yutou/tools/utils/Tools.java index fd7fc99..7423fe6 100644 --- a/src/main/java/com/yutou/tools/utils/Tools.java +++ b/src/main/java/com/yutou/tools/utils/Tools.java @@ -1,17 +1,20 @@ package com.yutou.tools.utils; import com.alibaba.fastjson.JSONArray; +import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.BufferedReader; +import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; +import java.util.Date; import java.util.Random; public class Tools { @@ -180,4 +183,35 @@ public class Tools { } return null; } + /** + * 保存上传的文件 + * + * @param path + * 路径 + * @param file + * 文件 + * @return + * @throws Exception + */ + public static String createFile(String path, MultipartFile file, String newFileName) throws Exception { + String savePath =new File("").getAbsolutePath()+File.separator +"html"+File.separator+ path; + File servicePath = new File(savePath); + if (!servicePath.exists()) { + servicePath.mkdirs(); + } + String fileName=file.getOriginalFilename(); + if(newFileName!=null) { + fileName=newFileName; + } + File saveFile = new File(savePath + "/" + fileName); + if(saveFile.exists()) { + if(!saveFile.delete()) { + saveFile = new File(savePath + "/" + fileName.replace(".", "_"+new Date().getTime()+".")); + } + } + file.transferTo(saveFile); + fileName=URLEncoder.encode(fileName,"UTF-8"); + System.out.println("上传文件保存路径:"+saveFile.getAbsolutePath()); + return path+fileName; + } }