对于JAVA实现SFTP实例-SSH-2.0-OpenSSH_5.3等报错解决

来源:互联网 发布:怎么开淘宝充值店 编辑:程序博客网 时间:2024/05/16 10:52

因为使用java链接ftp报一下错误

org.apache.commons.net.MalformedServerReplyException: Could not parse response code.Server Reply: SSH-2.0-OpenSSH_5.3

在网上找了一会发现一个兄弟写的帮助了我  http://www.cnblogs.com/chen1987lei/archive/2010/11/26/1888384.html

        非常好的示例 另外需要使用他的代码,还需要导入额外的jar包 .所以本人作为补充 jsch 所用的包 和依赖的包都在下面

<dependency>    <groupId>com.jcraft</groupId>    <artifactId>jsch</artifactId>    <version>0.1.51</version></dependency><!-- jsch 所依赖的jar包  --><dependency>    <groupId>com.jcraft</groupId>    <artifactId>jzlib</artifactId>    <version>1.0.7</version></dependency>

       这是我在maven中加载的包 如果不是在maven中使用ftp上传 可以直接网上搜索 groupId+artifactId+version

    修改过的示例

import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.Properties;import java.util.UUID;import java.util.Vector;import com.jcraft.jsch.Channel;import com.jcraft.jsch.ChannelSftp;import com.jcraft.jsch.JSch;import com.jcraft.jsch.Session;import com.jcraft.jsch.SftpException;public class FtpJSch {private static ChannelSftp sftp = null;//账号private static String user = "ftpuser";//主机ipprivate static String host =  "192.168.78.128";//密码private static String password = "ftpuser";//端口private static int port = 22;//上传地址private static String directory = "/home/www/imgs";//下载目录private static String saveFile = "D:\\VMware\\XuNiJi\\imgs";public static FtpJSch getConnect(){FtpJSch ftp = new FtpJSch();try {JSch jsch = new JSch();//获取sshSession  账号-ip-端口Session sshSession =jsch.getSession(user, host,port);//添加密码sshSession.setPassword(password);Properties sshConfig = new Properties();//严格主机密钥检查sshConfig.put("StrictHostKeyChecking", "no");sshSession.setConfig(sshConfig);//开启sshSession链接sshSession.connect();//获取sftp通道Channel channel = sshSession.openChannel("sftp");//开启channel.connect();sftp = (ChannelSftp) channel;} catch (Exception e) {e.printStackTrace();}return ftp;}/** *  * @param uploadFile 上传文件的路径 * @return 服务器上文件名 */public String upload(String uploadFile) {File file = null;String fileName = null;try {sftp.cd(directory);file = new File(uploadFile);//获取随机文件名fileName  = UUID.randomUUID().toString() + file.getName().substring(file.getName().length()-5);//文件名是 随机数加文件名的后5位sftp.put(new FileInputStream(file), fileName);} catch (Exception e) {e.printStackTrace();}return file == null ? null : fileName;}/** * 下载文件 *  * @param directory *            下载目录 * @param downloadFile *            下载的文件名 * @param saveFile *            存在本地的路径 * @param sftp */public void download(String downloadFileName) {try {sftp.cd(directory);File file = new File(saveFile);sftp.get(downloadFileName, new FileOutputStream(file));} catch (Exception e) {e.printStackTrace();}}/** * 删除文件 *  * @param deleteFile *            要删除的文件名字 * @param sftp */public void delete(String deleteFile) {try {sftp.cd(directory);sftp.rm(deleteFile);} catch (Exception e) {e.printStackTrace();}}/** * 列出目录下的文件 *  * @param directory *            要列出的目录 * @param sftp * @return * @throws SftpException */public Vector listFiles(String directory)throws SftpException {return sftp.ls(directory);}}

原创粉丝点击