实现FTP文件上传
应用中需要定时将本地文件上传到远程服务器,定时这块先不说了,说说文件上传的方法。这里用的是apache的FTP工具包。
单独写一个FTP管理类,向外提供公共方法调用。
建立连接connect方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /** * 连接FTP服务器 * @param host * @param port * @param userName * @param password * @return 连接成功返回TRUE,失败返回FALSE */ public boolean connect(String host, int port, String userName, String password) { boolean result = true; try { client.connect(host, port); int code = client.getReplyCode(); if (FTPReply.isPositiveCompletion(code)) { if (client.login(userName, password)) { log.info("连接并登陆上FTP服务器" + host + ":" + port); } else { log.info("登录FTP服务器失败" + host + ":" + port); result = false; } } else { log.info("连接FTP服务器失败,关闭连接" + host + ":" + port); result = false; disconnect(); } } catch (Exception e) { // TODO: handle exception disconnect(); log.error("Exception:", e); return false; } return result; } |
断开连接方法disconnect:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /** * 与FTP服务器断开连接 * */ public void disconnect() { // TODO Auto-generated method stub try { if (client.isConnected()) { client.logout(); client.disconnect(); } log.info("与FTP服务器连接已关闭!"); } catch (Exception e) { // TODO: handle exception log.error("Exception:", e); } } |
这里的实现很简单,没有什么好说的
上传文件upload方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | /** * 上传指定文件到服务器,服务器目录为根目录,并且本地文件与上传到服务器的文件名称相同 * @param local 本地文件相对路径,包含文件名 * @return */ public boolean upload(String local) { try { // 设置成服务器被动相应模式 client.enterLocalPassiveMode(); // 文件类型设为二进制类型 client.setFileType(FTP.BINARY_FILE_TYPE); String fileName = local.substring(local.lastIndexOf("/") + 1); FTPFile[] files = client.listFiles(fileName); if (files.length > 0) { log.info(fileName + "文件已存在,不进行上传!"); return false; } File file = new File(local); InputStream is = new FileInputStream(file); if (client.storeFile(fileName, is)) { log.info(fileName + "文件上传成功!"); is.close(); return true; } else { log.info(fileName + "文件上传失败!"); is.close(); return false; } } catch (Exception e) { // TODO: handle exception log.error("Exception:", e); return false; } } |
这里传进本地文件的相对路径作为参数,上传到服务器上指定用户下的根目录。有人问了,我上传到服务器后要将文件名改成指定格式的怎么办呢?很简单,“storeFile(String name, InputStream is)”方法中,第一个“name”参数就是上传到服务器后文件所用的名称。
又有人问了,那我不想上传到根目录下,我要将不同文件上传到指定目录以便管理,这要怎么办呢?工具包中并没有提供自动生成目录结构的方法,但是提供了创建目录和跳转目录的方法:makeDirectory(String arg0)、changeWorkingDirectory(String arg0)。我们可以使用这两个方法,利用递归,自己写一个生成目录的方法,以下:
重写upload方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | /** * 上传本地文件到服务器上的指定目录,可以修改文件名称,若服务器上目录不存在,则自动创建指定的目录结构 * @param local 本地文件的相对路径,包含文件名 * @param remote 服务器上的文件路径,包含文件名 * @return 上传成功返回TRUE,失败返回FALSE */ public boolean upload(String local, String remote) { try { client.enterLocalPassiveMode(); client.setFileType(FTP.BINARY_FILE_TYPE); String remoteFileName = remote; if (remote.contains("/")) { remoteFileName = remote.substring(remote.lastIndexOf("/") + 1); String remoteDirectory = remote.substring(0, remote.lastIndexOf("/") + 1); // 判断远程目录是否存在,若不存在则循环创建 if (!remoteDirectory.equalsIgnoreCase("/") && !client.changeWorkingDirectory(remoteDirectory)) { // 目录结构索引 int start = 0; int end = 0; String subDir = ""; if (remoteDirectory.startsWith("/")) { start = 1; } else { start = 0; } end = remoteDirectory.indexOf("/", start); while (true) { subDir = remoteDirectory.substring(start, end); if (!client.changeWorkingDirectory(subDir)) { if (client.makeDirectory(subDir)) { client.changeWorkingDirectory(subDir); } else { log.info(subDir + "目录创建失败!"); return false; } } start = end + 1; end = remoteDirectory.indexOf("/", start); if (end < start) { // 到目录结尾 break; } } } } FTPFile[] files = client.listFiles(remoteFileName); if (files.length > 0) { log.info(remoteFileName + "文件已存在,不进行上传!"); return false; } File file = new File(local); InputStream is = new FileInputStream(file); if (client.storeFile(remoteFileName, is)) { log.info(remoteFileName + "文件上传成功!"); is.close(); return true; } else { log.info(remoteFileName + "文件上传失败!"); is.close(); return false; } } catch (Exception e) { // TODO: handle exception log.error("Exception", e); return false; } } |
测试方法:
1 2 3 4 5 6 | public static void main(String[] a) { FtpConnection ftp = new FtpConnection(); ftp.connect("57.27.5.44", 21, "123", "456"); ftp.upload("upload/mysql-5.0.86-win32.zip", "/dir/text/mysql-5.0.86-win32.zip"); ftp.disconnect(); } |
十二月 13th, 2009 in
工作时间

