/** * @param path ex:/upload/2023 * @param filename xxx.jpg * @param input 输入流 * @return */ public static boolean uploadFile(String path, String filename, InputStream input) { boolean success = false; FTPClient ftpClient = null; try { ftpClient = getFTPClient(); int reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { return success; } // 使用二进制上传 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); ftpCreateDirectoryTree(ftpClient, path); success = ftpClient.storeFile(filename, input); input.close(); ftpClient.logout(); } catch (IOException e) { e.printStackTrace(); } finally { if (null != ftpClient && ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException ioe) { } } } return success; }
public static void main(String[] args) throws IOException { serverSocket = new ServerSocket(SERVER_PORT); ftpClient = new FTPClient(); ftpClient.connect(IP_FTP_CLIENT); ftpClient.login(USERNAME, PASSWORD); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE); while (true) { clientSocket = serverSocket.accept(); log.info("Client is connected: " + clientSocket.isConnected()); sendMockJSONScannedData(); sendMockFTPScannedImg(); } }
private void uploadDir(File directory) throws IOException { try { String[] children = directory.list(); if (children == null) { // Either dir does not exist or is not a directory } else { for (String filename : children) { File f = new File(directory, filename); if (f.isDirectory()) { ftpClient.mkd(f.getName()); ftpClient.changeWorkingDirectory(f.getName()); uploadDir(f); ftpClient.changeToParentDirectory(); } else { if (!uploaded.contains(filename)) { LOGGER.debug("uploading file: {}", filename); messageListener.addMessage(new Message("uploading file: " + filename)); ftpClient.setFileTransferMode(FTPClient.BINARY_FILE_TYPE); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); InputStream is = new BufferedInputStream(new FileInputStream(directory + "/" + filename)); ftpClient.storeFile(filename, is); uploaded.add(filename); } } } } } catch (IOException e) { LOGGER.error("Upload failure"); throw new IOException(e); } }
/** * Connect to the FTP server using configuration parameters * * * @return An FTPClient instance * @throws IOException */ private FTPClient connect() throws IOException { FTPClient client = null; Configuration conf = getConf(); String host = conf.get("fs.ftp.host"); int port = conf.getInt("fs.ftp.host.port", FTP.DEFAULT_PORT); String user = conf.get("fs.ftp.user." + host); String password = conf.get("fs.ftp.password." + host); client = new FTPClient(); client.connect(host, port); int reply = client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { throw new IOException("Server - " + host + " refused connection on port - " + port); } else if (client.login(user, password)) { client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE); client.setFileType(FTP.BINARY_FILE_TYPE); client.setBufferSize(DEFAULT_BUFFER_SIZE); } else { throw new IOException("Login failed on server - " + host + ", port - " + port); } return client; }