網路檔案下載─FTP: 從中華電信提供的網路存放空間,下載檔案。
使用外部元件: Apache Commons NET
commons-net-3.0.1-src.zip 的class : 下載點
1. Unzip it.
2. 到Eclipse的android作業平台上的 File -> Import -> General -> File System
3. 然後 Browse 剛才Unzip後 存放 commons-net-3.0.1-src 的夾子,找到 java 的子目錄,然後按『確定』。
需開啟之權限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
main.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.http.util.EncodingUtils;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class FTPDownloadFileActivity extends Activity {
/** Called when the activity is first created. */
private TextView tv;
private TextView tvContent;
private String fileName = "myFile.txt"; //網站上準備要下載的檔名
private final String SD_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();
private final String FILE_PATH = "/download";
private final String FILE_NAME = "copy_" + fileName; //下載進手機後的檔名
private final String ENCODING = "UTF-8";
private FileInputStream fis = null;
private ProgressDialog mProgressDialog;
private Handler handler = new Handler(){ //使用handler, thread 防止下載檔案太大
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
mProgressDialog.dismiss(); //close
readFile();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.textview);
tvContent = (TextView) findViewById(R.id.textviewContent);
processThread();
}
private void ftpDownload() { //使用FTP 下載檔案
FTPClient ftpClient = new FTPClient();
try {
// ftpClient.connect(InetAddress.getByName("203.66.87.21"));//與以下的String 意義一樣
ftpClient.connect(InetAddress.getByName("ftp.myweb.hinet.net")); //中華電信的網頁IP ADDRESS
ftpClient.login("username", "password");
tv.setText(String.valueOf(ftpClient.isConnected()));
ftpClient.changeWorkingDirectory("/");
File localfile = new File ("/sdcard/download/" + FILE_NAME);
OutputStream os = new FileOutputStream(localfile);
ftpClient.enterLocalPassiveMode();
ftpClient.retrieveFile(fileName, os);
ftpClient.logout();
ftpClient.disconnect();
}catch (Exception e) {
e.printStackTrace();
}
}
public void readFile () { //將內容顯示到螢幕上
String result = "";
try {
File mFilePath = new File (SD_PATH + FILE_PATH);
String mFileName = mFilePath + "/" + FILE_NAME;
File fileText = new File (mFileName);
fis = new FileInputStream(fileText);
int length = fis.available();
byte [] buffer = new byte [length];
fis.read(buffer);
result = EncodingUtils.getString(buffer, ENCODING);
fis.close();
tvContent.setText(result);
} catch (Exception e) {
e.printStackTrace();
}
}
private void processThread() {
mProgressDialog = ProgressDialog.show(this, "文件下載", "正在下載...");
new Thread () {
@Override
public void run() {
// TODO Auto-generated method stub
//super.run();
ftpDownload();
prolongedActionMethod(); //做測試用的
handler.sendEmptyMessage(0);
}
}.start();
}
private void prolongedActionMethod() { //故意延遲十秒鐘,做測試用的
try {
Thread.sleep(10000);
}catch (Exception e){
e.printStackTrace();
}
}
}
*************************************************
res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<!-- 將 TextView 包在 ScrollView 裡,當內容太長,可以捲動。 -->
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fadingEdge="vertical"
android:paddingTop="10dip"
android:scrollbars="vertical" >
<TextView
android:id="@+id/textviewContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dip"
android:text=""
android:textSize="20dp"
android:textColor="#00FF00" />
</ScrollView>
</LinearLayout>
沒有留言:
張貼留言