亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android關(guān)于FTP文件上傳和下載功能詳解

 更新時間:2017年09月21日 11:41:14   作者:一諾的秘密花園  
這篇文章主要為大家詳細(xì)介紹了Android關(guān)于FTP文件上傳和下載功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android九宮格圖片展示的具體代碼,供大家參考,具體內(nèi)容如下

此篇博客為整理文章,供大家學(xué)習(xí)。

1.首先下載commons-net  jar包,可以百度下載。

FTP的文件上傳和下載的工具類:

package ryancheng.example.progressbar; 
 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.RandomAccessFile; 
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.os.Environment; 
 
public class FTPManager { 
 FTPClient ftpClient = null; 
 
 public FTPManager() { 
  ftpClient = new FTPClient(); 
 } 
 
 // 連接到ftp服務(wù)器 
 public synchronized boolean connect() throws Exception { 
  boolean bool = false; 
  if (ftpClient.isConnected()) {//判斷是否已登陸 
   ftpClient.disconnect(); 
  } 
  ftpClient.setDataTimeout(20000);//設(shè)置連接超時時間 
  ftpClient.setControlEncoding("utf-8"); 
  ftpClient.connect("ip地址", 端口); 
  if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { 
   if (ftpClient.login("用戶名", "密碼")) { 
    bool = true; 
    System.out.println("ftp連接成功"); 
   } 
  } 
  return bool; 
 } 
 
 // 創(chuàng)建文件夾 
 public boolean createDirectory(String path) throws Exception { 
  boolean bool = false; 
  String directory = path.substring(0, path.lastIndexOf("/") + 1); 
  int start = 0; 
  int end = 0; 
  if (directory.startsWith("/")) { 
   start = 1; 
  } 
  end = directory.indexOf("/", start); 
  while (true) { 
   String subDirectory = directory.substring(start, end); 
   if (!ftpClient.changeWorkingDirectory(subDirectory)) { 
    ftpClient.makeDirectory(subDirectory); 
    ftpClient.changeWorkingDirectory(subDirectory); 
    bool = true; 
   } 
   start = end + 1; 
   end = directory.indexOf("/", start); 
   if (end == -1) { 
    break; 
   } 
  } 
  return bool; 
 } 
 
 // 實現(xiàn)上傳文件的功能 
 public synchronized boolean uploadFile(String localPath, String serverPath) 
   throws Exception { 
  // 上傳文件之前,先判斷本地文件是否存在 
  File localFile = new File(localPath); 
  if (!localFile.exists()) { 
   System.out.println("本地文件不存在"); 
   return false; 
  } 
  System.out.println("本地文件存在,名稱為:" + localFile.getName()); 
  createDirectory(serverPath); // 如果文件夾不存在,創(chuàng)建文件夾 
  System.out.println("服務(wù)器文件存放路徑:" + serverPath + localFile.getName()); 
  String fileName = localFile.getName(); 
  // 如果本地文件存在,服務(wù)器文件也在,上傳文件,這個方法中也包括了斷點上傳 
  long localSize = localFile.length(); // 本地文件的長度 
  FTPFile[] files = ftpClient.listFiles(fileName); 
  long serverSize = 0; 
  if (files.length == 0) { 
   System.out.println("服務(wù)器文件不存在"); 
   serverSize = 0; 
  } else { 
   serverSize = files[0].getSize(); // 服務(wù)器文件的長度 
  } 
  if (localSize <= serverSize) { 
   if (ftpClient.deleteFile(fileName)) { 
    System.out.println("服務(wù)器文件存在,刪除文件,開始重新上傳"); 
    serverSize = 0; 
   } 
  } 
  RandomAccessFile raf = new RandomAccessFile(localFile, "r"); 
  // 進(jìn)度 
  long step = localSize / 100; 
  long process = 0; 
  long currentSize = 0; 
  // 好了,正式開始上傳文件 
  ftpClient.enterLocalPassiveMode(); 
  ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
  ftpClient.setRestartOffset(serverSize); 
  raf.seek(serverSize); 
  OutputStream output = ftpClient.appendFileStream(fileName); 
  byte[] b = new byte[1024]; 
  int length = 0; 
  while ((length = raf.read(b)) != -1) { 
   output.write(b, 0, length); 
   currentSize = currentSize + length; 
   if (currentSize / step != process) { 
    process = currentSize / step; 
    if (process % 10 == 0) { 
     System.out.println("上傳進(jìn)度:" + process); 
    } 
   } 
  } 
  output.flush(); 
  output.close(); 
  raf.close(); 
  if (ftpClient.completePendingCommand()) { 
   System.out.println("文件上傳成功"); 
   return true; 
  } else { 
   System.out.println("文件上傳失敗"); 
   return false; 
  } 
 } 
 
