提交 71cbfc09 authored 作者: Thomas Mueller's avatar Thomas Mueller

Improve FTP client.

上级 3792b52f
...@@ -76,20 +76,24 @@ public class FtpClient { ...@@ -76,20 +76,24 @@ public class FtpClient {
message = message.substring(idx + 1); message = message.substring(idx + 1);
} }
} }
if (message.equals("Quotas off")) {
continue;
}
break; break;
} }
} }
private void readCode(int expected) throws IOException { private void readCode(int optional, int expected) throws IOException {
readLine();
if (code == optional) {
readLine(); readLine();
}
if (code != expected) { if (code != expected) {
throw new IOException("Expected: " + expected + " got: " + message); throw new IOException("Expected: " + expected + " got: " + code + " " + message);
} }
} }
private void readCode(int expected) throws IOException {
readCode(-1, expected);
}
private void send(String command) { private void send(String command) {
writer.println(command); writer.println(command);
writer.flush(); writer.flush();
...@@ -152,7 +156,7 @@ public class FtpClient { ...@@ -152,7 +156,7 @@ public class FtpClient {
*/ */
void delete(String fileName) throws IOException { void delete(String fileName) throws IOException {
send("DELE " + fileName); send("DELE " + fileName);
readCode(250); readCode(226, 250);
} }
/** /**
...@@ -162,7 +166,7 @@ public class FtpClient { ...@@ -162,7 +166,7 @@ public class FtpClient {
*/ */
public void makeDirectory(String dir) throws IOException { public void makeDirectory(String dir) throws IOException {
send("MKD " + dir); send("MKD " + dir);
readCode(257); readCode(226, 257);
} }
/** /**
...@@ -220,7 +224,7 @@ public class FtpClient { ...@@ -220,7 +224,7 @@ public class FtpClient {
private void passive() throws IOException { private void passive() throws IOException {
send("PASV"); send("PASV");
readCode(227); readCode(226, 227);
int first = message.indexOf('(') + 1; int first = message.indexOf('(') + 1;
int last = message.indexOf(')'); int last = message.indexOf(')');
String[] address = StringUtils.arraySplit(message.substring(first, last), ',', true); String[] address = StringUtils.arraySplit(message.substring(first, last), ',', true);
...@@ -252,6 +256,18 @@ public class FtpClient { ...@@ -252,6 +256,18 @@ public class FtpClient {
readCode(250); readCode(250);
} }
/**
* Read a file.
*
* @param fileName the file name
* @return the content, null if the file doesn't exist
*/
public byte[] retrieve(String fileName) throws IOException {
ByteArrayOutputStream buff = new ByteArrayOutputStream();
retrieve(fileName, buff, 0);
return buff.toByteArray();
}
/** /**
* Read a file ([REST] RETR). * Read a file ([REST] RETR).
* *
...@@ -267,7 +283,7 @@ public class FtpClient { ...@@ -267,7 +283,7 @@ public class FtpClient {
} }
send("RETR " + fileName); send("RETR " + fileName);
IOUtils.copyAndClose(inData, out); IOUtils.copyAndClose(inData, out);
readCode(226); readCode(150, 226);
} }
/** /**
...@@ -277,7 +293,7 @@ public class FtpClient { ...@@ -277,7 +293,7 @@ public class FtpClient {
*/ */
public void removeDirectory(String dir) throws IOException { public void removeDirectory(String dir) throws IOException {
send("RMD " + dir); send("RMD " + dir);
readCode(250); readCode(226, 250);
} }
/** /**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论