Вы находитесь на странице: 1из 4

Stack Overflow sign up log in

Questions Jobs Tags Users Badges Ask

Read this post in our app!

1 Uploading a file to a FTP server from android phone?


java android ftp apache-commons apache-commons-net

Following is the code that's suppose to create a text document and upload it to my FTP server. For some
reason it doesn't seem to work. I used to the libraries provided at
http://lavalatwork.blogspot.tw/2010/09/using-apache-commons-ftp-library-in.html

for communicating with the FTP server.

catch(IOException ex)
{

FTPClient mFTP = new FTPClient();


try {
// Connect to FTP Server
mFTP.connect("192.168.10.101");
//mFTP.login("user", "password");
mFTP.setFileType(FTP.BINARY_FILE_TYPE);
mFTP.enterLocalPassiveMode();

// Prepare file to be uploaded to FTP Server


File file = new File(getFileStreamPath("samplefile.txt")+ "");
FileInputStream ifile = new FileInputStream(file);

// Upload file to FTP Server


Any help would be appreciated.

share improve this question

Gdgames Gamers asked


88 1 1 12 Jul 22 '12 at 14:13

Dheeraj V.S. edited


8,923 4 35 73 Jul 22 '12 at 14:59

Checking the output of logcat is often very helpful when using Apache Commons FTP client. Dheeraj V.S. Jul 22 '12 at
14:53

1 Please be explicit about what exactly does not work. And post your logcat. Dheeraj V.S. Jul 22 '12 at 14:56

add a comment
2 Answers order by votes

See this...... This will help you rectify the probs in your code.
12
I have used the apache's commons library to upload and download an Audio file to and from the
Server... see this...

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");
Downloading:

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();

share improve this answer

Kumar Vivek Mitra answered


25k 5 25 54 Jul 22 '12 at 14:18

You can use Simple Java FTP Client and add it as externel jar for your projec, you can also refer to
1 this link
public class FileUpload
{

/**
* Upload a file to a FTP server. A FTP URL is generated with the
* following syntax:
* ftp://user:password@host:port/filePath;type=i.
*
* @param ftpServer , FTP server address (optional port ':portNumber').
* @param user , Optional user name to login.
* @param password , Optional password for user.
* @param fileName , Destination file name on FTP server (with optional
* preceding relative path, e.g. "myDir/myFile.txt").
* @param source , Source file to upload.
* @throws MalformedURLException, IOException on error.
*/
public void upload( String ftpServer, String user, String password,
String fileName, File source ) throws MalformedURLException,
IOException
{
You can also use the Apache commons-net-ftp library, for more details you can focus on this link.

import org.apache.commons.net.ftp.FTPClient;

FTPClient ftpClient = new FTPClient();

try {
ftpClient.connect(InetAddress.getByName(SERVER));
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();
ProgressInputStream progressInput = new ProgressInputStream(buffIn, progressHandle

boolean result = ftpClient.storeFile(localAsset.getFileName(), progressInput


buffIn.close();
ftpClient.logout();
ftpClient.disconnect();

share improve this answer

K_Anas answered
22k 5 43 71 Jul 22 '12 at 14:52

Your Answer

Body
Add picture

Log in

OR

Name

Email

By posting your answer, you agree to the privacy policy and terms of service.

Post Your Answer

meta chat tour help blog privacy policy legal contact us full site

Download the Stack Exchange Android app


2016 Stack Exchange, Inc

Вам также может понравиться