 // 實現(xiàn)下載文件功能,可實現(xiàn)斷點下載 
 public synchronized boolean downloadFile(String localPath, String serverPath) 
   throws Exception { 
  // 先判斷服務(wù)器文件是否存在 
  FTPFile[] files = ftpClient.listFiles(serverPath); 
  if (files.length == 0) { 
   System.out.println("服務(wù)器文件不存在"); 
   return false; 
  } 
  System.out.println("遠(yuǎn)程文件存在,名字為:" + files[0].getName()); 
  localPath = localPath + files[0].getName(); 
  // 接著判斷下載的文件是否能斷點下載 
  long serverSize = files[0].getSize(); // 獲取遠(yuǎn)程文件的長度 
  File localFile = new File(localPath); 
  long localSize = 0; 
  if (localFile.exists()) { 
   localSize = localFile.length(); // 如果本地文件存在,獲取本地文件的長度 
   if (localSize >= serverSize) { 
    System.out.println("文件已經(jīng)下載完了"); 
    File file = new File(localPath); 
    file.delete(); 
    System.out.println("本地文件存在,刪除成功,開始重新下載"); 
    return false; 
   } 
  } 
  // 進(jìn)度 
  long step = serverSize / 100; 
  long process = 0; 
  long currentSize = 0; 
  // 開始準(zhǔn)備下載文件 
  ftpClient.enterLocalActiveMode(); 
  ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
  OutputStream out = new FileOutputStream(localFile, true); 
  ftpClient.setRestartOffset(localSize); 
  InputStream input = ftpClient.retrieveFileStream(serverPath); 
  byte[] b = new byte[1024]; 
  int length = 0; 
  while ((length = input.read(b)) != -1) { 
   out.write(b, 0, length); 
   currentSize = currentSize + length; 
   if (currentSize / step != process) { 
    process = currentSize / step; 
    if (process % 10 == 0) { 
     System.out.println("下載進(jìn)度:" + process); 
    } 
   } 
  } 
  out.flush(); 
  out.close(); 
  input.close(); 
  // 此方法是來確保流處理完畢,如果沒有此方法,可能會造成現(xiàn)程序死掉 
  if (ftpClient.completePendingCommand()) { 
   System.out.println("文件下載成功"); 
   return true; 
  } else { 
   System.out.println("文件下載失敗"); 
   return false; 
  } 
 } 
 
 // 如果ftp上傳打開,就關(guān)閉掉 
 public void closeFTP() throws Exception { 
  if (ftpClient.isConnected()) { 
   ftpClient.disconnect(); 
  } 
 } 
} 

具體實現(xiàn)看代碼注釋寫的很詳細(xì)。

一.Android中FTP文件上傳代碼:

// 上傳例子 
private void ftpUpload() { 
 new Thread() { 
 public void run() { 
  try { 
  System.out.println("正在連接ftp服務(wù)器...."); 
  FTPManager ftpManager = new FTPManager(); 
  if (ftpManager.connect()) { 
  if (ftpManager.uploadFile(ftpManager.rootPath + "UpdateXZMarketPlatform.apk", "mnt/sdcard/")) { 
  ftpManager.closeFTP(); 
  } 
  } 
  } catch (Exception e) { 
  // TODO: handle exception 
  // System.out.println(e.getMessage()); 
  } 
 } 
 }.start(); 
 } 

二.Android中FTP文件下載代碼:

// 下載例子 
private void ftpDownload() { 
 new Thread() { 
 public void run() { 
  try { 
  System.out.println("正在連接ftp服務(wù)器...."); 
  FTPManager ftpManager = new FTPManager(); 
  if (ftpManager.connect()) { 
  if (ftpManager.downloadFile(ftpManager.rootPath, "20120723_XFQ07_XZMarketPlatform.db")) { 
  ftpManager.closeFTP(); 
  } 
  } 
  } catch (Exception e) { 
  // TODO: handle exception 
  // System.out.println(e.getMessage()); 
  } 
 } 
 }.start(); 
 } 

自己之前做項目的時候?qū)戇^的FTP上傳代碼:

package com.kandao.yunbell.videocall; 
 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.net.SocketException; 
 
import org.apache.commons.net.ftp.FTP; 
import org.apache.commons.net.ftp.FTPClient; 
import org.apache.commons.net.ftp.FTPReply; 
 
import com.kandao.yunbell.common.SysApplication; 
 
import android.content.Context; 
import android.util.Log; 
 
public class MyUploadThread extends Thread { 
 private String fileName;// 文件名字 
 private String filePath;// 文件本地路徑 
 private String fileStoragePath;// 文件服務(wù)器存儲路徑 
 private String serverAddress;// 服務(wù)器地址 
 private String ftpUserName;// ftp賬號 
 private String ftpPassword;// ftp密碼 
 private Context mContext; 
 public MyUploadThread() { 
  super(); 
  // TODO Auto-generated constructor stub 
 } 
 
 public MyUploadThread(Context mContext,String fileName, String filePath, 
    String fileStoragePath,String serverAddress,String ftpUserName,String ftpPassword) { 
  super(); 
  this.fileName = fileName; 
  this.filePath = filePath; 
  this.fileStoragePath = fileStoragePath; 
  this.serverAddress = serverAddress; 
  this.ftpUserName = ftpUserName; 
  this.ftpPassword = ftpPassword; 
  this.mContext=mContext; 
 } 
 
