update
This commit is contained in:
parent
f7436f2830
commit
490fe0e3fd
@ -1,4 +1,20 @@
|
|||||||
package com.yutou.tools.nas;
|
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 class SFTPManager {
|
||||||
|
|
||||||
|
public void upload(@RequestParam("file") MultipartFile file){
|
||||||
|
try {
|
||||||
|
String localFile= Tools.createFile("tmp",file,null);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,12 +10,23 @@ import java.util.ListIterator;
|
|||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
public class SFTPTools {
|
public class SFTPTools {
|
||||||
|
private static SFTPTools tools;
|
||||||
Session sshSession = null;
|
Session sshSession = null;
|
||||||
ChannelSftp sftp = 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();
|
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");
|
sshSession.setPassword("34864394");
|
||||||
Properties sshConfig = new Properties();
|
Properties sshConfig = new Properties();
|
||||||
sshConfig.put("StrictHostKeyChecking", "no");
|
sshConfig.put("StrictHostKeyChecking", "no");
|
||||||
@ -24,6 +35,7 @@ public class SFTPTools {
|
|||||||
Channel channel = sshSession.openChannel("sftp");
|
Channel channel = sshSession.openChannel("sftp");
|
||||||
channel.connect();
|
channel.connect();
|
||||||
sftp = (ChannelSftp) channel;
|
sftp = (ChannelSftp) channel;
|
||||||
|
sftp.cd("/media/yutou/4t/public/");
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 删除
|
* 删除
|
||||||
@ -63,22 +75,26 @@ public class SFTPTools {
|
|||||||
//https://www.cnblogs.com/nlpvv/articles/11244553.html
|
//https://www.cnblogs.com/nlpvv/articles/11244553.html
|
||||||
public void getPath() {
|
public void getPath() {
|
||||||
try {
|
try {
|
||||||
sftp.cd("/home");
|
for (Object o : sftp.ls("/media/yutou/4t/public/")) {
|
||||||
ListIterator listIterator=sftp.ls("/media/yutou/4t/public/").listIterator();
|
ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) o;
|
||||||
while (listIterator.hasNext()){
|
System.out.println(entry.getFilename() + " size=" + entry.getAttrs().getSize()+" "+entry.getAttrs().isDir());
|
||||||
ChannelSftp.LsEntry entry= (ChannelSftp.LsEntry) listIterator.next();
|
|
||||||
System.out.println(entry.getFilename()+" ="+entry.getAttrs().getSize());
|
|
||||||
}
|
}
|
||||||
sftp.disconnect();
|
|
||||||
} catch (SftpException e) {
|
} catch (SftpException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public void close(){
|
||||||
|
sftp.disconnect();
|
||||||
|
sftp.exit();
|
||||||
|
sftp.quit();
|
||||||
|
sshSession.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try {
|
try {
|
||||||
SFTPTools tools=new SFTPTools();
|
SFTPTools tools=new SFTPTools();
|
||||||
tools.getPath();
|
tools.getPath();
|
||||||
|
tools.close();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,20 @@
|
|||||||
package com.yutou.tools.utils;
|
package com.yutou.tools.utils;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONArray;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.servlet.http.Cookie;
|
import javax.servlet.http.Cookie;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class Tools {
|
public class Tools {
|
||||||
@ -180,4 +183,35 @@ public class Tools {
|
|||||||
}
|
}
|
||||||
return null;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user