SFTP connection throws odd FileTransferException that exposes SFTP password - java

It is really bizarre, why would zehon return my location of my eclipse, and my SFTP password as part of a FileSystemException?
I have checked that the remote host is indeed a SFTP server, and the client is connecting using SFTP.
Zehon API here
Stacktrace
Reading file from C:\srcFolde\FileToBeUploaded.zip
com.zehon.exception.FileTransferException:
org.apache.commons.vfs.FileSystemException:
Unknown message with code:
"C:<location of eclipse>/<sftp password>?" does not exist
at int result = sftp.sendFile(filePath, ftpDestFolder);
Code
SFTPClient sftp = new SFTPClient(ftpServer, 22, ftpUserName, ftpPassword, true);
FileInputStream fis = null;
try {
fis = new FileInputStream(fileName);
String filePath=fileName.substring(0, fileName.length()-4) + ".zip";
String ftpDestFolder="\\sftpDestFolder";
int result = sftp.sendFile(filePath, ftpDestFolder);
Logger.debug("sftp result = " + result);
} catch (FileTransferException e) {
e.printStackTrace();
return false;
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

You've used the wrong constructor. From the Javadocs
http://www.zehon.com/javadocs/com/zehon/sftp/SFTPClient.html
You've passed ftpPassword where it's expecting privateKeyPath.

Related

fetch data from ftp://ftp.funet.fi/pub/mirrors/ftp.imdb.com/pub/ using java

how can I fetch movie.list.gz folder from the link
1: ftp://ftp.funet.fi/pub/mirrors/ftp.imdb.com/pub/, I have used a code to access the FTP server for fetching data but it asks for port no. which I don't know. Can anyone please tell how to fetch data from the above link in java. I am developing a Web Application which will display all the movie names present in IMDB database.
CODE:
String server = "ftp://ftp.funet.fi/pub/mirrors/ftp.imdb.com";
int port = 21;
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// APPROACH #1: using retrieveFile(String, OutputStream)
String remoteFile1 = "/pub/movie.list.gz";
File downloadFile1 = new File("F:/softwares/mov.list.gz");
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
outputStream1.close();
if (success) {
System.out.println("File #1 has been downloaded successfully.");
}
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

How to copy a file with FTP Client using regular expression in Java

String server = "www.test.com";
int port = 21;
String username = "test";
String password = "test";
FTPClient ftpclient = new FTPClient();
try {
ftpclient.connect(server,port);
ftpclient.login(username, password);
ftpclient.enterLocalPassiveMode();
ftpclient.setFileType(FTP.BINARY_FILE_TYPE);
String remoteFile = "/home/test/workspace/9001_20150918165942_00085.xml";
File downloadfile = new File("C:/Users/Workspace/9001_20150918165942_00085.xml");
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadfile));
boolean success = ftpclient.retrieveFile(remoteFile, outputStream1);
outputStream1.close();
if (success) {
System.out.println("File has been downloaded successfully.");
}
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
}
finally {
try {
if (ftpclient.isConnected()) {
ftpclient.logout();
ftpclient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
I am using the above FTPClient to download a file from a Linux machine to Windows machine. For the intended file it's working fine.
But I am trying to achieve the same using regular expression. For instance, /home/test/workspace/*.xml which will be downloaded to C:/Users/Workspace/*.
Use the FTPClient.mlistDir method (or FTPClient.listFiles) to list files in the remote directory
Iterate the list to find files matching your pattern.
Download the matched files, one by one.

JAVA Commons-Net FTPClient, downloading files are corrupted

I am using commans.net api in order to perform tasks e.g uploading and
downloading files from the ftp server. I can perform those tasks
successfully but the downloaded files are corrupted. i couldn't open
those files in usual manner. please help me..
my simple code look like
public class FTPConnect {
public static void main(String[] args) {
startServer();
connectClient();
}
private static void startServer() {
FtpServerFactory serverFactory = new FtpServerFactory();
ListenerFactory factory = new ListenerFactory();
factory.setPort(21);// set the port of the listener (choose your desired
// port, not 1234)
System.out.println("port has been set to 21");
serverFactory.addListener("default", factory.createListener());
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
userManagerFactory.setFile(new File("lib/users.properties"));
System.out.println("users.properties has been set..");
userManagerFactory.setPasswordEncryptor(new PasswordEncryptor() {
#Override
public String encrypt(String password) {
return password;
}
#Override
public boolean matches(String passwordToCheck,
String storedPassword) {
return passwordToCheck.equals(storedPassword);
}
});
System.out.println("password has been encrypted...");
BaseUser user = new BaseUser();
user.setName("java");
user.setPassword("shiva.dave");
System.out.println("password has been set..to java and shiva.dave");
user.setHomeDirectory("lib");
System.out.println("home directory has been set...");
List<Authority> authorities = new ArrayList<Authority>();
authorities.add(new WritePermission());
user.setAuthorities(authorities);
UserManager um = userManagerFactory.createUserManager();
try {
um.save(user);// Save the user to the user list on the filesystem
System.out.println("user has been set to the filesystem..");
} catch (FtpException e1) {
e1.printStackTrace();
}
serverFactory.setUserManager(um);
FtpServer server = serverFactory.createServer();
try
{
server.start();//Your FTP server starts listening for incoming FTP-connections, using the configuration options previously set
System.out.println("Server has been started.....");
}
catch (FtpException ex)
{
ex.printStackTrace();
}
}
private static void connectClient() {
FTPClient client = new FTPClient();
try{
client.connect(InetAddress.getLocalHost(), 21);
String loging_success = client.login("java", "shiva.dave") == true ? "success" : "failed";
System.out.println("login: " + loging_success);
FTPFile[] clients = client.listFiles("/");
System.out.println("Listed " + clients.length + " files.");
for (FTPFile file : clients) {
System.out.println(file.getName());
}
for (FTPFile ftpFile : clients) {
String remoteFile2 = ftpFile.getName();
File downloadFile2 = new File("D:/test2/"+ftpFile.getName());
OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(downloadFile2));
InputStream inputStream = client.retrieveFileStream(remoteFile2);
byte[] bytesArray = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(bytesArray)) != -1) {
outputStream2.write(bytesArray, 0, bytesRead);
}
boolean success = client.completePendingCommand();
if (success) {
System.out.println("File #2 has been downloaded successfully.");
}
outputStream2.close();
inputStream.close();
}
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
client.logout();
client.disconnect();
}
catch(Exception e){
e.printStackTrace();
}
}
}
}
I founded a solution and want to put that here..
first of all when you try to download files from the ftp server you must need to specify the file type and transfer mode in order to download file successfully.
here is the code you need to insert.
client.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
client.setFileTransferMode(FTP.BINARY_FILE_TYPE);
"WATCH IT GUYS"
you must need to insert these two lines of code after successfully logged in otherwise you wouldn't be able to download files successfully...
Hope this answer will help you to download files without being corrupted..

upload files to a cloud account

i need to develop an application which will peridiocally upload files from my local machine to a cloud account(cloudme account)periodically.I tried this with Java and apache commons.but i couldnt.What technology i follow.Where can i find tutorials for this?pls help?
1. I always prefer Apache's common libs
I am also attaching my program which i used to upload and download song to ftp server using the apache's common lib
Uploading :
public void goforIt(){
FTPClient con = null;
try
{
con = new FTPClient();
con.connect("192.168.2.57");
if (con.login("Administrator", "KUjWbk"))
{
con.enterLocalPassiveMode(); // important!
con.setFileType(FTP.BINARY_FILE_TYPE);
String data = "/sdcard/vivekm4a.m4a";
FileInputStream in = new FileInputStream(new File(data));
boolean result = con.storeFile("/vivekm4a.m4a", in);
in.close();
if (result) Log.v("upload result", "succeeded");
con.logout();
con.disconnect();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
Dowloading:
public void goforIt(){
FTPClient con = null;
try
{
con = new FTPClient();
con.connect("192.168.2.57");
if (con.login("Administrator", "KUjWbk"))
{
con.enterLocalPassiveMode(); // important!
con.setFileType(FTP.BINARY_FILE_TYPE);
String data = "/sdcard/vivekm4a.m4a";
OutputStream out = new FileOutputStream(new File(data));
boolean result = con.retrieveFile("vivekm4a.m4a", out);
out.close();
if (result) Log.v("download result", "succeeded");
con.logout();
con.disconnect();
}
}
catch (Exception e)
{
Log.v("download result","failed");
e.printStackTrace();
}
}
I think, you should use WebDAV because cloudme supports that way.
There are also some java libs out there

How to download a text file via FTP and read the contents to a string

This is re-worded from a previous question (which was probably a bit unclear).
I want to download a text file via FTP from a remote server, read the contents of the text file into a string and then discard the file. I don't need to actually save the file.
I am using the Apache Commons library so I have:
import org.apache.commons.net.ftp.FTPClient;
Can anyone help please, without simply redirecting me to a page with lots of possible answers on?
Not going to do the work for you, but once you have your connection established, you can call retrieveFile and pass it an OutputStream. You can google around and find the rest...
FTPClient ftp = new FTPClient();
...
ByteArrayOutputStream myVar = new ByteArrayOutputStream();
ftp.retrieveFile("remoteFileName.txt", myVar);
ByteArrayOutputStream
retrieveFile
Normally I'd leave a comment asking 'What have you tried?'. But now I'm feeling more generous :-)
Here you go:
private void ftpDownload() {
FTPClient ftp = null;
try {
ftp = new FTPClient();
ftp.connect(mServer);
try {
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
throw new Exception("Connect failed: " + ftp.getReplyString());
}
if (!ftp.login(mUser, mPassword)) {
throw new Exception("Login failed: " + ftp.getReplyString());
}
try {
ftp.enterLocalPassiveMode();
if (!ftp.setFileType(FTP.BINARY_FILE_TYPE)) {
Log.e(TAG, "Setting binary file type failed.");
}
transferFile(ftp);
} catch(Exception e) {
handleThrowable(e);
} finally {
if (!ftp.logout()) {
Log.e(TAG, "Logout failed.");
}
}
} catch(Exception e) {
handleThrowable(e);
} finally {
ftp.disconnect();
}
} catch(Exception e) {
handleThrowable(e);
}
}
private void transferFile(FTPClient ftp) throws Exception {
long fileSize = getFileSize(ftp, mFilePath);
InputStream is = retrieveFileStream(ftp, mFilePath);
downloadFile(is, buffer, fileSize);
is.close();
if (!ftp.completePendingCommand()) {
throw new Exception("Pending command failed: " + ftp.getReplyString());
}
}
private InputStream retrieveFileStream(FTPClient ftp, String filePath)
throws Exception {
InputStream is = ftp.retrieveFileStream(filePath);
int reply = ftp.getReplyCode();
if (is == null
|| (!FTPReply.isPositivePreliminary(reply)
&& !FTPReply.isPositiveCompletion(reply))) {
throw new Exception(ftp.getReplyString());
}
return is;
}
private byte[] downloadFile(InputStream is, long fileSize)
throws Exception {
byte[] buffer = new byte[fileSize];
if (is.read(buffer, 0, buffer.length)) == -1) {
return null;
}
return buffer; // <-- Here is your file's contents !!!
}
private long getFileSize(FTPClient ftp, String filePath) throws Exception {
long fileSize = 0;
FTPFile[] files = ftp.listFiles(filePath);
if (files.length == 1 && files[0].isFile()) {
fileSize = files[0].getSize();
}
Log.i(TAG, "File size = " + fileSize);
return fileSize;
}
You can just skip the download to local filesystem part and do:
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
InputStream inputStream = ftpClient.retrieveFileStream("/folder/file.dat");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "Cp1252"));
while(reader.ready()) {
System.out.println(reader.readLine()); // Or whatever
}
inputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}

Categories

Resources