f.connect("ftp.drivehq.com",21);
if(f.login("XXXX", "XXX"))
{
f.enterLocalPassiveMode();
f.setFileType(FTP.BINARY_FILE_TYPE);
FileInputStream in = new FileInputStream(URL);
result = f.storeFile(newfile, in);
in.close();
f.logout();
f.disconnect();
}
It upload fine but not in particular path i also try as ftp://ftp.drivehq.com/myfolder
but it gives error in android unknown host exception like I need to upload file in particular folder So thanks in advance help me
Hi Please use the following code. it worked for me i have checked it.
SimpleFTP ftp = new SimpleFTP();
try
{
// Connect to an FTP server on port 21.
ftp.connect("host", 21, "username", "password");
// Set binary mode.
ftp.bin();
// Change to a new working directory on the FTP server.
ftp.cwd("/httpdocs/yourdestinationfolderinftp");
// Upload some files.
ftp.stor(new File("/mnt/sdcard/ftp.jpg"));
// Quit from the FTP server.
ftp.disconnect();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
Related
I am creating a sync service which will copy files to ftp folder selected by user. When i run the service, it connects with ftp and check whether file at android local storage is available at ftp or not and if not, it uploads. Below is my code.
FTPClient ftpClient = new FTPClient();
ftpClient.setPassive(true);
if (!ftpClient.isConnected()) {
ftpClient.connect(server, port);
}else {
Log.i(TAG,"FTP already connected");
}
if (!ftpClient.isAuthenticated()) {
ftpClient.login(username, password);
}else {
Log.i(TAG,"FTP already Logged In");
}
ftpClient.changeDirectory(rfolderpath);
String[] files = ftpClient.listNames();
if (Arrays.asList(files).contains(filename)) {
Log.i(TAG, filename + " Already Found in FTP, Skipping");
} else {
Log.i(TAG, "Sending Go Ahead For Upload");
File file = new File(filepath);
Log.i(TAG, "Uploading File: " + filename);
ftpClient.upload(file);
ftpClient.logout();
ftpClient.disconnect(true);
}
The code works fine for first 8 files and then i start getting exception of too many connections (8) from this IP and my sync terminates.
Here is Error text:
it.sauronsoftware.ftp4j.FTPException [code=421, message= Too many connections (8) from this IP]
Can someone help me how to handle this scenario.
I changed the logic of uploading files. Now created one ftp connection under AsyncTaskLoader and with that upload all files w/o error.
I am working on Volley Multipart to upload images to server.
for that I am using
mBuilder.addBinaryBody(headerKeyName, file, ContentType.create("image/jpg"),FILEPATH);
Now I have to send the empty file along with some headers.
how can I use that method for empty file.
Please someone help me.
Hi you can do something like this:
File file = new File("path");
try {
if (file.createNewFile()) {
mBuilder.addBinaryBody(headerKeyName, file,ContentType.create("image/jpg"), file.getAbsoluteFile());
}
} catch (IOException e) {
e.printStackTrace();
}
Regards.
I create small tracking app which get coordinates write them to xml file and then send this file with that data to ftp server. I am using function "SendFileGPS" to send that file but apk all the time crashes and it doesn't work. This function is using additional library common net 1.4 and it is quite strange because all the time when I start Android Studio I must download this library from web by Android Studio. And I got some additional questions.
I was thinking that maybe not only this function is a problem, how should look this server I mean how proper this server to get file from this app?
There is a another way to send this file without using additional libs ?
How it should look if I want send this file to another type of server (HTTPS)?
public void SendFileGPS()
{
FTPClient mFTP = new FTPClient();
try {
// Connect to FTP Server
mFTP.connect("here I put adress IP");
mFTP.login("here I put login", "here I put pass");
mFTP.setFileType(FTP.BINARY_FILE_TYPE);
mFTP.enterLocalPassiveMode();
// Prepare file to be uploaded to FTP Server
File file = new File("/path/to/MyXmlFileName.xml");
FileInputStream ifile = new FileInputStream(file);
// Upload file to FTP Server
mFTP.storeFile("filetotranfer",ifile);
mFTP.disconnect();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
i want to upload captured image from an android phone to Ftp Server i have written following code i m facing error at this line ftp.store(remote,input) after this line
i m getting replycode=550 file unavialable i m using apache commons-net 3.3 library i have tried
same with WebClient in .net it works fine but in java its gives me dis error(550) i have searched many sites but no help i even called hosting service they said no problem from there side i stucked from last 1 day plz correct me what i am missing here......
my code :
private void UploadImage(){
FTPClient ftp = new FTPClient();
try {
ftp.connect("hostname",21);// Using port no=21
int reply = ftp.getReplyCode(); //here i m getting 220
ftp.login("abc", "abc");
reply = ftp.getReplyCode(); //here i m getting 230
ftp.setFileType(FTP.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
ftp.enterLocalPassiveMode();
reply = ftp.getReplyCode();
File sfile = new File(PhotoPath); //here i m getting /mnt/sdcard/DCIM/CameraSample/abc769708880.jpg
String filename = sfile.getName();
FileInputStream fis = new FileInputStream(sfile);
boolean aRtn=ftp.storeFile("ftpPath"+sfile.getName(), fis);// return true if successful
reply = ftp.getReplyCode(); // here im getting 550
if(aRtn == true)
{
Toast toast= Toast.makeText(getApplicationContext(),
"Successfull", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER, 0, 0);
toast.show();
}
fis.close();
ftp.disconnect();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
That exception seems to be thrown when you try to perform network operations (such as FTP) from your main thread. This is not allowed for performance reasons (so that the application doesn't appear to lock up to the user, when performing an action which may take a while). Assuming you are using Honeycomb or higher, you would need to move the code that makes the connection into its own child thread.
http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
http://developer.android.com/guide/practices/design/responsiveness.html
What I want to achieve in my application is:
I have an account on ftp site like "filegenie.com" where i store some images.
Now I want to view those images when i start my application.
How can I do this in android? Can any one give me an head start into how those files should be accessed from the ftp site? I actually have no idea in this section. some simple tutorials, hints, examples are welcome.
My sole objective is to view images stored on filegenie.com from my android application
I have to do something similar with an FTP server I run off a destop. I downloaded a couple helper classes that are quite simple to use: FTP Classes Site
If you are using the android eclipse plugin open windows explorer and copy the jars into the "libs" folder in your project.
Here is a snipet of the type of code you would need:
import org.apache.commons.net.ftp.FTPClient;
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(my.ip.com,port);
ftpClient.login(USERNAME, PASSWORD);
ftpClient.changeWorkingDirectory(PATH);
if (ftpClient.getReplyString().contains("250")) {
ftpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
BufferedInputStream buffIn = null;
buffIn = new BufferedInputStream(new FileInputStream(FULL_PATH_TO_LOCAL_FILE));
ftpClient.enterLocalPassiveMode();
boolean result = ftpClient.storeFile(FILENAME, buffIn);
buffIn.close();
ftpClient.logout();
ftpClient.disconnect();
}
} catch (SocketException e) {
System.err.print...
} catch (UnknownHostException e) {
System.err.print...
} catch (IOException e) {
System.err.print...
}
if(!result)
{.....}
Feel free to look through the FTPClient library