Upload File From Windows to Unix via SFTP Jsch error with null values directory path

384 Views Asked by At

I'm using JSch to upload file via sftp.

im uploading file from windows to unix server

Upon uploading im using

ChannelSFTP.put(file)

As such:

File file = FiletoPath;
sftp.put(new FileInputStream(file), file.getName());

This However will appear as since getName() will return name of file or absolute path:

\\something\something\something\Documents$\something\something\myfilename.txt

when I try to split it will have error

String[] split = file.getName().split("\\");
sftp.put(new FileInputStream(file), split[split.length-1]);

I am out of idea. How do I solve this issue?

1

There are 1 best solutions below

0
On

I have Soved it.

Basically due to

\\ this back slashes they will have a null value and will throw an exception

I use org.google.guava.Splitter.jar: Download it online org.google.guava.Splitter.jar

Below my snippet code:

//Declaration of JSch Connection session,
//channel and ChannelSftp
//filetype: MultiPartFile

String filename = file.getName();
Iterables<String> itr = Splitter.on("\\").trimResults().omitEmptyStrings().split(filename);
String[] split = Iterables.toArray(itr);

ChannelSftp.put:

//Change Directory to upload the file:
String uploadDirectoryPath = "/usr/name/profile/toStorePath/files/";

try{
     csftp.cd(uploadDirectoryPath);
     csftp.put(file.getInputStream(), split[split.length-1]);
} catch(SftpException | IOException e){
    e.printStackTrace();
}