 @Override 
 public void run() { 
  super.run(); 
  try { 
   FileInputStream fis=null; 
   FTPClient ftpClient = new FTPClient(); 
   String[] idPort = serverAddress.split(":"); 
   ftpClient.connect(idPort[0], Integer.parseInt(idPort[1])); 
   int returnCode = ftpClient.getReplyCode(); 
   Log.i("caohai", "returnCode,upload:"+returnCode); 
   boolean loginResult = ftpClient.login(ftpUserName, ftpPassword); 
   Log.i("caohai", "loginResult:"+loginResult); 
   if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登錄成功 
     
    // 設(shè)置上傳目錄 
     
    if (((SysApplication) mContext).getIsVideo()) { 
     ((SysApplication) mContext).setIsVideo(false); 
     boolean ff=ftpClient.changeWorkingDirectory(fileStoragePath + "/video/"); 
     Log.i("caohai", "ff:"+ff); 
    }else{ 
    boolean ee=ftpClient.changeWorkingDirectory(fileStoragePath + "/photo/"); 
    Log.i("caohai", "ee:"+ee); 
    } 
    ftpClient.setBufferSize(1024); 
    // ftpClient.setControlEncoding("iso-8859-1"); 
    // ftpClient.enterLocalPassiveMode(); 
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
     fis = new FileInputStream(filePath + "/" 
      + fileName); 
     Log.i("caohai", "fileStoragePath00000:"+fileStoragePath); 
    String[] path = fileStoragePath.split("visitorRecord"); 
     
    boolean fs = ftpClient.storeFile(new String((path[1] 
      + "/photo/" + fileName).getBytes(), "iso-8859-1"), fis); 
    Log.i("caohai", "shifoushangchuanchenggong:"+fs); 
    fis.close(); 
    ftpClient.logout(); 
    //ftpClient.disconnect(); 
   } else {// 如果登錄失敗 
    ftpClient.disconnect(); 
   } 
  } catch (NumberFormatException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } catch (SocketException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } catch (FileNotFoundException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } catch (UnsupportedEncodingException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } catch (IOException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
 
 } 
} 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 自定義Dialog彈框和其背景陰影顯示方法

    自定義Dialog彈框和其背景陰影顯示方法

    今天小編就為大家分享一篇自定義Dialog彈框和其背景陰影顯示方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Android實現(xiàn)京東上滑效果

    Android實現(xiàn)京東上滑效果

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)京東上滑效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Android基于MLKit實現(xiàn)條形碼掃碼的代碼示例

    Android基于MLKit實現(xiàn)條形碼掃碼的代碼示例

    這篇文章將借助開源庫?MLKit?實現(xiàn)條形碼掃描,對于商品條形碼也可以很好地識別成功,該庫的使用內(nèi)容非常豐富,除了條碼識別,還有文字識別、圖像標(biāo)記、人臉檢測等等,本文篇文章就只介紹最基本的條形碼掃描使用,需要的朋友可以參考下
    2023-08-08
  • Android AnalogClock簡單使用方法實例

    Android AnalogClock簡單使用方法實例

    這篇文章主要介紹了Android AnalogClock簡單使用方法,結(jié)合實例形式簡單分析了AnalogClock的布局調(diào)用技巧,需要的朋友可以參考下
    2016-01-01
  • 使用Fragment+ViewPager實現(xiàn)底部導(dǎo)航欄

    使用Fragment+ViewPager實現(xiàn)底部導(dǎo)航欄

    這篇文章主要為大家詳細(xì)介紹了使用Fragment+ViewPager實現(xiàn)底部導(dǎo)航欄,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Android實現(xiàn)圓形ProgressBar停止轉(zhuǎn)動的方法詳解

    Android實現(xiàn)圓形ProgressBar停止轉(zhuǎn)動的方法詳解

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)圓形ProgressBar停止轉(zhuǎn)動方法的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03
  • Android封裝實現(xiàn)短信驗證碼的獲取倒計時

    Android封裝實現(xiàn)短信驗證碼的獲取倒計時

    這篇文章主要介紹了Android封裝實現(xiàn)短信驗證碼的獲取倒計時,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-03-03
  • Android自定義控件繪制基本圖形基礎(chǔ)入門

    Android自定義控件繪制基本圖形基礎(chǔ)入門

    這篇文章主要為大家詳細(xì)介紹了Android自定義控件繪制基本圖形基礎(chǔ)入門資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Android使用ViewPager實現(xiàn)自動輪播

    Android使用ViewPager實現(xiàn)自動輪播

    這篇文章主要介紹了Android使用ViewPager實現(xiàn)自動輪播的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • 深入android Unable to resolve target ''android-XX''詳解

    深入android Unable to resolve target ''android-XX''詳解

    本篇文章是對android Unable to resolve target 'android-XX'錯誤的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06

最新評論