⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpconnection.java

📁 基于Java的FTP源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			/* create a socket and return it */			//System.out.println(s_hostIP +":" + port);            return new Socket(s_hostIP, port);		}		else		{		   return null;		}	}	/**	 *	 * @return the <code>InputStream</code> for the control connection.	 */    public InputStream getIn() {        return in;    }	/**	 *	 * @return the <code>OutputStream</code> for the control connection.	 */    public OutputStream getOut() {        return out;    }	/**	 * Error messages are typically 500 status codes. This method returns	 * true if such a status code is not encountered.	 *	 * @return did the server accept the last action.	 *	 * @throws IOException	 */	public boolean isOk() throws IOException	{		/** @todo implement support for other error code */	    return !check_reply("500");	}	/**	 * Returns true if the server returns the expected code. The exact message	 * is not interesting.	 *	 * @param code the status code that we expect.	 * @return did we recieve the expected code.	 *	 * @throws IOException	 */	public boolean check_reply(String code) throws IOException	{		if(code.length() == 4)		{			/*			 * a specific check is made for a multiline reply (eg '230-')			 */			lastMessage = getLine();			log("> "+lastMessage);			if(lastMessage== null || !lastMessage.startsWith(code))			{				return false;			}			else			{				return true;			}		}		else		{			/*			 * the programmer doesn't want to bother with handling multi			 * line replies himself so let's handle it for him.			 */			lastMessage="";			String code2 = code +"-";			String s;			while(true)			{				s = getLine();				log("> " + s);				if(s != null)				{					lastMessage += s;					if(s.startsWith(code2))					{						continue;					}				}				break;			}			if(s== null || !s.startsWith(code))			{				return false;			}			else			{				return true;			}		}	}	/**	 * Create a new directory on the server, will not attempt to determine if	 * the directory already exists.	 *	 * @param dir - name of the new folder.	 * @return success or failure	 * @throws IOException	 */	public boolean mkdir(String dir) throws IOException	{		writeln("MKD " + dir);		return check_reply("257");	}	/**	 * sends the USER command to the server, you can call this method	 * directory if you want more control than given by the	 * {@link #login login()} method.	 * @param user username	 * @return true if the username is acceptable.	 * @throws IOException	 */	public boolean user(String user) throws IOException	{		String user_cmd ="user " + user;		writeln(user_cmd);		return check_reply("331");	}	/**	 * sends the password as part of the user authentication procedure and reads	 * the welcome message if one is available.	 *	 * @param pass - password for the user	 * @return Is the password accepted?	 *	 * @throws IOException	 */	public boolean pass(String pass) throws IOException	{		send_command("PASS", (pass==null) ? "anonymous@localhost" : pass);		if(check_reply("230-"))		{			/* we have a welcome message */			while(true)			{				welcome += lastMessage;				if(! check_reply("230-"))				{					return lastMessage.startsWith("230");				}			}		}		else		{			return lastMessage.startsWith("230");		}	}	/**	 * logs the communitcation.	 * @param mes add this message to the log.	 */	protected void log(String mes)	{		if(logWriter != null)		{			logWriter.println(mes);		}	}	/**	 * Returns the last response from the server. You may need to call this	 * method if {@link #check_reply check_reply()} returned false, which	 * indicates that the expected response was not recieved. The last	 * message should then be retrieved for closer ispection.	 *	 * @return last response.	 */    public String getLastMessage() {        return lastMessage;    }	/**	 * Swtich between Active and Passive modes.	 *	 * @return current mode	 */	public int switchMode()	{		connectMode = (connectMode == ACTIVE_MODE) ? PASV_MODE : ACTIVE_MODE;		return connectMode;	}	/**	 * open a new active or passive data connection.	 *	 * @return DataConnection	 * @throws IOException	 */	public DataConnection makeDataConnection() throws IOException	{		DataConnection con = new DataConnection();		if(connectMode == ACTIVE_MODE)		{			con.sock_active = port();		}		else		{			con.sock_pasv = pasv();		}		return con;	}	/**	 * Utility method used in calculating port/ip numbers.	 *	 * @param b byte to be converted to an unsigned short.	 * @return short	 */	private short makeUnsignedShort(byte b)	{		return ( b < 0 )         ? (short) (b + 256)  : (short) b;	}	/**	 * Where to log. The default is System.out, use null to disbale logging.	 * @param logWriter use this writer for loggin.	 */    public void setLogWriter(PrintStream logWriter) {        this.logWriter = logWriter;    }    public PrintStream getLogWriter() {        return logWriter;    }	/**	 * <p>	 * Inner class that acts as an abstraction layer for <code>Socket</code>	 * and <code>ServerSocket</code>.	 * </p>	 * <p>	 * Unfortunately Socket and SeverSocket do not share any ancestors.	 * Therefore we need to create our adapter class that encloses both a     * ServerSocket and a Socket.	 * </p>	 * <p>Though it's possible to obtain a Socket	 * instance from ServerSocket by calling the accept method() it's not	 * suitable for our work because the thread would immidiately become	 * blocked.	 * </p>	 */	public class DataConnection	{		ServerSocket sock_active;		Socket sock_pasv;		/**		 * Follows the adapter pattern, returns the input stream of the		 * server socket or client socket.		 *		 * @return <code>InputStream</code> for the data connection.		 * @throws IOException		 */		public InputStream getInputStream() throws IOException		{			if(connectMode == ACTIVE_MODE)			{				return sock_active.accept().getInputStream();			}			else			{				return sock_pasv.getInputStream();			}		}		/**		 * returns the output stream of the client or server socket depending		 * on whether the active or passive mode is in effect.		 *		 * @return <code>OutputStream</code> for the data connection.		 * @throws IOException		 */		public OutputStream getOutputStream() throws IOException		{			if(connectMode == ACTIVE_MODE)			{				return sock_active.accept().getOutputStream();			}			else			{				return sock_pasv.getOutputStream();			}		}	}	/**	 * Are you there?	 *	 * @return yes or no	 */	public boolean testConnection()	{		if(sock_control.isConnected() &&				!sock_control.isInputShutdown() &&				!sock_control.isOutputShutdown())        {            try {                writeln("NOOP");                return  check_reply("200");            }            catch (IOException ex) {                return false;            }        }        return false;	}	/**	 * Returns the writer associated with the control socket.	 * @return Writer	 */    public Writer getWriter() {        return writer;    }    public void setLocation(URL location) {        this.location = location;    }    public URL getLocation() {        return location;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -