upload files to a cloud account - java

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

Related

Error when connecting to FTP using apache commons

I am using apache common library for connecting to FTP with Android app.
Now I want to upload a file from internal storage to FTP server and I get this reply from getReplyString() method.
And I get this msg
553 Can't open that file: Permission denied
//Write file to the internal storage
String path = "/sdcard/";
File file = new File(path, fileName);
FileOutputStream stream = null;
try {
stream = new FileOutputStream(file);
stream.write(jsonObject.toString().getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// Read the file from resources folder.
try {
File file1 = new File(path, fileName);
Log.d("path",file1.getPath());
BufferedInputStream in = new BufferedInputStream (new FileInputStream (file1.getPath()));
client.connect(FTPHost);
client.login(FTPUserName, FTPPassword);
client.enterLocalPassiveMode();
client.setFileType(FTP.BINARY_FILE_TYPE);
// Store file to server
Log.d("reply",client.getReplyString());
boolean res = client.storeFile("/"+fileName, in);
Log.d("reply",client.getReplyString());
Log.d("result",res+"");
client.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}

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.

SFTP connection throws odd FileTransferException that exposes SFTP password

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.

Unable to Upload file on FTP Server in android

I am trying to Upload file like this
try {
String extStorageDirectory = Environment.getExternalStorageDirectory().toString
FTPClient ftpClient = new FTPClient();
ftpClient.connect("xxx.xxx.xx.xx");
if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode()))
{
boolean status=ftpClient.login("username", "password");
Log.d(TAG, "login status=="+status);
status=ftpClient.changeWorkingDirectory("New directory");
Log.d(TAG, "changeWorkingDirectory status=="+status);
status=ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
Log.d(TAG, "setFileType status=="+status);
ftpClient.enterLocalPassiveMode();
String srcFilePath=extStorageDirectory + "/AA.txt";
FileInputStream srcFileStream = new FileInputStream(new File(srcFilePath));
status=ftpClient.storeFile("AA.txt", srcFileStream);
Log.d(TAG, "upload status=="+status);
ftpClient.logout();
ftpClient.disconnect();
}
else
{
Log.d(TAG, "connectfail");
}
} catch (SocketException e) {
Log.d(TAG, "SocketException status=="+e.toString());
e.printStackTrace();
} catch (FileNotFoundException e) {
Log.d(TAG, "FileNotFoundException status=="+e.toString());
e.printStackTrace();
} catch (IOException e) {
Log.d(TAG, "IOException status=="+e.toString());
e.printStackTrace();
}
below is my logcat status
07-11 12:24:43.359: D/FTPDownloadDroid(10647): <!>com.ss.dr 138<!> login status==true
07-11 12:24:48.379: D/FTPDownloadDroid(10647): <!>com.ss.dr 141<!> changeWorkingDirectory status==true
07-11 12:24:48.859: D/FTPDownloadDroid(10647): <!>com.ss.dr 143<!> setFileType status==tr
07-11 12:24:54.359: D/FTPDownloadDroid(10647): <!>com.ss.dr 150<!> upload status==false
I want to Upload file on "New Directory" folder and file name AA.txt ** but it is giving Upload status false.**
Is the problem in server or in my Code???
Please help!!!!!!!!!
Thanks in advance
Try my code below, i used this to upload and download a song on the server. I am using the Apache's common lib.
Please make the changes for the directories and file name in the below code.
UPLOAD:
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();
}
}
DOWNLOAD:
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();
}
}

Categories

Resources