- 浏览: 7255257 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (1546)
- 企业中间件 (236)
- 企业应用面临的问题 (236)
- 小布Oracle学习笔记汇总 (36)
- Spring 开发应用 (54)
- IBatis开发应用 (16)
- Oracle基础学习 (23)
- struts2.0 (41)
- JVM&ClassLoader&GC (16)
- JQuery的开发应用 (17)
- WebService的开发应用 (21)
- Java&Socket (44)
- 开源组件的应用 (254)
- 常用Javascript的开发应用 (28)
- J2EE开发技术指南 (163)
- EJB3开发应用 (11)
- GIS&Mobile&MAP (36)
- SWT-GEF-RCP (52)
- 算法&数据结构 (6)
- Apache开源组件研究 (62)
- Hibernate 学习应用 (57)
- java并发编程 (59)
- MySQL&Mongodb&MS/SQL (15)
- Oracle数据库实验室 (55)
- 搜索引擎的开发应用 (34)
- 软件工程师笔试经典 (14)
- 其他杂项 (10)
- AndroidPn& MQTT&C2DM&推技术 (29)
- ActiveMQ学习和研究 (38)
- Google技术应用开发和API分析 (11)
- flex的学习总结 (59)
- 项目中一点总结 (20)
- java疑惑 java面向对象编程 (28)
- Android 开发学习 (133)
- linux和UNIX的总结 (37)
- Titanium学习总结 (20)
- JQueryMobile学习总结 (34)
- Phonegap学习总结 (32)
- HTML5学习总结 (41)
- JeeCMS研究和理解分析 (9)
最新评论
-
lgh1992314:
[u][i][b][flash=200,200][url][i ...
看看mybatis 源代码 -
尼古拉斯.fwp:
图片根本就不出来好吧。。。。。。
Android文件图片上传的详细讲解(一)HTTP multipart/form-data 上传报文格式实现手机端上传 -
ln94223:
第一个应该用排它网关吧 怎么是并行网关, 并行网关是所有exe ...
工作流Activiti的学习总结(八)Activiti自动执行的应用 -
ZY199266:
获取不到任何消息信息,请问这是什么原因呢?
ActiveMQ 通过JMX监控Connection,Queue,Topic的信息 -
xiaoyao霄:
DestinationSourceMonitor 报错 应该导 ...
ActiveMQ 通过JMX监控Connection,Queue,Topic的信息
在项目中使用到FTP功能,于是采用类似Spring的各种回调机制公用各种代码,减少代码的开发量,简化开发人员的学习难度.本文仅供学习交流使用,如有比较好的意见希望可以一起交流,再次表示感谢.
package easyway.tbs.transfer.ftp; import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; /** * FTPCLient回调 * @author longgangabai * * @param <T> */ public interface FTPClientCallback<T> { public T doTransfer(FTPClient ftp)throws IOException; }
package easyway.tbs.transfer.ftp; import java.io.IOException; import java.util.Collection; import org.apache.commons.net.ftp.FTPFile; /** * FTP操作的客户端操作上传下载 * @author longgangbai * */ public interface FTPClientOperations { /** * 文件上传的方法 * @param remote * @param local * @return * @throws IOException */ public UploadStatus upload(String remote, String local) throws IOException; /** * * 从远程服务器目录下载文件到本地服务器目录中 * @param localdir FTP服务器保存目录 * @param remotedir FTP下载服务器目录 * @param localTempFile临时下载记录文件 * @return 成功返回true,否则返回false */ public Collection<String> downloadList(final String localdir, final String remotedir,final String localTmpFile) throws IOException; /** * 文件下载的方法 * @param remote * @param local * @return * @throws IOException */ public DownloadStatus downloadFile( String local,String remote) throws IOException; /** * 查看服务器上文件列表方法 * @param remotedir * @return * @throws IOException */ public FTPFile[] list(final String remotedir) throws IOException; }
package easyway.tbs.transfer.ftp; import java.io.File; import java.io.IOException; import java.net.ConnectException; import java.util.ArrayList; import java.util.Collection; import java.util.Properties; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.math.NumberUtils; 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; import org.apache.log4j.Logger; import easyway.tbs.commons.FileOperateUtils; /** * Ftp回调模板 * * @author longgangbai * */ public class FTPClientTemplate implements FTPClientOperations{ private static final Logger logger = Logger.getLogger(FTPClientTemplate.class); private static String DEAFULT_REMOTE_CHARSET="UTF-8"; private static int DEAFULT_REMOTE_PORT=21; private static String separator = File.separator; private FTPClientConfig ftpClientConfig; private String host; private String username; private String password; private String port; public FTPClientTemplate(String host,String user,String pwd,String port){ this.host=host; this.username=user; this.password=pwd; this.port=port; } /** * 查看服务器上文件列表方法 * @param remotedir * @return * @throws IOException */ public FTPFile[] list(final String remotedir) throws IOException{ return execute(new FTPClientCallback<FTPFile[]>(){ public FTPFile[] doTransfer(FTPClient ftp) throws IOException { ftp.changeWorkingDirectory(remotedir); FTPFile[] files= ftp.listFiles(remotedir); return files; } }); } /** * 文件上传的方法 * @param remote * @param local * @return * @throws IOException */ public UploadStatus upload(final String local,final String remote) throws IOException{ return execute(new FTPClientCallback<UploadStatus>(){ public UploadStatus doTransfer(FTPClient ftpClient)throws IOException { return FtpHelper.getInstance().upload(ftpClient, local, remote); } }); } /** * 上传文件到服务器,新上传和断点续传 * @param remoteFile 远程文件名,在上传之前已经将服务器工作目录做了改变 * @param localFile 本地文件File句柄,绝对路径 * @param processStep 需要显示的处理进度步进值 * @param ftpClient FTPClient引用 * @return * @throws IOException */ public UploadStatus uploadFile(String remoteFile, File localFile, FTPClient ftpClient, long remoteSize) throws IOException { return FtpHelper.getInstance().uploadFile(remoteFile, localFile, ftpClient, remoteSize); } /** * 从远程服务器目录下载文件到本地服务器目录中 * @param localdir FTP服务器保存目录 * @param remotedir FTP下载服务器目录 * @param localTempFile临时下载记录文件 * @return 成功下载记录 */ public Collection<String> downloadList(final String localdir, final String remotedir,final String localTmpFile) throws IOException { return execute(new FTPClientCallback<Collection<String>>(){ public Collection<String> doTransfer(final FTPClient ftp) throws IOException { //切换到下载目录的中 ftp.changeWorkingDirectory(remotedir); //获取目录中所有的文件信息 FTPFile[] ftpfiles=ftp.listFiles(); Collection<String> fileNamesCol=new ArrayList<String>(); //判断文件目录是否为空 if(!ArrayUtils.isEmpty(ftpfiles)){ for (FTPFile ftpfile : ftpfiles) { String remoteFilePath=remotedir+separator+ftpfile.getName(); String localFilePath=localdir+separator+ftpfile.getName(); System.out.println("remoteFilePath ="+remoteFilePath +" localFilePath="+localFilePath); //单个文件下载状态 DownloadStatus downStatus=downloadFile(remoteFilePath, localFilePath); if(downStatus==DownloadStatus.Download_New_Success){ //临时目录中添加记录信息 fileNamesCol.add(remoteFilePath); } } } if(CollectionUtils.isNotEmpty(fileNamesCol)){ FileOperateUtils.writeLinesToFile(fileNamesCol, localTmpFile); } return fileNamesCol; } }); } /** * 从FTP服务器上下载文件,支持断点续传,上传百分比汇报 * @param remote 远程文件路径 * @param local 本地文件路径 * @return 上传的状态 * @throws IOException * */ public DownloadStatus downloadFile(final String remote, final String local) throws IOException{ return execute(new FTPClientCallback<DownloadStatus>(){ public DownloadStatus doTransfer(FTPClient ftpClient) throws IOException { DownloadStatus result=FtpHelper.getInstance().download(ftpClient, remote, local); return result; } }); } /** * 执行FTP回调操作的方法 * @param callback 回调的函数 * @throws IOException */ public <T> T execute(FTPClientCallback<T> callback) throws IOException{ FTPClient ftp=new FTPClient(); try { /*if(getFtpClientConfig()!=null){ ftp.configure(getFtpClientConfig()); ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID()); }*/ //登录FTP服务器 try { //设置超时时间 ftp.setDataTimeout(7200); //设置默认编码 ftp.setControlEncoding(DEAFULT_REMOTE_CHARSET); //设置默认端口 ftp.setDefaultPort(DEAFULT_REMOTE_PORT); //设置是否显示隐藏文件 ftp.setListHiddenFiles(false); //连接ftp服务器 if(StringUtils.isNotEmpty(port)&&NumberUtils.isDigits(port)){ ftp.connect(host, Integer.valueOf(port)); }else{ ftp.connect(host); } } catch(ConnectException e) { logger.error("连接FTP服务器失败:"+ftp.getReplyString()+ftp.getReplyCode()); throw new IOException("Problem connecting the FTP-server fail",e); } //得到连接的返回编码 int reply=ftp.getReplyCode(); if(!FTPReply.isPositiveCompletion(reply)){ ftp.disconnect(); } //登录失败权限验证失败 if(!ftp.login(getUsername(), getPassword())) { ftp.quit(); ftp.disconnect(); logger.error("连接FTP服务器用户或者密码失败::"+ftp.getReplyString()); throw new IOException("Cant Authentificate to FTP-Server"); } if(logger.isDebugEnabled()){ logger.info("成功登录FTP服务器:"+host+" 端口:"+port); } ftp.setFileType(FTPClient.BINARY_FILE_TYPE); //回调FTP的操作 return callback.doTransfer(ftp); }finally{ //FTP退出 ftp.logout(); //断开FTP连接 if(ftp.isConnected()){ ftp.disconnect(); } } } protected String resolveFile(String file) { return null; //return file.replace(System.getProperty("file.separator").charAt(0), this.remoteFileSep.charAt(0)); } /** * 获取FTP的配置操作系统 * @return */ public FTPClientConfig getFtpClientConfig() { //获得系统属性集 Properties props=System.getProperties(); //操作系统名称 String osname=props.getProperty("os.name"); //针对window系统 if(osname.equalsIgnoreCase("Windows XP")){ ftpClientConfig=new FTPClientConfig(FTPClientConfig.SYST_NT); //针对linux系统 }else if(osname.equalsIgnoreCase("Linux")){ ftpClientConfig=new FTPClientConfig(FTPClientConfig.SYST_UNIX); } if(logger.isDebugEnabled()){ logger.info("the ftp client system os Name "+osname); } return ftpClientConfig; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
package easyway.tbs.transfer.ftp; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.RandomAccessFile; import java.util.StringTokenizer; 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; /** * FTP下载工具类 * @author longgangbai * */ public class FtpHelper { private static String DEAFULT_REMOTE_CHARSET="GBK"; private static String DEAFULT_LOCAL_CHARSET="iso-8859-1"; private static FtpHelper instance=new FtpHelper(); public static FtpHelper getInstance(){ return instance; } /** * 连接到FTP服务器 * @param hostname主机名 * @param port 端口 * @param username 用户名 * @param password 密码 * @return 是否连接成功 * @throws IOException */ public boolean connect(FTPClient ftpClient,String hostname, int port, String username,String password) throws IOException { ftpClient.connect(hostname, port); ftpClient.setControlEncoding(DEAFULT_REMOTE_CHARSET); if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { if (ftpClient.login(username, password)) { return true; } } disconnect(ftpClient); return false; } /** * 从FTP服务器上下载文件,支持断点续传,上传百分比汇报 * @param remoteFilePath 远程文件路径 * @param localFilePath 本地文件路径 * @return 上传的状态 * @throws IOException */ public DownloadStatus download(FTPClient ftpClient,String remoteFilePath, String localFilePath) throws IOException { // 设置被动模式 ftpClient.enterLocalPassiveMode(); // 设置以二进制方式传输 ftpClient.setFileType(FTP.BINARY_FILE_TYPE); DownloadStatus result; // 检查远程文件是否存在 FTPFile[] files = ftpClient.listFiles(new String( remoteFilePath.getBytes(DEAFULT_REMOTE_CHARSET), DEAFULT_LOCAL_CHARSET)); if (files.length != 1) { System.out.println("远程文件不存在"); return DownloadStatus.Remote_File_Noexist; } long lRemoteSize = files[0].getSize(); File f = new File(localFilePath); // 本地存在文件,进行断点下载 if (f.exists()) { long localSize = f.length(); // 判断本地文件大小是否大于远程文件大小 if (localSize >= lRemoteSize) { System.out.println("本地文件大于远程文件,下载中止"); return DownloadStatus.Local_Bigger_Remote; } // 进行断点续传,并记录状态 FileOutputStream out = new FileOutputStream(f, true); ftpClient.setRestartOffset(localSize); InputStream in = ftpClient.retrieveFileStream(new String(remoteFilePath .getBytes(DEAFULT_REMOTE_CHARSET), DEAFULT_LOCAL_CHARSET)); byte[] bytes = new byte[1024]; long step = lRemoteSize / 100; long process = localSize / step; int c; while ((c = in.read(bytes)) != -1) { out.write(bytes, 0, c); localSize += c; long nowProcess = localSize / step; if (nowProcess > process) { process = nowProcess; if (process % 10 == 0){ System.out.println("下载进度:" + process); } // TODO 更新文件下载进度,值存放在process变量中 } } in.close(); out.close(); boolean isDo = ftpClient.completePendingCommand(); if (isDo) { result = DownloadStatus.Download_From_Break_Success; } else { result = DownloadStatus.Download_From_Break_Failed; } } else { OutputStream out = new FileOutputStream(f); InputStream in = ftpClient.retrieveFileStream(new String(remoteFilePath .getBytes(DEAFULT_REMOTE_CHARSET), DEAFULT_LOCAL_CHARSET)); byte[] bytes = new byte[1024]; long step = lRemoteSize / 100; long process = 0; long localSize = 0L; int c; while ((c = in.read(bytes)) != -1) { out.write(bytes, 0, c); localSize += c; long nowProcess = localSize / step; if (nowProcess > process) { process = nowProcess; if (process % 10 == 0){ System.out.println("下载进度:" + process); } // TODO 更新文件下载进度,值存放在process变量中 } } in.close(); out.close(); boolean upNewStatus = ftpClient.completePendingCommand(); if (upNewStatus) { result = DownloadStatus.Download_New_Success; } else { result = DownloadStatus.Download_New_Failed; } } return result; } /** * * 上传文件到FTP服务器,支持断点续传 * @param localFilePath 本地文件名称,绝对路径 * @param remoteFilePath 远程文件路径,使用/home/directory1/subdirectory/file.ext * 按照Linux上的路径指定方式,支持多级目录嵌套,支持递归创建不存在的目录结构 * @return 上传结果 * @throws IOException */ public UploadStatus upload(FTPClient ftpClient,String localFilePath, String remoteFilePath) throws IOException { // 设置PassiveMode传输 ftpClient.enterLocalPassiveMode(); // 设置以二进制流的方式传输 ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.setControlEncoding(DEAFULT_REMOTE_CHARSET); UploadStatus result; // 对远程目录的处理 String remoteFileName = remoteFilePath; if (remoteFilePath.contains("/")) { remoteFileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/") + 1); // 创建服务器远程目录结构,创建失败直接返回 if (createDirecroty(remoteFilePath, ftpClient) == UploadStatus.Create_Directory_Fail) { return UploadStatus.Create_Directory_Fail; } } // ftpClient.feat(); // System.out.println( ftpClient.getReply()); // System.out.println( ftpClient.acct(null)); // System.out.println(ftpClient.getReplyCode()); // System.out.println(ftpClient.getReplyString()); // 检查远程是否存在文件 FTPFile[] files = ftpClient.listFiles(new String(remoteFileName .getBytes(DEAFULT_REMOTE_CHARSET), DEAFULT_LOCAL_CHARSET)); if (files.length == 1) { long remoteSize = files[0].getSize(); File f = new File(localFilePath); long localSize = f.length(); if (remoteSize == localSize) { // 文件存在 return UploadStatus.File_Exits; } else if (remoteSize > localSize) { return UploadStatus.Remote_Bigger_Local; } // 尝试移动文件内读取指针,实现断点续传 result = uploadFile(remoteFileName, f, ftpClient, remoteSize); // 如果断点续传没有成功,则删除服务器上文件,重新上传 if (result == UploadStatus.Upload_From_Break_Failed) { if (!ftpClient.deleteFile(remoteFileName)) { return UploadStatus.Delete_Remote_Faild; } result = uploadFile(remoteFileName, f, ftpClient, 0); } } else { result = uploadFile(remoteFileName, new File(localFilePath), ftpClient, 0); } return result; } /** * * 断开与远程服务器的连接 * @throws IOException */ public void disconnect(FTPClient ftpClient) throws IOException { if (ftpClient.isConnected()) { ftpClient.disconnect(); } } /** * 递归创建远程服务器目录 * @param remote 远程服务器文件绝对路径 * @param ftpClient FTPClient对象 * @return 目录创建是否成功 * @throws IOException */ public UploadStatus createDirecroty(String remote, FTPClient ftpClient) throws IOException { UploadStatus status = UploadStatus.Create_Directory_Success; String directory = remote.substring(0, remote.lastIndexOf("/") + 1); if (!directory.equalsIgnoreCase("/") && !ftpClient.changeWorkingDirectory(new String(directory .getBytes(DEAFULT_REMOTE_CHARSET), DEAFULT_LOCAL_CHARSET))) { // 如果远程目录不存在,则递归创建远程服务器目录 int start = 0; int end = 0; if (directory.startsWith("/")) { start = 1; } else { start = 0; } end = directory.indexOf("/", start); while (true) { String subDirectory = new String(remote.substring(start, end) .getBytes(DEAFULT_REMOTE_CHARSET), DEAFULT_LOCAL_CHARSET); if (!ftpClient.changeWorkingDirectory(subDirectory)) { if (ftpClient.makeDirectory(subDirectory)) { ftpClient.changeWorkingDirectory(subDirectory); } else { System.out.println("创建目录失败"); return UploadStatus.Create_Directory_Fail; } } start = end + 1; end = directory.indexOf("/", start); // 检查所有目录是否创建完毕 if (end <= start) { break; } } } return status; } /** * 上传文件到服务器,新上传和断点续传 * @param remoteFile 远程文件名,在上传之前已经将服务器工作目录做了改变 * @param localFile 本地文件File句柄,绝对路径 * @param processStep 需要显示的处理进度步进值 * @param ftpClient FTPClient引用 * @return * @throws IOException */ public UploadStatus uploadFile(String remoteFile, File localFile, FTPClient ftpClient, long remoteSize) throws IOException { UploadStatus status; // 显示进度的上传 long step = localFile.length() / 100; long process = 0; long localreadbytes = 0L; RandomAccessFile raf = new RandomAccessFile(localFile, "r"); String remote = new String(remoteFile.getBytes(DEAFULT_REMOTE_CHARSET), DEAFULT_LOCAL_CHARSET); OutputStream out = ftpClient.appendFileStream(remote); if (out == null) { String message = ftpClient.getReplyString(); throw new RuntimeException(message); } // 断点续传 if (remoteSize > 0) { ftpClient.setRestartOffset(remoteSize); process = remoteSize / step; raf.seek(remoteSize); localreadbytes = remoteSize; } byte[] bytes = new byte[1024]; int c; while ((c = raf.read(bytes)) != -1) { out.write(bytes, 0, c); localreadbytes += c; if (localreadbytes / step != process) { process = localreadbytes / step; System.out.println("上传进度:" + process); // TODO 汇报上传状态 } } out.flush(); raf.close(); out.close(); boolean result = ftpClient.completePendingCommand(); if (remoteSize > 0) { status = result ? UploadStatus.Upload_From_Break_Success : UploadStatus.Upload_From_Break_Failed; } else { status = result ? UploadStatus.Upload_New_File_Success : UploadStatus.Upload_New_File_Failed; } return status; } protected void makeRemoteDir(FTPClient ftp, String dir) throws IOException { String workingDirectory = ftp.printWorkingDirectory(); if (dir.indexOf("/") == 0) { ftp.changeWorkingDirectory("/"); } String subdir = new String(); StringTokenizer st = new StringTokenizer(dir, "/"); while (st.hasMoreTokens()) { subdir = st.nextToken(); if (!(ftp.changeWorkingDirectory(subdir))) { if (!(ftp.makeDirectory(subdir))) { int rc = ftp.getReplyCode(); if (((rc != 550) && (rc != 553) && (rc != 521))) { throw new IOException("could not create directory: " + ftp.getReplyString()); } } else { ftp.changeWorkingDirectory(subdir); } } } if (workingDirectory != null){ ftp.changeWorkingDirectory(workingDirectory); } } /** * 获取指定目录下的文件名称列表 * @param currentDir * 需要获取其子目录的当前目录名称 * @return 返回子目录字符串数组 */ public String[] GetFileNames(FTPClient ftpClient,String currentDir) { String[] dirs = null; try { if (currentDir == null) dirs = ftpClient.listNames(); else dirs = ftpClient.listNames(currentDir); } catch (IOException e) { e.printStackTrace(); } return dirs; } /** * 获取指定目录下的文件与目录信息集合 * @param currentDir * 指定的当前目录 * @return 返回的文件集合 */ public FTPFile[] GetDirAndFilesInfo(FTPClient ftpClient,String currentDir) { FTPFile[] files = null; try { if (currentDir == null) files = ftpClient.listFiles(); else files = ftpClient.listFiles(currentDir); } catch (IOException ex) { ex.printStackTrace(); } return files; } }
package easyway.tbs.transfer.ftp; import easyway.tbs.commons.EnumUtils; /** * 文件下载状态 * @author longgangbai * */ public enum DownloadStatus { Remote_File_Noexist(0,"远程文件不存在"), // 远程文件不存在 //用于单个下载 Download_New_Success(1,"下载文件成功"), // 下载文件成功 Download_New_Failed(2,"下载文件失败"), // 下载文件失败 Local_Bigger_Remote(3,"本地文件大于远程文件"), // 本地文件大于远程文件 Download_From_Break_Success(4,"文件断点续传成功"), // 断点续传成功 Download_From_Break_Failed(5,"文件断点续传失败"), // 断点续传失败 //用于批量下载 Download_Batch_Success(6,"文件批量下载成功"), Download_Batch_Failure(7,"文件批量下载失败"), Download_Batch_Failure_SUCCESS(8,"文件批量下载不完全成功"); private int code; private String description; private DownloadStatus(int code , String description) { this.code = code; this.description = description; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } /** * 下载状态中中使用的code * @param code * @return */ public static DownloadStatus fromCode(int code) { return EnumUtils.fromEnumProperty(DownloadStatus.class, "code", code); } }
package easyway.tbs.transfer.ftp; import easyway.tbs.commons.EnumUtils; /** * 文件上传状态 * @author longgangbai * */ public enum UploadStatus { Create_Directory_Fail(0,"远程服务器相应目录创建失败"), // 远程服务器相应目录创建失败 Create_Directory_Success(1,"远程服务器闯将目录成功"), // 远程服务器闯将目录成功 Upload_New_File_Success(2,"上传新文件成功"), // 上传新文件成功 Upload_New_File_Failed(3,"上传新文件失败"), // 上传新文件失败 File_Exits(4,"文件已经存在"), // 文件已经存在 Remote_Bigger_Local(5,"远程文件大于本地文件"), // 远程文件大于本地文件 Upload_From_Break_Success(6," 断点续传成功"), // 断点续传成功 Upload_From_Break_Failed(7,"断点续传失败"), // 断点续传失败 Delete_Remote_Faild(8,"删除远程文件失败"); // 删除远程文件失败 private int code; private String description; private UploadStatus(int code , String description) { this.code = code; this.description = description; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } /** * 下载状态中中使用的code * @param code * @return */ public static UploadStatus fromCode(int code) { return EnumUtils.fromEnumProperty(UploadStatus.class, "code", code); } }
package easyway.tbs.commons; import org.apache.commons.lang.ObjectUtils; import org.apache.log4j.Logger; /** * 枚举的工具类 * @author longgangbai * */ public abstract class EnumUtils { private static final Logger logger = Logger.getLogger(EnumUtils.class); /** * 从指定的枚举类中根据property搜寻匹配指定值的枚举实例 * @param <T> * @param enumClass * @param property * @param propValue * @return */ public static <T extends Enum<T>> T fromEnumProperty(Class<T> enumClass, String property, Object propValue) { T[] enumConstants = enumClass.getEnumConstants(); for (T t : enumConstants) { Object constantPropValue; try { constantPropValue = BeanUtils.getDeclaredFieldValue(t, property); if (ObjectUtils.equals(constantPropValue, propValue)) { return t; } } catch (Exception e) { logger.error(e.getMessage()); throw new RuntimeException(e); } } return null; } /** * 从指定的枚举类中根据名称匹配指定值 * @param <T> * @param enumClass * @param constantName * @return */ public static <T extends Enum<T>> T fromEnumConstantName(Class<T> enumClass, String constantName) { T[] enumConstants = enumClass.getEnumConstants(); for (T t : enumConstants) { if (((Enum<?>) t).name().equals(constantName)) { return t; } } return null; } }
package easyway.tbs.commons; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Collection; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOCase; import org.apache.commons.io.IOUtils; import org.apache.commons.io.filefilter.FalseFileFilter; import org.apache.commons.io.filefilter.FileFilterUtils; import org.apache.commons.io.filefilter.IOFileFilter; import org.apache.log4j.Logger; /** * 文件操作工具类 * * @author longgangbai * */ public class FileOperateUtils { private static final Logger logger = Logger.getLogger(FileOperateUtils.class); /** * 查找需要合并文件的临时文件信息 * * @param directory * 临时文件所在的目录 * @param prefix * 临时文件的前缀 * @param caseSensitivity * 临时文件的大小写敏感 * @return */ public static Collection<File> searchPrefixFile(File directory,String prefix,boolean caseSensitivity){ IOCase iocase=IOCase.INSENSITIVE; if(caseSensitivity){ iocase=IOCase.SENSITIVE; } //创建相关的过滤器 IOFileFilter fileFilter=FileFilterUtils.prefixFileFilter(prefix, iocase); //检查相关的过滤信息 return FileUtils.listFiles(directory, fileFilter, FalseFileFilter.INSTANCE); } /** * 查找目录下特定后缀的文件 * @param directory * 特定的目录 * @param extensions * 临时文件的后缀扩展 * @param recursive * 是否查询临时文件所在的目录下的子目录 * @return */ public static Collection<File> searchExtensionFile(File directory,String[] extensions,boolean recursive){ return FileUtils.listFiles(directory, extensions, recursive); } /** * 文件追加功能 * @param lines * @param tmpFilePath */ public static void writeLinesToFile(Collection<String> lines,String tmpFilePath){ OutputStream output=null; try { output = new FileOutputStream(tmpFilePath,true); IOUtils.writeLines(lines, "UTF-8", output); } catch (Exception e) { logger.error(tmpFilePath+"追加文件失败"+e.getMessage()); } } }
评论
5 楼
fym548
2014-11-14
下载乱码需要怎么做?
4 楼
fym548
2014-11-14
上传中文乱码怎么处理?
3 楼
yuaihui
2013-11-08
FileOperateUtils、EnumUtils求同
2 楼
longgangbai
2012-11-19
FileOperateUtils、EnumUtils怎么都没有?这两个类自己封装的两个工具类.已经在下边代码中添加,请知悉!
1 楼
yangkai
2012-11-16
FileOperateUtils、EnumUtils怎么都没有?
能否提供一份打包的源代码呀,谢谢!!!
alex1161596325@gmail.com
能否提供一份打包的源代码呀,谢谢!!!
alex1161596325@gmail.com
发表评论
-
【转】Django resources
2014-01-23 14:35 10718Django resources This page li ... -
使用国内镜像源来加速python pypi包的安装
2014-01-16 11:16 197573pipy国内镜像目前有: http://pypi.d ... -
[转 ]vagrant使用简介
2014-01-10 13:53 256161> 简介: vagrant提供了易于配置,重复性 ... -
[转]在Java中调用Python
2014-01-07 13:08 9144在执行之前都需要把jython对应的包加载进去,这个是必须的 ... -
[转]Jython初探
2014-01-07 11:19 2355转载自: ... -
[转]Eclipse配置PyDev插件
2014-01-02 14:25 2782安装python解释器 安装PyDev: 首 ... -
RestFuse的研究(五) Http请求的封装
2014-06-14 15:50 3518在RestFuse中封装了Http请 ... -
RestFuse的研究(四) Junit的Statement的分析
2013-12-06 11:46 1607在RestFuse提供了多种单 ... -
RestFuse的研究(三) Junit的Rule的使用和分析
2013-12-06 11:01 2191在junit中定义一些可以公用的规则(R ... -
RestFuse的研究(二) Junit的Runner的分类和模式
2013-12-06 10:40 1558在Junit4中的调用JunitCore可以采 ... -
RestFuse的研究(一) HttpJunitRunner的实现
2013-12-06 10:11 1694在RestFuse是一种针对Rest We ... -
[转]An open-source JUnit extension to test HTTP/REST APIs
2013-12-06 09:57 1055http://developer.eclipsesource ... -
TestNG简单的学习(十三)TestNG中Junit的实现
2013-12-04 09:00 3289TestNG和junit的整合 ... -
TestNG简单的学习(十二)TestNG运行
2013-12-03 09:08 51453文档来自官方地址: ... -
TestNG简单的学习(十一)TestNG学习总结
2013-12-03 09:08 13999最近一直在学习关于TestNG方面的知识,根 ... -
TestNG简单的学习(十)TestNG @Listeners 的使用
2013-12-03 09:07 8629TestNG官方网站: http://testng.or ... -
TestNG简单的学习(九)TestNG Method Interceptors 的使用
2013-12-03 09:07 2658TestNG官方网站: http://testng ... -
TestNG简单的学习(八)TestNG Annotation Transformers 的使用
2013-12-03 09:07 2761TestNG官方网站: http://testng.or ... -
TestNG简单的学习(七)TestNG编程方式运行
2013-12-02 09:22 2399TestNG官方网站: http://testng.or ... -
TestNG简单的学习(六)测试工厂注释的使用
2013-12-02 09:22 2717TestNG官方网站: http://testng.or ...
相关推荐
FTPClient封装了所有必要的功能来存储和检索从FTP服务器上的文件。 这个类负责所有与FTP服务器交互的底层细节,并提供了便捷的更高层次的接口。 正如来自所有类SocketClient ,您必须首先连接到与服务器connect做...
apache对网络传输封装源码,易用简单, FTPClient encapsulates all the functionality necessary to store and retrieve files from an FTP server. This class takes care of all low level details of ...
利用commons-net-3.1中的FtpClient,自己设计并封装的在Android上的一个FTP客户端例子,库稳定可用于实战项目
基于org.apache.commons.net.ftp.FTPClient; 实现的FTP 上传下载 封装类
包括FTP的客户端和服务器的安装包,commons-net-3.1 jar包,FTP的客户端服务器的详细安装和配置,和对org.apache.commons.net.ftp.FTPClient; 类的操作和封装的一些方法,和使用过程中遇到的一些问题的归纳,如...
开源库的包名是这个org.apache.commons.net.ftp.FTPClient;是属于局域网的ftp上传,要有ip、端口、用户名以及密码。我根据网上的下载demo,自已研究了一番,又封装了一个类自已使用。欢迎访问博客:...
笔者当初为了学习JAVA,收集了很多经典源码,源码难易程度分为初级、中级、高级等,详情看源码列表,需要的可以直接下载! 这些源码反映了那时那景笔者对未来的盲目,对代码的热情、执着,对IT的憧憬、向往!此时此...
简介 笔者当初为了学习JAVA,收集了很多经典源码,源码难易程度分为初级、中级、高级等,详情看源码列表,需要的可以直接下载! 这些源码反映了那时那景笔者对未来的盲目,对代码的热情、执着,对IT的憧憬、向往!...