sftp 文件上传报 5:Bad message

qq_35081827 2017-09-21 10:13:12
用java实现sftp客户端上传文件到服务器,用户密码,端口,都没错,连接正常!就是在上传文件的时候报 5 Bad message
各位大神帮忙看看是什么问题!


异常消息:




代码:


package com.ericsson.adp.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;



public class SftpUtil {
private static final Logger LOG = LoggerFactory.getLogger(SftpUtil.class);
private String userName; // FTP 登录用户名
private String password; // FTP 登录密码
private String ip; // FTP 服务器地址IP地址
private int port; // FTP 端口
private ChannelSftp channelSftp = null; // FTP 客户端代理
private static Session sshSession = null;

public SftpUtil(){

}

/**
* 实例化FTP
* @param userName 用户名
* @param password 密码
* @param IP
* @param port 端口
*/
public SftpUtil(String userName, String password, String ip, int port) {
this.userName = userName;
this.password = password;
this.ip = ip;
this.port = port;
}

public void connect() throws JSchException{
channelSftp=this.connect(ip, port, userName, password);
}


public void close(){
if(channelSftp != null){
((Channel)channelSftp).disconnect();
channelSftp = null;
}
if (sshSession != null) {
sshSession.disconnect();
sshSession = null;
}
}

/**
* 连接sftp服务器
*
* @param host
* 主机
* @param port
* 端口
* @param username
* 用户名
* @param password
* 密码
* @return
* @throws JSchException
*/
public ChannelSftp connect(String host, int port, String username,
String password) throws JSchException {
ChannelSftp sftp = null;
// try {
JSch jsch = new JSch();
sshSession = jsch.getSession(username, host, port);
LOG.info("Session created.");
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshSession.setTimeout(30000);
// sshSession.setConfig("userauth.gssapi-with-mic", "no");
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
LOG.info("Session connected.");
LOG.info("Opening Channel.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
LOG.info("Opening Channel.");
LOG.info("Connected to " + host + " success.");
// } catch (Exception e) {
LOG.error("Disconnect without connection for 30 seconds");
// LOG.error("30 seconds failed to connect");
// e.printStackTrace();
// LOG.error("Connected to " + host + " fail."+ e.getMessage());
// }
return sftp;
}

/**
* 上传文件
*
* @param directory
* 上传的目录
* @param uploadFile
* 要上传的文件
* @param return true/false
* @throws SftpException
* @throws FileNotFoundException
*/
@SuppressWarnings("static-access")
public boolean upload(String directory, String uploadFile) throws SftpException, FileNotFoundException {
boolean flag = false;
try {
channelSftp.cd(directory);
File file = new File(uploadFile);
channelSftp.put(new FileInputStream(file), file.getName());
flag = true;
}catch(SftpException sException){//这个方法是判断上传的目录是否存在,如果不存在就创建
if(channelSftp.SSH_FX_NO_SUCH_FILE == sException.id){
try {
channelSftp.mkdir(directory);//homt/twera/test1/
upload(directory,uploadFile);
} catch (SftpException e) {
// e.printStackTrace();
LOG.error("upload fails,messs = ",e);
}
}else{
LOG.error("upload fails,messs = ",sException);
}
} catch (Exception e) {
LOG.error("upload fails,messs = ",e);
}
return flag;
}

/**
* 删除文件
*
* @param directory
* 要删除文件所在目录
* @param deleteFile
* 要删除的文件
* @param return true/false
*/
@SuppressWarnings("static-access")
public boolean deleteFile(String directory, String deleteFile
) {
boolean flag = false;
try {
channelSftp.cd(directory);
channelSftp.rm(deleteFile);
flag = true;
// }catch(SftpException sException){//这个方法是判断上传的目录是否存在,
// if(channelSftp.SSH_FX_NO_SUCH_FILE == sException.id){
// LOG.error("DeleteFile exception,messs = "+sException.getMessage());
// flag = true;
// }
//
} catch (Exception e) {
LOG.error("DeleteFile exception,messs = "+e.getMessage());
String mes = e.getMessage();//No such file (说明之前删除过了)
if(mes.equals("No such file")){
flag = true;
}
}
return flag;
}

public boolean deleteFile(SftpUtil ftp,String directory, String deleteFile) throws JSchException{
boolean flag = false;
ftp.connect();
flag = ftp.deleteFile(directory, deleteFile);
ftp.close();

return flag;
}

public boolean uploadFile(SftpUtil ftp,String directory, String uploadFile)throws SftpException, FileNotFoundException, JSchException {
boolean flag = false;
ftp.connect();
flag = ftp.upload(directory, uploadFile);
ftp.close();

return flag;
}



public static void main(String[] args) throws Exception {

// try {
// // 主程序进行上传
//// SftpUtil ftp = new SftpUtil("twera", "twera", "192.168.2.101", 22);
//// ftp.connect();
//// ftp.deleteFile("/home/twera/FTBTest/FTP/Test", "170524190118001.mp4");
//// ftp.close();
//// SftpUtil ftp = new SftpUtil("twera", "twera", "192.168.2.101", 22);
// SftpUtil ftp = new SftpUtil("twera", "twera", "192.168.2.100", 22);
//// SftpUtil ftp = new SftpUtil("twera", "twera", "192.168.2.223", 22);
// ftp.connect();
// ftp.upload("/home/twera/FTBTest/FTP/Test", "D:\\era_V2.0\\twera-business2V\\src\\main\\webapp\\era\\advVideoInf\\170524190118001.mp4");
//// ftp.upload("/home/twera", "D:/era_V2.0/twera-business2V/src/main/webapp/era/advVideoInf/170524190118001.mp4");
// ftp.close();
//
// } catch (Exception e) {
// // TODO: handle exception
// }
// //disconnect();
// System.out.println("*********");

// }


try {
SftpUtil ftp = new SftpUtil("tweras", "tweras", "192.168.2.228", 22);
// SftpUtil ftp = new SftpUtil("admin", "edgeware", "172.17.238.55", 22);
ftp.connect();
// boolean flse =ftp.upload("/media/iptv", "/usr/local/twera/business/apache-tomcat-6.0.26/webapps/twera-business/era/vast/2.mp4");
// ftp.upload("/home/twera", "D:/era_V2.0/twera-business2V/src/main/webapp/era/advVideoInf/170524190118001.mp4333");
ftp.upload("/home/twera/FTBTest/FTP/Test3/", "D:/era_V2.0/twera-business2V/src/main/webapp/era/vast/2.mp4");
ftp.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (SftpException e) {
System.out.println(e.getMessage());
} catch (JSchException e) {
System.out.println(e.getMessage());
}
}
}
...全文
2176 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复 1
我上传附件时也出现了这个问题,不过我上传的附件的名称很长很长,而且名称中含有双引号和空格等字符,我把附件的名称缩短了一半,就可以上传成功。由于时间紧迫没有仔细研究问题所在,怀疑是名称的长度过长导致,或者名称中含有特殊字符等导致,希望对后来人有帮助。

10,606

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 其他
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