愛碼士_yan 2021-04-22 我要評(píng)論 1.前言 在開發(fā)中,會(huì)遇到向FTP服務(wù)器上傳文件的需求,首先要導(dǎo)入 Ftp相關(guān)代碼 import android.util.Log; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import java.io.FileInputStream; public class FTPClientUtils { private static final String TAG = "MainActivity"; private FTPClient ftpClient = null; // FTP客戶端 /** * 連接到FTP服務(wù)器 * * @param host ftp服務(wù)器域名 * @param username 訪問用戶名 * @param password 訪問密碼 * @param port 端口 * @return 是否連接成功 */ public boolean ftpConnect(String host, String username, String password, int port) { try { ftpClient = new FTPClient(); ftpClient.connect(host,port); // 根據(jù)返回的狀態(tài)碼,,判斷鏈接是否建立成功 if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { boolean status = ftpClient.login(username, password); /* * 設(shè)置文件傳輸模式 * 避免一些可能會(huì)出現(xiàn)的問題,在這里必須要設(shè)定文件的傳輸格式,。 * 在這里我們使用BINARY_FILE_TYPE來傳輸文本,、圖像和壓縮文件。 */ ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); return status; } } catch (Exception e) { e.printStackTrace(); } return false; } /** * 斷開ftp服務(wù)器連接 * * @return 斷開結(jié)果 */ public boolean ftpDisconnect() { // 判斷空指針 if (ftpClient == null) { return true; } // 斷開ftp服務(wù)器連接 try { ftpClient.logout(); ftpClient.disconnect(); return true; } catch (Exception e) { e.printStackTrace(); } return false; } /** * ftp 文件上傳 * * @param srcFilePath 源文件目錄 * @param desFileName 文件名稱 * @return 文件上傳結(jié)果 */ public boolean ftpUpload(String srcFilePath, String desFileName) { boolean status = false; try { FileInputStream srcFileStream = new FileInputStream(srcFilePath); status = ftpClient.storeFile(desFileName, srcFileStream); srcFileStream.close(); return status; } catch (Exception e) { e.printStackTrace(); } return status; } /** * ftp 更改目錄 * * @param path 更改的路徑 * @return 更改是否成功 */ public boolean ftpChangePath(String path) { boolean status = false; try { status = ftpClient.changeWorkingDirectory(path); } catch (Exception e) { e.printStackTrace(); } return status; } } 2.調(diào)用api boolean isConnect = mFtpClient.ftpConnect("服務(wù)器host", "用戶名", "密碼", 21);//默認(rèn)端口號(hào)是21 if (isConnect) { boolean isSuccessful = mFtpClient.ftpUpload("/sdcard/" + folderName + "/" + mPicturename, "/htdocs/pics/" + mPicturename); if (isSuccessful) { mFtpClient.ftpDisconnect(); //上傳成功 } else { //上傳失敗 } } else { //服務(wù)器連接失敗 } 附錄:自己之前做項(xiàng)目的時(shí)候?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ù)器存儲(chǔ)路徑 private String serverAddress;// 服務(wù)器地址 private String ftpUserName;// ftp賬號(hào) 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(); } } } |
|