今天同學說他備份了聯系人的資料放在一個檔案裡,想把它存到伺服器上,以便之後可以進行下載下傳恢複..于是幫他寫了個上傳,下載下傳檔案的demo
主要是 跟FTP伺服器打交道~因為這個東東有免費的可以身親哈
1. 首先申請一個免費的FTP空間:
http://www.3v.cm/host/
我這邊注冊了個賬号,密碼分别為:
chenww
chenww
2. 檢視FTP空間的位址(端口預設是21)
FTP伺服器: chenww.3vfree.us
端口: 21
3. Demo部分
主Activity: FTPMainActivity
<span style="color:#000000">package com.example.ftpdemo;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
/**
* 2013年09月20日21:28:51 測試demo
*
* 1.檔案上傳到FTP伺服器
* 2.從FTP伺服器上下載下傳檔案
*
* 所需jar包:commons-net-3.0.1.jar
* 将commons-net-3.0.1.jar放于libs中
*
* @author xiaoyaomeng
*
*/
public class FTPMainActivity extends Activity implements OnClickListener {
//傻逼Buttons
private Button buttonUpLoad = null;
private Button buttonDownLoad = null;
//FTP工具類
private FTPUtils ftpUtils = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ftpmain);
//擷取控件對象
buttonUpLoad = (Button) findViewById(R.id.button_upload);
buttonDownLoad = (Button) findViewById(R.id.button_download);
//設定控件對應相應函數
buttonUpLoad.setOnClickListener(this);
buttonDownLoad.setOnClickListener(this);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
//初始化和FTP伺服器互動的類
InitFTPServerSetting();
}
public void InitFTPServerSetting() {
// TODO Auto-generated method stub
ftpUtils = FTPUtils.getInstance();
boolean flag = ftpUtils.initFTPSetting("chenww.3vfree.us", 21, "chenww", "chenww");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.ftpmain, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button_upload: {
//上傳檔案
}
break;
case R.id.button_download: {
//下載下傳檔案
}
break;
default:
break;
}
}
}</span>
工具類:FTPUtils
<strong>package com.example.ftpdemo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import android.R.bool;
/**
* 2013年09月20日21:38:04
*
* 用于Android和FTP伺服器進行互動的工具類
*
* @author xiaoyaomeng
*
*/
public class FTPUtils {
private FTPClient ftpClient = null;
private static FTPUtils ftpUtilsInstance = null;
private String FTPUrl;
private int FTPPort;
private String UserName;
private String UserPassword;
private FTPUtils()
{
ftpClient = new FTPClient();
}
/*
* 得到類對象執行個體(因為隻能有一個這樣的類對象,是以用單例模式)
*/
public static FTPUtils getInstance() {
if (ftpUtilsInstance == null)
{
ftpUtilsInstance = new FTPUtils();
}
return ftpUtilsInstance;
}
/**
* 設定FTP伺服器
* @param FTPUrl FTP伺服器ip位址
* @param FTPPort FTP伺服器端口号
* @param UserName 登陸FTP伺服器的賬号
* @param UserPassword 登陸FTP伺服器的密碼
* @return
*/
public boolean initFTPSetting(String FTPUrl, int FTPPort, String UserName, String UserPassword)
{
this.FTPUrl = FTPUrl;
this.FTPPort = FTPPort;
this.UserName = UserName;
this.UserPassword = UserPassword;
int reply;
try {
//1.要連接配接的FTP伺服器Url,Port
ftpClient.connect(FTPUrl, FTPPort);
//2.登陸FTP伺服器
ftpClient.login(UserName, UserPassword);
//3.看傳回的值是不是230,如果是,表示登陸成功
reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply))
{
//斷開
ftpClient.disconnect();
return false;
}
return true;
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
/**
* 上傳檔案
* @param FilePath 要上傳檔案所在SDCard的路徑
* @param FileName 要上傳的檔案的檔案名(如:Sim唯一辨別碼)
* @return true為成功,false為失敗
*/
public boolean uploadFile(String FilePath, String FileName) {
if (!ftpClient.isConnected())
{
if (!initFTPSetting(FTPUrl, FTPPort, UserName, UserPassword))
{
return false;
}
}
try {
//設定存儲路徑
ftpClient.makeDirectory("/data");
ftpClient.changeWorkingDirectory("/data");
//設定上傳檔案需要的一些基本資訊
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("UTF-8");
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
//檔案上傳吧~
FileInputStream fileInputStream = new FileInputStream(FilePath);
ftpClient.storeFile(FileName, fileInputStream);
//關閉檔案流
fileInputStream.close();
//退出登陸FTP,關閉ftpCLient的連接配接
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
/**
* 下載下傳檔案
* @param FilePath 要存放的檔案的路徑
* @param FileName 遠端FTP伺服器上的那個檔案的名字
* @return true為成功,false為失敗
*/
public boolean downLoadFile(String FilePath, String FileName) {
if (!ftpClient.isConnected())
{
if (!initFTPSetting(FTPUrl, FTPPort, UserName, UserPassword))
{
return false;
}
}
try {
// 轉到指定下載下傳目錄
ftpClient.changeWorkingDirectory("/data");
// 列出該目錄下所有檔案
FTPFile[] files = ftpClient.listFiles();
// 周遊所有檔案,找到指定的檔案
for (FTPFile file : files) {
if (file.getName().equals(FileName)) {
//根據絕對路徑初始化檔案
File localFile = new File(FilePath);
// 輸出流
OutputStream outputStream = new FileOutputStream(localFile);
// 下載下傳檔案
ftpClient.retrieveFile(file.getName(), outputStream);
//關閉流
outputStream.close();
}
}
//退出登陸FTP,關閉ftpCLient的連接配接
ftpClient.logout();
ftpClient.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
}</strong>
需要的Jar包下載下傳位址:
commons-net-3.0.1.jar