使用FTPClient构造自己的FTP类
来源:互联网 发布:数据库物理设计阶段 编辑:程序博客网 时间:2024/05/29 04:09
使用FTPClient构造自己的FTP类
所有操作需要放在子线程
public class FTPManager { private static final String VIDEO_UPLOAD_SEG_CACHE_FOLDER_PATH = Environment.getExternalStorageDirectory() + "/Cache/seg"; public static final int UPLOAD_SUCCEED = 0; public static final int UPLOAD_FAILED = 1; public static final int DOWNLOAD_SUCCEED = 2; public static final int DOWNLOAD_FAILED = 3; private FTPClient ftpClient; private String ftpServerIp; private int ftpServerPort; //default is 21 private String ftpUserName; private String ftpPassword; public FTPManager() { ftpClient = new FTPClient(); ftpServerIp = "109.201.57.102"; ftpServerPort = 21; ftpUserName = "ftp"; ftpPassword = "123456"; } public FTPManager(String ip, int port, String username, String password) { ftpClient = new FTPClient(); ftpServerIp = ip; ftpServerPort = port; ftpUserName = username; ftpPassword = password; } private boolean uploadFile(String remotePath, String remoteFileName, File file) { boolean succeed = false; try { if (ftpClient.makeDirectory(remotePath)) { ErrRpt.debug("ftp", "ftp 路径创建成功 " + remotePath); } else { ErrRpt.debug("ftp", "ftp 路径创建失败,可能已存在 " + remotePath); } if (ftpClient.changeWorkingDirectory(remotePath)) { ErrRpt.debug("ftp", "ftp 路径切换成功 " + remotePath); } else { ErrRpt.debug("ftp", "ftp 路径切换失败 " + remotePath); } FileInputStream fileInputStream = new FileInputStream(file); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); succeed = ftpClient.storeFile(remoteFileName, bufferedInputStream); bufferedInputStream.close(); fileInputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return succeed; } private void downloadFile(String serverPath) { openConnection(); try { FTPFile[] remoteFiles = ftpClient.listFiles(); File localFile = new File("0", "0.d"); FileOutputStream fileOutputStream = new FileOutputStream(localFile, true); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); ftpClient.setRestartOffset(5000); InputStream inputStream = ftpClient.retrieveFileStream(serverPath); byte[] bytes = new byte[1024]; int length = 0; while ((length = inputStream.read(bytes)) != -1) { bufferedOutputStream.write(bytes, 0, bytes.length); } bufferedOutputStream.flush(); bufferedOutputStream.close(); fileOutputStream.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } closeConnection(); } private void openConnection() { try { ftpClient.setControlEncoding("UTF-8"); ftpClient.connect(ftpServerIp, ftpServerPort); int reply = ftpClient.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) { ErrRpt.debug("ftp", "ftp 连接成功"); } else { ErrRpt.debug("ftp", "ftp 连接失败"); //断开连接 ftpClient.disconnect(); return; } ftpClient.login(ftpUserName, ftpPassword); reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ErrRpt.debug("ftp", "ftp 登陆失败"); //断开连接 ftpClient.disconnect(); return; } else { ErrRpt.debug("ftp", "ftp 登陆成功"); // 获取登录信息 FTPClientConfig ftpClientConfig = new FTPClientConfig(ftpClient.getSystemName().split(" ")[0]); ftpClientConfig.setServerLanguageCode("zh"); ftpClient.configure(ftpClientConfig); // 使用被动模式设为默认 ftpClient.enterLocalPassiveMode(); // 二进制文件支持 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE); } } catch (IOException e) { e.printStackTrace(); } } private void closeConnection() { if (ftpClient != null) { try { ftpClient.logout(); ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } /** * 通过可变长数组传入多个文件或单个文件 */ public class UploadFiles extends AsyncTask<File, Void, Boolean> { String remotePath, remoteFileName; ArrayList<String> all_file_names; Handler handler; boolean needSegFile = true; public UploadFiles(String remotePath, Handler handler) { this.remotePath = remotePath; this.handler = handler; } @Override protected Boolean doInBackground(File... params) { openConnection(); boolean succeed = false; all_file_names = new ArrayList<>(); for (File file : params) { remoteFileName = constructRemoteFileName(file); succeed = uploadFile(remotePath, remoteFileName, file); all_file_names.add(remoteFileName); } return succeed; } @Override protected void onPostExecute(Boolean succeed) { if (succeed) { handler.sendEmptyMessage(UPLOAD_SUCCEED); //视频上传成功后,上传seg文件 //将在SendSeqFile里面上传后closeConnection if (needSegFile) { needSegFile = false; new Thread(new SendSegFile(all_file_names)).start(); } else { closeConnection(); } } else { handler.sendEmptyMessage(UPLOAD_FAILED); closeConnection(); } } } /** * uid_日期_时间.mp4, * 日期格式为:yyyy-mm-dd, 日期格式为:hh:mm:ss, 为24小时格式 * old name: 2016-11-2_15:43:23.mp4 * new name: 65555_2016-11-2_15:43:23.mp4 * @param file * @return */ private String constructRemoteFileName(File file) { String oldName = file.getName(); if (!oldName.contains("_")) { return "filename_not_correct.mp4"; } String year_month_day = oldName.split("_")[0]; String hour_minute_second = oldName.split("_")[1]; //15-43-23.mp4 int uid = User.getUid(); String newName = String.format("uid_%d_%s_%s", uid, year_month_day, hour_minute_second); ErrRpt.debug("ftp", "upload new remote filename: " + newName); return newName; } /** * 在子线程中发送file list的文件 * seg_corp_uid_日期_时间.txt * 日期格式为:yyyy-mm-dd, 日期格式为:hh:mm:ss, 为24小时格式 * * remotePath: 视频文件list路径: /home/用户名/video/logs/日期 */ private class SendSegFile implements Runnable { private ArrayList<String> contents; public SendSegFile(ArrayList<String> contents) { if (contents == null) { this.contents = new ArrayList<>(); } else { this.contents = contents; } } @Override public void run() { int uid = User.getUid(); Date now = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss"); String nowTime = simpleDateFormat.format(now); String fileName = String.format("seg_%d_%s.txt", uid, nowTime); File dir = new File(VIDEO_UPLOAD_SEG_CACHE_FOLDER_PATH); if (!dir.exists()) { dir.mkdirs(); } File file = new File(dir, fileName); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } String content = ""; for (String s : contents) { content += (s + "\n"); } try { FileOutputStream fos = new FileOutputStream(file); PrintWriter pw = new PrintWriter(fos); pw.write(content); pw.flush(); pw.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String remotePath = "/home/" + ftpUserName + "/video/logs/" + nowTime.split("_")[0]; boolean succeed = uploadFile(remotePath, fileName, file); if (succeed) { ErrRpt.debug("ftp", "upload seg file succeed. : " + fileName); } else { ErrRpt.debug("ftp", "upload seg file failed. : " + fileName); } closeConnection(); } } //public class DownloadSingleFile extends AsyncTask}
0 0
- 使用FTPClient构造自己的FTP类
- commons-ftp中ftpClient类的API(构造方法)
- 使用Sun的FtpClient做FTP上传下载
- FTP文件上传------FTPClient的使用
- JAVA使用FTPClient类读写FTP
- 使用FtpClient类实现FTP文件上传下载
- JAVA使用FTPClient类读写FTP
- JAVA使用FTPClient类读写FTP
- JAVA使用FTPClient类读写FTP
- JAVA使用FTPClient类读写FTP
- FTPClient的使用类
- ftp中ftpClient类的API--
- commons-ftp中ftpClient类的API
- commons-ftp中ftpClient类的API
- 使用FtpClient连接FTP服务器
- 使用apache的FTPClient修改FTP服务器上的文件名称
- FTP读取远程文件&解决使用FTPClient类时线程挂起的问题
- JAVA中使用FTPClient实现FTP上文件的操作
- Linux 2.6.38 User-space interface for Crypto API
- unity VR Unet 多人互联的实现 以及unet组件的的坑
- a series of Fourier
- 总结和分析几种判断RecyclerView到达底部的方法
- shell脚本 set 选项
- 使用FTPClient构造自己的FTP类
- 将SVG 图引入到HTML页面
- listview 多布局 根据type加载
- Flask + Gunicorn + Nginx 部署
- Qt分页布局与切分窗口的实现
- JZOJ4857. 【GDOI2017模拟11.4】Tourist Attractions
- 去掉jsp页面在myeclipse上显示
- Hibernate实现dao自定义sql查询结果
- leetcode--remove_duplicates_from_sorted_list