文件上传成功,但0kb文件正在使用jsch sftp java在远程服务器上传 [英] file uploading successfully, but 0kb file is uploading on remote server using jsch sftp java

查看:1524
本文介绍了文件上传成功,但0kb文件正在使用jsch sftp java在远程服务器上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jsch和Sftp协议在简单的Java应用程序中将文件从本地计算机上传到远程服务器。

我的代码没有错误或没有异常,并且运行成功,但是当我查看远程位置时,文件上传为0kb,没有扩展名,命名为'd'。

我尝试了很多方法,但是我无法弄清楚这个错误。



这是我的代码..



$ p $ String remoteDirectory =D :\\Datastores\\RootStore\\Test\\;
String localDirectory =C:\\pdffiles\\;


String fileToFTP =demo.pdf;
字符串SFTPHOST =hostIPaddress;
int SFTPPORT = 22;
String SFTPUSER =username;
字符串SFTPPASS =密码;


字符串local_filename = fileToFTP;
字符串local_pathname = localDirectory;
字符串remote_filename = fileToFTP;


JSch jsch = null;
Session session = null;
ChannelSftp sftpChannel = null;


尝试
{
jsch = new JSch();
session = jsch.getSession(SFTPUSER,SFTPHOST);
session.setPassword(SFTPPASS);
session.setPort(SFTPPORT);
session.setConfig(StrictHostKeyChecking,no);
session.setConfig( cipher.s2c, AES128-CTR,AES128-CBC,3DES-CTR,3DES-CBC,河豚-CBC,AES192-CBC,AES256-CBC,AES256-CTR);
session.setConfig( cipher.c2s, AES128-CTR,AES128-CBC,3DES-CTR,3DES-CBC,河豚-CBC,AES192-CBC,AES256-CBC,AES256-CTR);
session.connect();

sftpChannel =(ChannelSftp)session.openChannel(sftp);
sftpChannel.connect();

System.out.println(sftpChannel);
System.out.println(sftpChannel.getSession()。isConnected());

FileInputStream fileBodyIns = new FileInputStream(local_pathname + local_filename);

System.out.println(local_pathname + local_filename);
System.out.println(remoteDirectory + remote_filename);

sftpChannel.put(fileBodyIns,remoteDirectory + remote_filename);
fileBodyIns.close();

sftpChannel.exit();
session.disconnect();

System.out.println(File uploaded successfully);
返回File uploaded successfully;

catch(Exception e)
{
e.printStackTrace();
返回e.getMessage();

$ / code>

我的连接成功,行

  System.out.println(sftpChannel.getSession()。isConnected()); 

给出真实

  System.out.println(File uploaded successfully); 


解决方案

  String remoteDirectory =D:\\\\\\\\\\\\\\\\ 

SFTP协议为远程服务器上的文件路径名使用类似unix的命名方案。文件分隔符是/,没有前导/的路径名被认为是相对于当前目录。冒号和反斜杠没有特殊含义。

通过这些规则,您要求远程服务器在您的主目录中创建一个名为D:\ DataStores的文件。 ..与文字冒号和反斜杠。如果您的远程服务器是Cygwin OpenSSH服务器或基于Unix的服务器的其他端口,则SFTP服务器解释文件名的方式与Windows操作系统解释文件名的方式之间可能存在不匹配。



您需要找出正确的语法,以通过此SFTP服务器引用D:驱动器上的路径。如果是Cygwin SFTP服务器,我认为正确的语法是/ cygdrive / d / Datastores / RootStore / Test / ...。如果SFTP服务器是由其他供应商提供的,则可能需要查阅服务器文档。或者,您可以尝试以交互方式登录到SFTP服务器,cd到/目录,并从那里探索。它应该变得明显如何参考D盘上的文件夹。


I am uploading a file from my local computer to remote server in a simple java application using Jsch, and Sftp protocol.

My code is getting no error or no exception and it runs successfully, but when I look at the remote location, the file is uploaded as 0kb with no extension and named as 'D'.

I have tried many ways but I am not able to figure out the mistake.

Here is my code..

        String remoteDirectory = "D:\\Datastores\\RootStore\\Test\\";
        String localDirectory = "C:\\pdffiles\\";


        String fileToFTP = "demo.pdf";
        String SFTPHOST = "hostIPaddress";
        int SFTPPORT = 22;
        String SFTPUSER = "username";
        String SFTPPASS = "password";


        String local_filename = fileToFTP;
        String local_pathname = localDirectory;
        String remote_filename = fileToFTP;


        JSch jsch = null;
        Session session = null;
        ChannelSftp sftpChannel = null;


        try
        {
          jsch = new JSch();
          session = jsch.getSession(SFTPUSER, SFTPHOST);
          session.setPassword(SFTPPASS);
          session.setPort(SFTPPORT);
          session.setConfig("StrictHostKeyChecking", "no");
          session.setConfig("cipher.s2c", "aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc,aes256-ctr");
          session.setConfig("cipher.c2s", "aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc,aes256-ctr");
          session.connect();

          sftpChannel = (ChannelSftp)session.openChannel("sftp");
          sftpChannel.connect();

          System.out.println(sftpChannel);
          System.out.println(sftpChannel.getSession().isConnected());

          FileInputStream fileBodyIns = new FileInputStream(local_pathname + local_filename);

          System.out.println(local_pathname + local_filename);
          System.out.println(remoteDirectory + remote_filename);

          sftpChannel.put(fileBodyIns, remoteDirectory + remote_filename);
          fileBodyIns.close();

          sftpChannel.exit();
          session.disconnect();

          System.out.println("File uploaded successfully");
          return "File uploaded successfully";
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return e.getMessage();
        }

My connection makes successfully, the line

System.out.println(sftpChannel.getSession().isConnected());

gives true

and the following line prints successfully

System.out.println("File uploaded successfully");

解决方案

String remoteDirectory = "D:\\Datastores\\RootStore\\Test\\";

The SFTP protocol uses a unix-like naming scheme for file pathnames on the remote server. The file separator is "/", and a pathname without a leading "/" is considered to be relative to the current directory. Colons and backslashes have no special meaning.

By those rules, you're asking the remote server to create a file in your home directory named "D:\DataStores..." with a literal colon and backslashes. If your remote server is the Cygwin OpenSSH server or some other port of a Unix-based server, there may be a mismatch between the way the SFTP server interprets the filename and the way the Windows OS interprets the filename.

You need to figure out the correct syntax to refer to the paths on the D: drive through this SFTP server. If it's the Cygwin SFTP server, I think the right syntax would be "/cygdrive/d/Datastores/RootStore/Test/...". If the SFTP server is by some other vendor, you may need to consult the server documentation. Alternately, you could try logging into the SFTP server interactively, cd'ing to the "/" directory, and exploring from there. It ought to become obvious how to refer to folders on the D drive.

这篇关于文件上传成功,但0kb文件正在使用jsch sftp java在远程服务器上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