FTP工具类代码
下面是FTP上传下载的工具类Java代码
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| package com.commons.util;
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; 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.FTPFile; import org.apache.commons.net.ftp.FTPReply;
public class FtpUtil {
public static boolean uploadFile(String host, int port, String username, String password, String basePath, String filePath, String filename, InputStream input) { boolean result = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(host, port); ftp.login(username, password); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } 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)) { return result; } else { ftp.changeWorkingDirectory(tempPath); } } } } ftp.setFileType(FTP.BINARY_FILE_TYPE); if (!ftp.storeFile(filename, input)) { return result; } input.close(); ftp.logout(); result = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } try { ftp.disconnect(); } catch (IOException e) { System.out.println("断开连接失败"); e.printStackTrace(); } return result; }
public static boolean downloadFile(String host, int port, String username, String password, String remotePath, String fileName, String localPath) { boolean result = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(host, port); ftp.login(username, password); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } ftp.changeWorkingDirectory(remotePath); 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(); } }
ftp.logout(); result = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return result; }
public static void main(String[] args) { try { FileInputStream in=new FileInputStream(new File("D:/1.jpg")); boolean flag = uploadFile("192.168.1.63", 21, "ftpuser", "123456", "/home/ftpuser/images/","/2020", "3.jpg", in); System.out.println(flag); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
|
上传图片失败解决方法。
然而我们在测试上传图片时,会出现上传失败的结果。我总结了是三个问题。
未放行 [21] Ftp端口,导致的连接超时,以至于上传失败的原因。
在我们放行端口之后,还是上传失败。原来在配置FTP路径的时候,还需要给路径读写的权限,这样才能够上传。chmod 777 FTP文件路径
。
配置好路径之后,我们再次上传文件,成功了!但是发现图片的大小为0kb。然后我在网上搜索了一下,发现还要工具类下载方法里加上一串代码ftp.enterLocalPassiveMode();
。我在网上查阅到的一些解释:
1 在FTP服务中,涉及到客户端和服务器端的连接,连接就会涉及到端口的打开问题;
2 而端口的打开中,又涉及到主动模式和被动模式。主动模式:客户端开放端口给服务端用;被动模式:服务端开放端口给客户端用。由于很多客户端在防火墙内,开放端口给服务器端用比较困难。所以用被动模式的时候比较多。
3 如果涉及到了内网、局域网等环境时,出现FTP连接问题,也可以考虑是否是因为连接模式引起的。