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

📄 ftpclient.java

📁 一个实用工具类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		}			} finally {		socket.close();	}		socket.close();}		/* * pw * */public StringBuffer retrieveFileSB(String remoteFile) throws IOException {	return retrieveFileSB(remoteFile, BINARY_TRANSFER_MODE);}/*** !AS*/public StringBuffer retrieveFileSB(String remoteFile, char transferType) throws IOException {				checkLoggedIn();		checkStringOk(remoteFile, "No file information specified");		ByteArrayOutputStream out = new ByteArrayOutputStream();		f_bytesTransferred = 0;		Socket socket = enterPassiveMode();		try {			setType(transferType);			checkReply(sendCommand("RETR " + remoteFile), "1");			trace("After creating a new socket");			BufferedInputStream bufferedinputstream = new BufferedInputStream(socket.getInputStream());			BufferedOutputStream bufferedoutputstream = null;			try {				transfer(bufferedinputstream, out, transferType);			} finally {				if (bufferedoutputstream != null) {					bufferedoutputstream.close();				}								if (bufferedinputstream != null) {					bufferedinputstream.close();				}								checkReply(getReply(), "2");			}		} finally {			socket.close();		}				socket.close();		return new StringBuffer(out.toString());	}public String[] retrieveList() throws IOException {	return retrieveList(currentDirectory(), true);}public String[] retrieveList(String dir, boolean detail) throws IOException {	return doDirList(dir, detail);}public String sendQuoteCommand(String cmd) throws IOException {	//!AS	checkLoggedIn();	checkStringOk(cmd, "No argument specified for QUOTE command!");		String reply = null;		try {		trace("Sending the command: " + cmd);		reply = sendCommand(cmd);	} catch(IOException ioexception) {		if (ioexception.getMessage().startsWith("500")) {			reply = sendCommand(cmd.toUpperCase());		} else {			reply = ioexception.getMessage();		}	}		return reply;}public void sendSiteCommand(String cmd) throws IOException {	checkLoggedIn();	checkStringOk(cmd, "No argument specified for SITE command!");	try {		checkReply(sendCommand("SITE " + cmd), "2");	} catch(IOException ioexception) {		if (ioexception.getMessage().startsWith("500")) {			checkReply(sendCommand("site " + cmd), "2");		} else {			throw ioexception;		}	}}public void setAccount(String account) throws IOException {	f_account = account;}public void setPassword(String password) {	f_password = password;}public void setPort(int port) {	f_port = port;}public void setType(char type) throws IOException {	checkLoggedIn();		// change directory	if (type == ASCII_TRANSFER_MODE		|| type == BINARY_TRANSFER_MODE		|| type == EBCDIC_TRANSFER_MODE) {			checkReply(sendCommand("TYPE " + type), "2");		f_type = type;		trace("hostTransferType set to: " + f_type);		} else {		Log.logStr(this, Log.LOG_TYPE_WARN, "setType", "Unknown type: " + type);	}}/** * Simply sends a NOOP (No operation) to test  * the connection. *  * @throws IOException if connection cannot be made */public void testConnection() throws IOException {	checkLoggedIn();		sendCommand("NOOP");}public void setUserName(String userName) {	f_userName = userName;}public void storeFile(StringBuffer buf, String remoteFile, char type) throws IOException {	checkLoggedIn();		f_bytesTransferred = 0;			Socket socket = enterPassiveMode();		try {		trace("Remote filename = " + remoteFile);		setType(type);		checkReply(sendCommand("STOR " + remoteFile), "1");		ByteArrayInputStream bais = new ByteArrayInputStream(buf.toString().getBytes());		BufferedInputStream bis = new BufferedInputStream(bais);		BufferedInputStream bufferedinputstream = bis;		BufferedOutputStream bufferedoutputstream = null;		try {			bufferedoutputstream = new BufferedOutputStream(socket.getOutputStream());			transfer(bufferedinputstream, bufferedoutputstream, type);		} finally {			if(bufferedoutputstream != null) {				bufferedoutputstream.close();			}			if(bufferedinputstream != null) {				bufferedinputstream.close();			}			checkReply(getReply(), "2");		}	} finally {		socket.close();	}	socket.close();}public void storeFile(String localFile, String remoteFile, char type) throws IOException {	checkLoggedIn();	checkStringOk(localFile, "Local file name is missing!");	checkStringOk(remoteFile, "Remote file name is missing!");	f_bytesTransferred = 0;	Socket socket = enterPassiveMode();	try {		trace("Remote filename = " + remoteFile);		setType(type);		checkReply(sendCommand("STOR " + remoteFile), "1");		BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(localFile));		BufferedOutputStream bufferedoutputstream = null;		try {			bufferedoutputstream = new BufferedOutputStream(socket.getOutputStream());			transfer(bufferedinputstream, bufferedoutputstream, type);		} finally {			if(bufferedoutputstream != null) {				bufferedoutputstream.close();			}						if(bufferedinputstream != null) {				bufferedinputstream.close();			}			checkReply(getReply(), "2");		}	} finally {		socket.close();	}		socket.close();}public void storeFile(String localFile, String remoteFile) throws IOException {	storeFile(localFile, remoteFile, f_type);}public void storeFile(String file) throws IOException {	storeFile(file, file);}protected void transfer(InputStream inputstream, OutputStream outputstream, char type) throws IOException {	if (type == ASCII_TRANSFER_MODE) {		BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(inputstream));		DataOutputStream dataoutputstream = null;		try {			dataoutputstream = new DataOutputStream(outputstream);			String s;			while((s = bufferedreader.readLine()) != null) {				dataoutputstream.writeBytes(s + "\n");				f_bytesTransferred += s.length() + 1;			}		} finally {			if (dataoutputstream != null) {				dataoutputstream.close();			}						if (bufferedreader != null) {				bufferedreader.close();			}		}				return;			} else if (type == BINARY_TRANSFER_MODE) {		int j = 0;		byte abyte0[] = new byte[1024];		int i;				while ((i = inputstream.read()) > -1) {			if (j < abyte0.length) {				abyte0[j++] = (byte)i;			} else {				outputstream.write(abyte0);				outputstream.flush();				f_bytesTransferred += abyte0.length;				j = 0;				abyte0[j++] = (byte)i;			}		}				if (j > 0) {			outputstream.write(abyte0, 0, j);			f_bytesTransferred += j;		}	}}/*** !AS*/public StringBuffer unzipFileSB(String remoteFile) throws IOException {				checkLoggedIn();		checkStringOk(remoteFile, "No file information specified");		StringBuffer out = new StringBuffer();		f_bytesTransferred = 0;		Socket socket = enterPassiveMode();		setType(BINARY_TRANSFER_MODE);				try {			checkReply(sendCommand("RETR " + remoteFile), "1");			trace("After creating a new socket");			try {				out = unzip(socket.getInputStream());			} finally {				checkReply(getReply(), "2");			}		} finally {			socket.close();		}				socket.close();		return out;	}protected StringBuffer unzip(InputStream inputstream) throws IOException {	byte[] bytes = new byte[512];	int len;	StringBuffer out = null;	java.util.zip.ZipInputStream zis = null;	java.util.zip.ZipEntry ze = null;		try {		zis = new java.util.zip.ZipInputStream(inputstream);				ze = zis.getNextEntry();		out = new StringBuffer();			while ((len = zis.read(bytes)) >= 0) {			out.append(new String(bytes));		}	} catch (java.io.IOException e) {		Log.logException(this, Log.LOG_TYPE_ERROR, "zip", e);	}	if (zis != null) {		zis.close();	}		return out;}protected StringBuffer unzip(java.util.zip.ZipInputStream zis) throws IOException {	byte[] bytes = new byte[512];	int len;	StringBuffer out = null;	java.util.zip.ZipEntry ze = null;		try {		ze = zis.getNextEntry();		out = new StringBuffer();			while ((len = zis.read(bytes)) >= 0) {			out.append(bytes);		}	} catch (java.io.IOException e) {		Log.logException(this, Log.LOG_TYPE_ERROR, "zip", e);	}	if (zis != null) {		zis.close();	}		return out;}public FtpClient() {	super();		f_userName = "";	f_account = "";	f_password = "";	f_loggedIn = false;	f_type = ASCII_TRANSFER_MODE;	setMultiLine(true);	f_retryCommand = true;}public void zipFileSB(StringBuffer buf, String remoteFile) throws IOException {	checkLoggedIn();		f_bytesTransferred = 0;			Socket socket = enterPassiveMode();		try {		trace("Remote filename = " + remoteFile);					checkReply(sendCommand("STOR " + remoteFile + ".zip"), "1");		ByteArrayInputStream bais = new ByteArrayInputStream(buf.toString().getBytes());		BufferedInputStream bis = new BufferedInputStream(bais);				BufferedInputStream bufferedinputstream = bis;		BufferedOutputStream bufferedoutputstream = null;		try {			bufferedoutputstream = new BufferedOutputStream(socket.getOutputStream());			zip(bufferedinputstream, bufferedoutputstream, remoteFile);		} finally {			if(bufferedoutputstream != null) {				bufferedoutputstream.close();			}			if(bufferedinputstream != null) {				bufferedinputstream.close();			}			checkReply(getReply(), "2");		}	} finally {		socket.close();	}	socket.close();}protected void zip(InputStream inputstream, OutputStream outputstream, String entryName) throws IOException {	byte[] bytes = new byte[512];	int len;	java.util.zip.ZipOutputStream zos = null;	java.util.zip.ZipEntry ze = null;		try {		zos = new java.util.zip.ZipOutputStream(outputstream);		ze = new java.util.zip.ZipEntry(entryName);				zos.putNextEntry(ze);		while ((len = inputstream.read(bytes)) >= 0) {			zos.write(bytes, 0, len);		}		Log.logStr(Log.LOG_LEVEL_FULL, this, Log.LOG_TYPE_INFO, "zip", ze.getName() + " (" + ze.getCompressedSize() *100 /ze.getSize() + "%)");	} catch (java.io.IOException e) {		Log.logException(this, Log.LOG_TYPE_ERROR, "zip", e);	}	if (zos != null) {		zos.closeEntry();		zos.close();	}}public boolean changeFilePermission(String permission, String fileName) throws IOException {	checkLoggedIn();	String chmodValues = "01234567";		if (permission == null || permission.trim().length() != 3) {		Log.logStr(this, Log.LOG_TYPE_ERROR, "changeFilePermission()", "invalid permission, is either null, to short or to long");		throw new IOException("changeFilePermission - invalid permission");	}		if (fileName == null || fileName.trim().length() <= 0) {		Log.logStr(this, Log.LOG_TYPE_ERROR, "changeFilePermission()", "invalid fileName, is either null, or empty");		throw new IOException("changeFilePermission - invalid fileName");	}				for (int i = 0; i < 3; i++) {		if (chmodValues.indexOf(permission.charAt(i)) < 0) {			Log.logStr(this, Log.LOG_TYPE_ERROR, "changeFilePermission()", "invalid permission, values must be in 0-7 range");			throw new IOException("changeFilePermission - invalid permission values");		}	}		sendSiteCommand("CHMOD " + permission + " " + fileName);	return true;}		/**	 * @return	 */	public String getInitialDir() {		return f_initialDir;	}	/**	 * @param string	 */	public void setInitialDir(String string) {		f_initialDir = string;	}}

⌨️ 快捷键说明

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