ftpconnection.java
来自「kaffe Java 解释器语言,源码,Java的子集系统,开放源代码」· Java 代码 · 共 1,082 行 · 第 1/2 页
JAVA
1,082 行
{ StringBuffer buf = new StringBuffer(STRU); buf.append(' '); switch (structure) { case STRUCTURE_FILE: buf.append('F'); break; case STRUCTURE_RECORD:buf.append('R'); break; case STRUCTURE_PAGE:buf.append('P'); break; default:throw new IllegalArgumentException(Integer.toString(structure)); } send(buf.toString()); FTPResponse response = getResponse(); switch (response.getCode()) { case 200: fileStructure = structure; break; default: throw new FTPException(response); } } /** * Returns the current transfer mode. * @return MODE_STREAM, MODE_BLOCK, or MODE_COMPRESSED */ public int getTransferMode() { return transferMode; } /** * Sets the desired transfer mode. * @param mode MODE_STREAM, MODE_BLOCK, or MODE_COMPRESSED */ public void setTransferMode(int mode) throws IOException { StringBuffer buf = new StringBuffer(MODE); buf.append(' '); switch (mode) { case MODE_STREAM: buf.append('S'); break; case MODE_BLOCK:buf.append('B'); break; case MODE_COMPRESSED:buf.append('C'); break; default:throw new IllegalArgumentException(Integer.toString(mode)); } send(buf.toString()); FTPResponse response = getResponse(); switch (response.getCode()) { case 200: transferMode = mode; if (dtp != null) dtp.setTransferMode(mode); break; default: throw new FTPException(response); } } /** * Retrieves the specified file. * @param filename the filename of the file to retrieve * @return an InputStream containing the file content */ public InputStream retrieve(String filename) throws IOException { if (dtp == null || transferMode == MODE_STREAM) initialiseDTP(); /* int size = -1; String cmd = new StringBuffer(SIZE) .append(' ') .append(filename) .toString(); send(cmd); FTPResponse response = getResponse(); switch (response.getCode()) { case 213: size = Integer.parseInt(response.getMessage()); break; case 550: // File not found default: throw new FTPException(response); } */ String cmd = new StringBuffer(RETR).append(' ').append(filename).toString(); send(cmd); FTPResponse response = getResponse(); switch (response.getCode()) { case 125: // Data connection already open; transfer starting case 150: // File status okay; about to open data connection return dtp.getInputStream(); default:throw new FTPException(response); } } /** * Returns a stream for uploading a file. * If a file with the same filename already exists on the server, it will * be overwritten. * @param filename the name of the file to save the content as * @return an OutputStream to write the file data to */ public OutputStream store(String filename) throws IOException { if (dtp == null || transferMode == MODE_STREAM) initialiseDTP(); String cmd = new StringBuffer(STOR).append(' ').append(filename).toString(); send(cmd); FTPResponse response = getResponse(); switch (response.getCode()) { case 125: // Data connection already open; transfer starting case 150: // File status okay; about to open data connection return dtp.getOutputStream(); default:throw new FTPException(response); } } /** * Returns a stream for uploading a file. * If a file with the same filename already exists on the server, the * content specified will be appended to the existing file. * @param filename the name of the file to save the content as * @return an OutputStream to write the file data to */ public OutputStream append(String filename) throws IOException { if (dtp == null || transferMode == MODE_STREAM) initialiseDTP(); String cmd = new StringBuffer(APPE).append(' ').append(filename).toString(); send(cmd); FTPResponse response = getResponse(); switch (response.getCode()) { case 125: // Data connection already open; transfer starting case 150: // File status okay; about to open data connection return dtp.getOutputStream(); default:throw new FTPException(response); } } /** * This command may be required by some servers to reserve sufficient * storage to accommodate the new file to be transferred. * It should be immediately followed by a <code>store</code> or * <code>append</code>. * @param size the number of bytes of storage to allocate */ public void allocate(long size) throws IOException { String cmd = new StringBuffer(ALLO).append(' ').append(size).toString(); send(cmd); FTPResponse response = getResponse(); switch (response.getCode()) { case 200: // OK case 202: // Superfluous break; default:throw new FTPException(response); } } /** * Renames a file. * @param oldName the current name of the file * @param newName the new name * @return true if successful, false otherwise */ public boolean rename(String oldName, String newName) throws IOException { String cmd = new StringBuffer(RNFR).append(' ').append(oldName).toString(); send(cmd); FTPResponse response = getResponse(); switch (response.getCode()) { case 450: // File unavailable case 550: // File not found return false; case 350: // Pending break; default:throw new FTPException(response); } cmd = new StringBuffer(RNTO).append(' ').append(newName).toString(); send(cmd); response = getResponse(); switch (response.getCode()) { case 250: // OK return true; case 450: case 550: return false; default: throw new FTPException(response); } } /** * Aborts the transfer in progress. * @return true if a transfer was in progress, false otherwise */ public boolean abort() throws IOException { send(ABOR); FTPResponse response = getResponse(); // Abort client DTP if (dtp != null) dtp.abort(); switch (response.getCode()) { case 226: // successful abort return false; case 426: // interrupted response = getResponse(); if (response.getCode() == 226) return true; default:throw new FTPException(response); } } /** * Causes the file specified to be deleted at the server site. * @param filename the file to delete */ public boolean delete(String filename) throws IOException { String cmd = new StringBuffer(DELE).append(' ').append(filename).toString(); send(cmd); FTPResponse response = getResponse(); switch (response.getCode()) { case 250: // OK return true; case 450: // File unavailable case 550: // File not found return false; default:throw new FTPException(response); } } /** * Causes the directory specified to be deleted. * This may be an absolute or relative pathname. * @param pathname the directory to delete */ public boolean removeDirectory(String pathname) throws IOException { String cmd = new StringBuffer(RMD).append(' ').append(pathname).toString(); send(cmd); FTPResponse response = getResponse(); switch (response.getCode()) { case 250: // OK return true; case 550: // File not found return false; default:throw new FTPException(response); } } /** * Causes the directory specified to be created at the server site. * This may be an absolute or relative pathname. * @param pathname the directory to create */ public boolean makeDirectory(String pathname) throws IOException { String cmd = new StringBuffer(MKD).append(' ').append(pathname).toString(); send(cmd); FTPResponse response = getResponse(); switch (response.getCode()) { case 257: // Directory created return true; case 550: // File not found return false; default:throw new FTPException(response); } } /** * Returns the current working directory. */ public String getWorkingDirectory() throws IOException { send(PWD); FTPResponse response = getResponse(); switch (response.getCode()) { case 257: String message = response.getMessage(); if (message.charAt(0) == '"') { int end = message.indexOf('"', 1); if (end == -1) throw new ProtocolException(message); return message.substring(1, end); } else { int end = message.indexOf(' '); if (end == -1) return message; else return message.substring(0, end); } default: throw new FTPException(response); } } /** * Returns a listing of information about the specified pathname. * If the pathname specifies a directory or other group of files, the * server should transfer a list of files in the specified directory. * If the pathname specifies a file then the server should send current * information on the file. A null argument implies the user's * current working or default directory. * @param pathname the context pathname, or null */ public InputStream list(String pathname) throws IOException { if (dtp == null || transferMode == MODE_STREAM) initialiseDTP(); if (pathname == null) send(LIST); else { String cmd = new StringBuffer(LIST).append(' ').append(pathname).toString(); send(cmd); } FTPResponse response = getResponse(); switch (response.getCode()) { case 125: // Data connection already open; transfer starting case 150: // File status okay; about to open data connection return dtp.getInputStream(); default: throw new FTPException(response); } } /** * Returns a directory listing. The pathname should specify a * directory or other system-specific file group descriptor; a null * argument implies the user's current working or default directory. * @param pathname the directory pathname, or null * @return a list of filenames (strings) */ public List nameList(String pathname) throws IOException { if (dtp == null || transferMode == MODE_STREAM) initialiseDTP(); if (pathname == null) send(NLST); else { String cmd = new StringBuffer(NLST).append(' ').append(pathname).toString(); send(cmd); } FTPResponse response = getResponse(); switch (response.getCode()) { case 125: // Data connection already open; transfer starting case 150: // File status okay; about to open data connection InputStream in = dtp.getInputStream(); in = new BufferedInputStream(in); in = new CRLFInputStream(in); // TODO ensure that TYPE is correct LineInputStream li = new LineInputStream(in); List ret = new ArrayList(); for (String line = li.readLine(); line != null; line = li.readLine()) ret.add(line); li.close(); return ret; default: throw new FTPException(response); } } /** * Returns the type of operating system at the server. */ public String system() throws IOException { send(SYST); FTPResponse response = getResponse(); switch (response.getCode()) { case 215: String message = response.getMessage(); int end = message.indexOf(' '); if (end == -1) return message; else return message.substring(0, end); default:throw new FTPException(response); } } /** * Does nothing. * This method can be used to ensure that the connection does not time * out. */ public void noop() throws IOException { send(NOOP); FTPResponse response = getResponse(); switch (response.getCode()) { case 200: break; default:throw new FTPException(response); } } // -- I/O -- /** * Sends the specified command line to the server. * The CRLF sequence is automatically appended. * @param cmd the command line to send */ protected void send(String cmd) throws IOException { byte[] data = cmd.getBytes(US_ASCII); out.write(data); out.write(CRLF); out.flush(); } /** * Reads the next response from the server. * If the server sends the "transfer complete" code, this is handled here, * and the next response is passed to the caller. */ protected FTPResponse getResponse() throws IOException { FTPResponse response = readResponse(); if (response.getCode() == 226) { if (dtp != null) dtp.transferComplete(); response = readResponse(); } return response; } /** * Reads and parses the next response from the server. */ protected FTPResponse readResponse() throws IOException { String line = in.readLine(); if (line == null) throw new ProtocolException("EOF"); if (line.length() < 4) throw new ProtocolException(line); int code = parseCode(line); if (code == -1) throw new ProtocolException(line); char c = line.charAt(3); if (c == ' ') return new FTPResponse(code, line.substring(4)); else if (c == '-') { StringBuffer buf = new StringBuffer(line.substring(4)); buf.append('\n'); while (true) { line = in.readLine(); if (line == null) throw new ProtocolException("EOF"); if (line.length() >= 4 && line.charAt(3) == ' ' && parseCode(line) == code) return new FTPResponse(code, line.substring(4), buf.toString()); else { buf.append(line); buf.append('\n'); } } } else throw new ProtocolException(line); } /* * Parses the 3-digit numeric code at the beginning of the given line. * Returns -1 on failure. */ static final int parseCode(String line) { char[] c = { line.charAt(0), line.charAt(1), line.charAt(2) }; int ret = 0; for (int i = 0; i < 3; i++) { int digit = ((int) c[i]) - 0x30; if (digit < 0 || digit > 9) return -1; // Computing integer powers is way too expensive in Java! switch (i) { case 0: ret += (100 * digit); break; case 1: ret += (10 * digit); break; case 2: ret += digit; break; } } return ret; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?