I am trying to download some files vis sftp using a java application which uses the sshj library. I am using this tutorial
My code is as follows:
try {
sftpClient = client.newSFTPClient();
remoteFileList = sftpClient.ls("gtdt/xml");
logger.debug("trying to explicitly donwload file");
sftpClient.get("remotepath/xml/somefile.xml", new FileSystemFile(localDownloadDir));
} catch (IOException e) {
logger.error("Error getting list of remote files on sftp server", e);
fail("error getting list or remote ftp files");
}
Later I loop over the files that were returned:
assertTrue(remoteFileList.size() > 0);
for (RemoteResourceInfo info : remoteFileList) {
logger.info(info.toString());
}
but when I try to download any files (have tried one and many) like so
// get first file in list and download
RemoteResourceInfo info = remoteFileList.get(0);
try {
String localCopyPath = localDownloadDir.getAbsolutePath() + "/" + info.getName();
FileSystemFile localCopyFile = new FileSystemFile(localDownloadDir.getAbsolutePath() + "/" + info.getName());
logger.debug("attempting to copy remote file " + info.getPath() + " to : " + localCopyPath);
sftpClient.get("remotepath/xml/somefile.xml", localCopyFile);
} catch (IOException e) {
logger.error("unable to copy file to local dir ", e);
fail("unable to copy " + info.getName() + " to local dir " + localDownloadDir.getAbsolutePath());
}
I get an error like this:
[main] WARN net.schmizz.sshj.xfer.FileSystemFile - Could not set permissions for c:\mypath\somefile.xml to 1a0
any idea how to fix this? The actual code that is being executed there starts at line 142 here. It wasn't clear to me if this is and issue on my local machine or something I need to configure in sshj.
thanks!
Related
i need an approach with Java to make the Programm know when a File in Linux has been modified. I have tried Files.getLastModifiedTime(), BasicFileAttributes and lastModified. Unfortunately this has not work.
The Scenarios
Scenario A:
an empty Textfile beeing copied from A to B Folder.
then the same empty Textfile in A beeing edited and written something like "Hello World!!!" in an Editor and then copy this file again in Folder B.
Scenario B:
an empty Textfile beeing copied from A to B Folder.
then the same empty Textfile beeing copied again in Folder B.
*In Scenario A my Java Programm identify a modified File in Folder B. I use a timestamp in (dd.MM.yyyy HH:mm:SS) to give it some Unique Identifikation when i relate this Timestamp with its filename as Strings.
*But in Scenario B my Java Programm identify this file also als modified, even though there hasnt been any changes to this Textfile, its the same and its empty in Folder B. I dont know which attribute to use in Java in order to make the programm know that this File has not been modified.(This only happened to me in Linux)
I use openSuse Leap for the Linux environment and Java openJdk 1.8 for my Programm.
As this Programm starts ill copy the 2 existing files in "/tmp/testfolder" to
"/tmp/testfolder/AFolder" with a Linux cp command.
It seems to me that openSUSE Leap doesnt save the Last modified data on its files,
because i get a new and same timestamp for values in Folder A and Folder B.
This doesnt happen in Linux Mint. Unfortunately my company uses openSuse Leap :(
My code:
package com.company;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import org.apache.commons.io.FileUtils;
public class App
{
public static void main( String[] args )
{
File textFile1 = new File("/tmp/testfolder/TextFile1.txt");
File textFile2 = new File("/tmp/testfolder/TextFile2.txt");
File textFile1CopyInA = new File("/tmp/testfolder/AFolder/TextFile1.txt");
File textFile2CopyInA = new File("/tmp/testfolder/AFolder/TextFile2.txt");
File textFile1CopyInB = new File("/tmp/testfolder/BFolder/TextFile1.txt");
File textFile2CopyInB = new File("/tmp/testfolder/BFolder/TextFile2.txt");
File bfolder = new File("/tmp/testfolder/BFolder/");
try {
System.out.println("Last Modified of File at testFolder" + textFile1.getAbsolutePath() + " :: " + Files.getLastModifiedTime(textFile1.toPath(), LinkOption.NOFOLLOW_LINKS));
System.out.println("Last Modified of File at testFolder" + textFile2.getAbsolutePath() + " :: " + Files.getLastModifiedTime(textFile2.toPath(), LinkOption.NOFOLLOW_LINKS));
BufferedWriter writer = new BufferedWriter(new FileWriter(textFile1));
String data = "Hello World!!!";
writer.write(data);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
while(true) {
if((textFile1CopyInA.exists())&&(textFile2CopyInA.exists())){
try {
System.out.println("Last Modified of File in A" + textFile1CopyInA.getAbsolutePath() + " :: " + Files.getLastModifiedTime(textFile1CopyInA.toPath(), LinkOption.NOFOLLOW_LINKS));
System.out.println("Last Modified of File in A" + textFile2CopyInA.getAbsolutePath() + " :: " + Files.getLastModifiedTime(textFile2CopyInA.toPath(), LinkOption.NOFOLLOW_LINKS));
FileUtils.copyFileToDirectory(textFile1CopyInA, bfolder);
FileUtils.copyFileToDirectory(textFile2CopyInA, bfolder);
} catch (IOException e) {
e.printStackTrace();
}
}
if((textFile1CopyInB.exists())&&(textFile2CopyInB.exists())){
try {
System.out.println("Last Modified of File in B " + textFile1CopyInB.getAbsolutePath() + " :: " + Files.getLastModifiedTime(textFile1CopyInB.toPath(), LinkOption.NOFOLLOW_LINKS));
System.out.println("Last Modified of File in B" + textFile2CopyInB.getAbsolutePath() + " :: " + Files.getLastModifiedTime(textFile2CopyInB.toPath(), LinkOption.NOFOLLOW_LINKS));
} catch (IOException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Thanks a lot in Advance!!
I am building a desktop app using javafx, I am downloading a file around 500 MB using ftp. I am choosing the download location using DirectoryChooser but after choosing the directory my application hang and doesn't response.
Though the file is downloaded.
here is my code:-
try {
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
success = ftpClient.changeWorkingDirectory(PATH + preset + "/" + file_to_download + offset);
System.out.println("Download Path:-" + PATH + preset + "/" + file_to_download + offset);
if (!success) {
System.out.println("Could not changed the directory to RIBS");
return;
} else {
System.out.println("Directory changed to RIBS");
}
FTPFile[] files = ftpClient.listFiles();
for (FTPFile file : files) {
if (file.getName().contains(".zip")) {
dfile = file.getName();
}
}
DirectoryChooser dirChooser = new DirectoryChooser();
File chosenDir = dirChooser.showDialog(tableView.getScene().getWindow());
System.out.println(chosenDir.getAbsolutePath());
OutputStream output;
output = new FileOutputStream(chosenDir.getAbsolutePath() + "/" + dfile);
int timeOut = 500;
ftpClient.setConnectTimeout(timeOut);
if (ftpClient.retrieveFile(dfile, output) == true) {
downloadButton.setDisable(true);
}
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
How can I improve this??
You are performing your download on the application thread which blocks the UI. Have a look at the documentation about JavaFX concurrency.
https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/concurrency.htm
My requirement is to copy the file from local machine(Windows) to unix serevr. I have the code which was working fine with some Mbs data. i am using jsch lib to connect and to transfer.
But now i have to transfer the files of 1GB, 2Gb or may be 5GB.
When i am using the same approach.
Its getting failed. Its stucking at
channelsftpObj.put(from,to);
and then exception is "faliure". Nothing else coming in exception.
May i know the reason or how can i transfer these files?
private boolean executeCommand (String localDir, String remoteDir, String fileList, String actionFlg) {
boolean boolError = false;
String localPath, destinationPath;
ChannelSftp channelSFTPObj = (ChannelSftp) channelObj;
for (int i = 0; i < filelistArr.length; i++ ) {
localPath = localDir + "/" + filelistArr[i];
destinationPath = remoteDir + "/" + filelistArr[i];
try {
if (actionFlg.toLowerCase() == "upload") {
channelSFTPObj.put (localPath, destinationPath);
System.out.println ("Uploaded " + filelistArr[i] + " to " + remoteDir);
}
}
catch (SftpException e) {
System.out.println(e);
boolError = true;
}
}
channelSFTPObj.exit();
return boolError;
}
localDir is my path of my local system, remoteDir is the server path.
Successfully connected to the server.
I am trying to create dmp file of a table A_LOCKER using expdp cmd and then sftp it back to local machine on which my java code is deployed.The server is an oracle weblogic server.
The code is working fine for first time but from second time it is giving error as:Permission denied
The dmp file is created in a data pump directory Extract which is create by me.
Now i try to sftp the dmp file and for that i need to provide sudo permission for the file
so i tried following in my java code:
1)To create session:
StringBuffer cmd = new StringBuffer();
FileOutputStream fileOutputStream = null;
SshClient ssh = new SshClient();
SessionChannelClient session = null;
SftpClient sftp = null;
HostKeyVerification host = new IgnoreHostKeyVerification();
// Try to connect to the machine that includes the shared folder
// Throw an exception when a connection is failed.
try {
ssh.connect(machine, host);
} catch (IOException e) {
logger.error("Cannot connect to dbserver in machine "
+ machine,e);
throw new EPCShareFileException(
"Cannot connect to dbserver location in machine "
+ machine, e);
}
PasswordAuthenticationClient auth = new PasswordAuthenticationClient();
auth.setUsername(user);
auth.setPassword(password);
// Authenticate user name and password for connection
// Throw an exception if authentication is failed
try {
ssh.authenticate(auth);
} catch (IOException e) {
logger.error("Cannot authenticate user "
+ user + " " + " password " + password);
ssh.disconnect();
throw new EPCShareFileException("Cannot authenticate user "
+ user + " " + " password " + password, e);
}
2)Execute chmod command using pseudo permission
cmd.append("/usr/local/bin/sudo /bin/chmod -R 777 ")
.append(location+dbDumpFileName);
try{
session = ssh.openSessionChannel();
session.executeCommand(cmd.toString());
} catch (IOException e){
logger.error("Cannot execute chmod cmd");
}
finally{
try {
session.close();
} catch (IOException e) {
// TODO Auto-generated catch block
logger.info("Cannot close the session:"+e.getMessage());
}
}
3)Then i am trying to sftp the dmp file to local server
try {
sftp = ssh.openSftpClient();
} catch (IOException e) {
logger.error("Cannot open connection to database server");
ssh.disconnect();
throw new EPCShareFileException(
"Cannot open connection to database server");
}
try{
fileOutputStream = new FileOutputStream(dbDumpFileName);
sftp.get(location+dbDumpFileName, fileOutputStream);
}catch (IOException e) {
**throw new EPCShareFileException("Cannot retrive file "
+"Error:"+e.getMessage());**
}finally{
try {
fileOutputStream.close();
sftp.quit();
fileOutputStream = null;
sftp = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ssh.disconnect();
ssh = null;
}
For first time when i am deploying my code as ear file on oracle weblogic server it is working file and dmp file is created and sftped to desired location as well
But from second time i am getting error:
Error:Permission denied
Please Help..
First of all, don't ever use chmod -R 777 - that's like publishing your data for anyone with access to the machine, no questions asked. If you used this to debug the issue, then okay, but don't forget to change it back (or get rid of it; you shouldn't need it at all).
To debug "Permission denied" errors, you need to have access to the machine. On the machine, you need to login as the user that got the error (don't try as root or another user).
As this user, try to access every folder in the path that you couldn't access. So if it failed for /srv/www/foo/bar/x/y/z/file.txt, then you need to
ls /srv
ls /srv/www/
ls /srv/www/foo/
ls /srv/www/foo/bar/
ls /srv/www/foo/bar/x/
ls /srv/www/foo/bar/x/y/
ls /srv/www/foo/bar/x/y/z/
ls /srv/www/foo/bar/x/y/z/file.txt
Do it in this order. Find the first command that failed and then check why this user doesn't have permissions for the folder in question.
Well, my code is as below. I'd like to know why there's always an exception. The mp3 file is in the same directory as the test.java file. What am I doing wrong? Also, how do I read mp3 files from say my Music Library : path - Libraries\Music
import java.io.IOException;
import com.mpatric.mp3agic.ID3v1;
import com.mpatric.mp3agic.InvalidDataException;
import com.mpatric.mp3agic.Mp3File;
import com.mpatric.mp3agic.UnsupportedTagException;
public class test
{
public static void main(String args[])
{
Mp3File mp3file = null;
try {
mp3file = new Mp3File("dom.mp3");
} catch (UnsupportedTagException | InvalidDataException | IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("File not found.");
}
if (mp3file.hasId3v1Tag()) {
ID3v1 id3v1Tag = mp3file.getId3v1Tag();
System.out.println("Track: " + id3v1Tag.getTrack());
System.out.println("Artist: " + id3v1Tag.getArtist());
System.out.println("Title: " + id3v1Tag.getTitle());
System.out.println("Album: " + id3v1Tag.getAlbum());
System.out.println("Year: " + id3v1Tag.getYear());
System.out.println("Genre: " + id3v1Tag.getGenre() + " (" + id3v1Tag.getGenreDescription() + ")");
System.out.println("Comment: " + id3v1Tag.getComment());
}
}
}
Exception
java.io.FileNotFoundException: File not found dom.mp3
at com.mpatric.mp3agic.FileWrapper.init(FileWrapper.java:26)
at com.mpatric.mp3agic.FileWrapper.<init>(FileWrapper.java:19)
at com.mpatric.mp3agic.Mp3File.<init>(Mp3File.java:53)
at com.mpatric.mp3agic.Mp3File.<init>(Mp3File.java:41)
at test.main(test.java:13)
File not found.
Exception in thread "main" java.lang.NullPointerException at test.main(test.java:19)
The mpatric package is 3rd party. I'm guessing that works fine.
What do you mean by "same directory as you run your java process from"? Can you give me a for instance, please?
Printing this:
System.out.println("File not found.");
is misleading, given this:
catch (UnsupportedTagException | InvalidDataException | IOException e)
You need to dump the exception (e.printStackTrace() to determine the real issue).
Your .mp3 file is in the same directory as your .java file. But that's not relevant. Is it in the same directory as where you run your java process from ? That's where it needs to be.
e.g.
$ cd /mydir
$ java com.whatever.TestJava
In the above your .mp3 file needs to be in the /mydir directory
As mentioned by #BrianAgnew, you should dump your Exception.
UPDATE
Try this and select your file you want to use:
public class Test
{
public static void main(String args[])
{
Mp3File mp3file = null;
try {
JFileChooser jfc = new JFileChooser();
int fileResult = jfc.showOpenDialog(null);
if (fileResult == JFileChooser.APPROVE_OPTION) {
String path = jfc.getSelectedFile().getPath();
mp3file = new Mp3File(path);
if (mp3file!=null && mp3file.hasId3v1Tag()) {
ID3v1 id3v1Tag = mp3file.getId3v1Tag();
System.out.println("Track: " + id3v1Tag.getTrack());
System.out.println("Artist: " + id3v1Tag.getArtist());
System.out.println("Title: " + id3v1Tag.getTitle());
System.out.println("Album: " + id3v1Tag.getAlbum());
System.out.println("Year: " + id3v1Tag.getYear());
System.out.println("Genre: " + id3v1Tag.getGenre() + "("+id3v1Tag.getGenreDescription() + ")");
System.out.println("Comment: " + id3v1Tag.getComment());
} else {
System.out.println("The mp3 file does not exists or does not have a ID3v1Tag");
}
}
} catch (UnsupportedTagException | InvalidDataException | IOException e) {
System.err.println("File not found.");
e.printStackTrace();
}
}
}
Solved it! System.out.println(System.getProperty("user.dir")); Pasted that into my code, found out the root dir. Apparently it's on the same level as the src and bin folders. Pasted the file there and it works like a charm now.
Alright, if anyone was wondering if you can change the home directory, you can't.
If you want to access another folder, you'll have to resort to directory traversal. Say your music file "Ride the Lightning.mp3" is in C:\Users\"Your Username"\Music\
Then to read that you'll have to do something like this:
mp3file = new Mp3File("../../../Music/Misc/Ride the Lightning.mp3");
Cheers to Brian and Chasmo for the helpful posts.