Java 如果文件大小小于200KB,则不通过ssh上载文件
我正在尝试将一个大约6kb的文件上载到ssh服务器。我尝试在我的终端上使用该命令,甚至测试了其他命令,它们都能工作。更奇怪的是,它可以上传200KB或更大的任何内容。我不知道出了什么问题,但我怀疑这与我缓冲while语句有关。(注意:我试过不同的buf尺寸。)如有任何帮助,将不胜感激。(注意:所有设置都是正确的,所以不要询问我的用户信息…我的库也使用jsch) 完成此操作后,不会显示任何错误。。。该文件根本没有显示在ssh服务器中Java 如果文件大小小于200KB,则不通过ssh上载文件,java,file-upload,ssh,scp,jsch,Java,File Upload,Ssh,Scp,Jsch,我正在尝试将一个大约6kb的文件上载到ssh服务器。我尝试在我的终端上使用该命令,甚至测试了其他命令,它们都能工作。更奇怪的是,它可以上传200KB或更大的任何内容。我不知道出了什么问题,但我怀疑这与我缓冲while语句有关。(注意:我试过不同的buf尺寸。)如有任何帮助,将不胜感激。(注意:所有设置都是正确的,所以不要询问我的用户信息…我的库也使用jsch) 完成此操作后,不会显示任何错误。。。该文件根本没有显示在ssh服务器中 public void testListener(FileUpl
public void testListener(FileUploadEvent event){
InputStream fis = null;
String lfile = event.getFile().getFileName();
if(lfile.lastIndexOf('\\') > 0){
lfile = lfile.substring(lfile.lastIndexOf('\\') + 1);
}
String rfile = "media/"+ lfile;
JSch jsch = new JSch();
Session session = jsch.getSession(user, remotehost);
session.setPassword(password);
session.connect();
boolean ptimestamp = false;
String command="scp "+ (ptimestamp ? " -p " : "")+ "-t " + rfile;
//String command = "cat > foo.txt";
System.out.println(command);
Channel channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream();
channel.connect();
/*
if(ptimestamp){
command="T " +100 + " 0";
command += (" " + 1000 + " 0\n");
out.write(command.getBytes());
out.flush();
}
*/
long filesize = event.getFile().getSize();
command = "C0644 " + filesize + " " + lfile + "\n";
out.write(command.getBytes());
System.out.write(command.getBytes());
out.flush();
fis = event.getFile().getInputstream();
byte[] buf = new byte[1024];
int len=0;
while((len=fis.read(buf, 0, buf.length)) !=-1){
//Change this remove break
out.write(buf, 0, len);
}
fis.close();
fis=null;
buf[0]=0;
out.write(buf, 0, 1);
out.flush();
out.close();
channel.disconnect();
session.disconnect();
}
我找到了解决办法,但这只是一个解决办法
String command="cat >" + rfile +"; chmod 777 " + rfile +"; scp -t " + rfile;
我更改了创建文件的命令,它解决了所有问题。我还删除了,因为文件是用cat创建的。但由于每个人(没有人)的帮助,现在没有问题了
command = "C0644 " + filesize + " " + lfile + "\n";
out.write(command.getBytes());
System.out.write(command.getBytes());
out.flush();
你看到了什么不正确的行为?错误消息?例外?程序挂起?生成的文件大小不正确?死亡的蓝屏?我想我们需要更多的细节。比如没有错误消息或异常或发生任何事情,即使没有抛出异常或任何事情,文件也不会出现在ssh服务器上。。。