日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android客户端是手机版下载视频格式,Android手机FTP客户端开发

發布時間:2024/1/1 Android 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android客户端是手机版下载视频格式,Android手机FTP客户端开发 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

FTP簡介FTP 是File Transfer Protocol(文件傳輸協議)的英文簡稱,而中文簡稱為“文傳協議”。用于Internet上的控制文件的雙向傳輸。同時,它也是一個應用程序(Application)。基于不同的操作系統有不同的FTP應用程序,而所有這些應用程序都遵守同一種協議以傳輸文件。在FTP的使用當中,用戶經常遇到兩個概念:"下載"(Download)和"上傳"(Upload)。"下載"文件就是從遠程主機拷貝文件至自己的計算機上;"上傳"文件就是將文件從自己的計算機中拷貝至遠程主機上。用Internet語言來說,用戶可通過客戶機程序向(從)遠程主機上傳(下載)文件。

實現簡單了解了FTP協議之后,開始代碼實現。如果要自己完整實現FTP通訊,其實工作量還有點的,在這個高效率的時代,誰還沒事全部自己手寫呢?JAVA開源的框架多的去了,這里我們用到的FTP框架是ftp4j-1.7.2?,里面已經封裝好了我們要用的一些函數,如連接遠程FTP服務器,登錄,列出文件列表,下載文件……

看簡單寫的一個例子

package com.androiddemo;

import it.sauronsoftware.ftp4j.FTPAbortedException;

import it.sauronsoftware.ftp4j.FTPClient;

import it.sauronsoftware.ftp4j.FTPDataTransferException;

import it.sauronsoftware.ftp4j.FTPException;

import it.sauronsoftware.ftp4j.FTPFile;

import it.sauronsoftware.ftp4j.FTPIllegalReplyException;

import it.sauronsoftware.ftp4j.FTPListParseException;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

public class MainActivity extends Activity {

private FTPClient client;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

FileUtil.init(getApplication());

setContentView(R.layout.activity_main);

new Thread() {

public void run() {

initFtp();

}

}.start();

}

public void initFtp() {

try {

client = new FTPClient();

// 連接我本地的服務器

String[] cInfo = client.connect("113.57.128.59");

for (String string : cInfo) {

println(string);

}

client.login("plant", "plant");

FTPFile[] files = client.list();

for (FTPFile ftpFile : files) {

String fileName = ftpFile.getName();

if (fileName.equals(".") || fileName.equals(".."))

continue;

download(fileName);

}

} catch (IllegalStateException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (FTPIllegalReplyException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (FTPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (FTPDataTransferException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (FTPAbortedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (FTPListParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

private void download(String fileName) throws IllegalStateException,

FileNotFoundException, IOException, FTPIllegalReplyException,

FTPException, FTPDataTransferException, FTPAbortedException {

String writePath = FileUtil.getFileDir() + fileName;

client.download(fileName, new File(writePath));

}

void disconnectFtp() {

new Thread() {

@Override

public void run() {

// TODO Auto-generated method stub

super.run();

close();

}

}.start();

}

void close() {

try {

if (client != null)

client.disconnect(true);

} catch (IllegalStateException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (FTPIllegalReplyException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (FTPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

void println(String text) {

Log.i("FTP", text);

}

@Override

public void finish() {

// TODO Auto-generated method stub

super.finish();

disconnectFtp();

}

@Override

protected void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

System.exit(0);

}

}

文件類

package com.androiddemo;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import android.content.Context;

import android.os.Environment;

import android.os.StatFs;

/**

* 圖片遠程下載、保存

*/

public class FileUtil {

private static Context context;

private static String storage = Environment.getExternalStorageState();

private static String fileDir = FileUtil.class.getName() + "/";

public static enum FileType {

JPG, PNG, BMP, RAR, ZIP, SEVENZ, OTHER

};

public static Context getContext() {

return context;

}

public static void init(Context context) {

FileUtil.context = context;

if (isMounted()) {

fileDir = Environment.getExternalStorageDirectory()

.getAbsolutePath() + File.separator + fileDir;

} else {

fileDir = context.getFilesDir().getAbsolutePath() + File.separator

+ fileDir;

}

File file = new File(fileDir);

if (!file.exists())

file.mkdir();

}

/**

* @param fileName

* @return

*/

public static File readFile(String fileName) {

if (fileName == null)

return null;

StringBuffer buffer = new StringBuffer(fileDir);

buffer.append(File.separatorChar);

buffer.append(fileName);

File file = new File(buffer.toString());

if (file.exists())

return file;

return null;

}

public static String getFileType(String fileName) {

int pos = fileName.lastIndexOf(".");

if (pos != -1 && pos < fileName.length()) {

String dot = fileName.substring(pos + 1);

return dot;

}

return "png";

}

/**

* 向SD卡寫文件

*

* @param path

* @param data

* @isRecovery 是否覆蓋

* @return

*/

public static boolean writeFile(String path, byte[] data, boolean isRecovery) {

try {

File file = new File(path);

if (file.exists() && !isRecovery)

return true;

file.createNewFile();

FileOutputStream fos = new FileOutputStream(file);

fos.write(data);

fos.flush();

fos.close();

return true;

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return false;

}

/**

* 是否掛載SD卡

*

* @return

*/

public static boolean isMounted() {

return storage.equals(Environment.MEDIA_MOUNTED);

}

// 獲取本機容量信息

public long phoneCapacity() {

// 獲取本機信息

File data = context.getFilesDir();

StatFs statFs = new StatFs(data.getPath());

long availableBlocks = statFs.getAvailableBlocks();// 可用存儲塊的數量

long size = statFs.getBlockSize();// 每塊存儲塊的大小

long availableSize = availableBlocks * size;// 可用容量

return availableSize;

}

// 獲取sdcard容量信息

public long sdcardCapacity() {

// 獲取sdcard信息

File sdData = Environment.getExternalStorageDirectory();

StatFs sdStatFs = new StatFs(sdData.getPath());

long sdAvailableBlocks = sdStatFs.getAvailableBlocks();// 可用存儲塊的數量

long sdSize = sdStatFs.getBlockSize();// 每塊存儲塊的大小

long sdAvailableSize = sdAvailableBlocks * sdSize;

return sdAvailableSize;

}

public static String getFileDir() {

return fileDir;

}

}

執行結果如下圖:

然后會在SD卡中創建一個目錄,目錄下面就有FTP目錄對應的文件了。

總結

以上是生活随笔為你收集整理的android客户端是手机版下载视频格式,Android手机FTP客户端开发的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。