100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 上传手机文件ftp服务器 从android手机上传文件到FTP服务器?

上传手机文件ftp服务器 从android手机上传文件到FTP服务器?

时间:2019-10-03 19:47:43

相关推荐

上传手机文件ftp服务器 从android手机上传文件到FTP服务器?

您可以使用Simple Java FTP Client,并将其添加为您的项目外部JAR,您也可以参考这个link

public class FileUpload

{

/**

* Upload a file to a FTP server. A FTP URL is generated with the

* following syntax:

* ftp://user:[emailprotected]: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

{

if (ftpServer != null && fileName != null && source != null)

{

StringBuffer sb = new StringBuffer("ftp://");

// check for authentication else assume its anonymous access.

if (user != null && password != null)

{

sb.append(user);

sb.append(':');

sb.append(password);

sb.append('@');

}

sb.append(ftpServer);

sb.append('/');

sb.append(fileName);

/*

* type ==> a=ASCII mode, i=image (binary) mode, d= file directory

* listing

*/

sb.append(";type=i");

BufferedInputStream bis = null;

BufferedOutputStream bos = null;

try

{

URL url = new URL(sb.toString());

URLConnection urlc = url.openConnection();

bos = new BufferedOutputStream(urlc.getOutputStream());

bis = new BufferedInputStream(new FileInputStream(source));

int i;

// read byte by byte until end of stream

while ((i = bis.read()) != -1)

{

bos.write(i);

}

}

finally

{

if (bis != null)

try

{

bis.close();

}

catch (IOException ioe)

{

ioe.printStackTrace();

}

if (bos != null)

try

{

bos.close();

}

catch (IOException ioe)

{

ioe.printStackTrace();

}

}

}

else

{

System.out.println("Input not available.");

}

}

import .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(.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, progressHandler);

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

buffIn.close();

ftpClient.logout();

ftpClient.disconnect();

}

} catch (SocketException e) {

Log.e(SorensonApplication.TAG, e.getStackTrace().toString());

} catch (UnknownHostException e) {

Log.e(SorensonApplication.TAG, e.getStackTrace().toString());

} catch (IOException e) {

Log.e(SorensonApplication.TAG, e.getStackTrace().toString());

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。