FTP上传下载删除
来源:互联网 发布:淘宝宠物狗 编辑:程序博客网 时间:2024/06/05 17:26
导入包:commons-net-xx.jar
import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import org.apache.commons.net.ftp.FTP;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPClientConfig;import org.apache.commons.net.ftp.FTPFile;import org.apache.commons.net.ftp.FTPReply;public class FtpUtil { private static String CONTROL_ENCODING = "GBK"; private static int DATE_TIMEOUT = 60000;//设置传输超时时间为60秒 private static int CONNECT_TIMEOUT = 460000;//设置链接超时时间为60秒 private static FTPClient ftp; //登陆服务 public static void loginFTPService(String host, int port, String username, String password){ ftp = new FTPClient(); try { int reply = 0; ftp.setDataTimeout(DATE_TIMEOUT); ftp.setConnectTimeout(CONNECT_TIMEOUT); ftp.connect(host, port); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); } ftp.enterLocalPassiveMode(); FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT); conf.setServerLanguageCode("zh"); ftp.setControlEncoding(CONTROL_ENCODING); ftp.setFileType(FTP.ASCII_FILE_TYPE); ftp.setFileType(FTP.BINARY_FILE_TYPE); } catch (Exception e) { e.printStackTrace(); } } //上传文件 public static void uploadFile( String basePath,String filePath, String filename, InputStream input) { try { //切换到上传目录 if (!ftp.changeWorkingDirectory(basePath+filePath)) { //如果目录不存在创建目录 String[] dirs = filePath.split("/"); String tempPath = basePath; for (String dir : dirs) { if (null == dir || "".equals(dir)) continue; tempPath += "/" + dir; if (!ftp.changeWorkingDirectory(tempPath)) { if (!ftp.makeDirectory(tempPath)) { } else { ftp.changeWorkingDirectory(tempPath); } } } } //上传文件 ftp.storeFile(new String(filename.getBytes("GBK"),"ISO-8859-1"), input); input.close(); ftp.logout(); } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } } //下载文件 public static void downloadFile(String remotePath,String fileName, String localPath) { try { ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录 FTPFile[] fs = ftp.listFiles(); for (FTPFile ff : fs) { if (ff.getName().equals(fileName)) { File localFile = new File(localPath + "/" + ff.getName()); OutputStream is = new FileOutputStream(localFile); ftp.retrieveFile(ff.getName(), is); is.close(); } } } catch (IOException e) { e.printStackTrace(); } } //删除文件 public static void deleteFtpFile( String remotePath,String fileName) { try { // 转移到FTP服务器目录 ftp.deleteFile(remotePath + "/" + fileName); } catch (IOException e) { e.printStackTrace(); } } //删除文件目录 public static void deleteFTPdir(String pathName) throws Exception { ftp.enterLocalPassiveMode(); FTPFile[] files = ftp.listFiles(pathName); if (null != files && files.length > 0) { for (FTPFile file : files) { if (file.isDirectory()) { deleteFTPdir(pathName +"/"+ file.getName()); ftp.changeWorkingDirectory(pathName.substring(0,pathName.lastIndexOf("/"))); ftp.removeDirectory(pathName); }else{ ftp.deleteFile(pathName+"/"+file.getName()); } } } ftp.changeWorkingDirectory(pathName.substring(0,pathName.lastIndexOf("/"))); ftp.removeDirectory(pathName); } //退出ftp服务 public static void logoutFTPService() throws IOException{ if(ftp != null){ ftp.logout(); } }}
阅读全文
0 0
- FTP上传下载删除
- FTP上传下载删除
- JAVA FTP文件上传下载删除
- java ftp上传下载(包含删除操作)
- ftp上传下载
- FTP上传下载
- FTP上传下载
- FTP上传下载
- 上传下载ftp
- ftp上传下载
- FTP上传下载
- Ftp上传下载
- FTP上传下载
- ftp上传下载
- 实现 ftp 文件/文件夹的上传下载以及删除
- FTP文件上传下载删除_解决乱码.doc
- java操作FTP,实现文件上传下载删除操作
- C# FTP文件上传下载
- 电影热搜API及微信菜单自定义开发
- Android解决使用广播监听网络状态变化时接收到多个广播的问题
- Worley Noise (三)
- fousrce
- 207. Course Schedule
- FTP上传下载删除
- 实验楼 Linux 基础入门 用户及文件权限管理 作业笔记
- JAVA中字符串匹配indexof()的用法
- 零基础之位图(理解计算机图像用)
- nginx反向代理多个tomcat
- arm交叉编译器gnueabi、none-eabi、arm-eabi、gnueabihf、gnueabi区别
- DAY10
- 关键字static的作用
- 在同一个类中调用另一个方法没有触发 Spring AOP 的问题【转】