Консультация № 175552
23.12.2009, 00:14
35.00 руб.
0 0 0
Здравствуйте. В данный момент пытаюсь реализовать на java ftp-клиент. В качестве сервера использую FileZilla
Проблема состоит в приеме файлов с сервера. Из потока InputStream приходят файлы малого размера (например 25 кб) При больших размерах файлов (например 400 кб) программа зависает на получении входного потока, затем через некоторое время сервер разрывает соединение с сообщением 425.
Виснет на строчке BufferedInputStream input = new BufferedInputStream(dataSocket.getInputStream());

Спасибо

Приложение:
public synchronized void retr(String filename, File f) {
String response = null;
InputStream inputStr = null;

if (filename.endsWith(".doc") ||

filename.endsWith(".ini") ||
filename.endsWith(".txt")) {
try {
this.ascii();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
} else {
this.bin();
}

try {
sendLine("PASV");
response = readLine();
System.out.println(response);
if (!response.startsWith("227 ")) {
throw new IOException("SimpleFTP could not request passive mode: "
+ response);
}

String ip = null;
int port = -1;
int opening = response.indexOf('(');
int closing = response.indexOf(')', opening + 1);
if (closing > 0) {
String dataLink = response.substring(opening + 1, closing);
StringTokenizer tokenizer = new StringTokenizer(dataLink, ",");
try {
ip = tokenizer.nextToken() + "." + tokenizer.nextToken() + "."
+ tokenizer.nextToken() + "." + tokenizer.nextToken();
port = Integer.parseInt(tokenizer.nextToken()) * 256
+ Integer.parseInt(tokenizer.nextToken());
} catch (Exception e) {
throw new IOException("SimpleFTP received bad data link information: "
+ response);
}

}

int length = 0;

System.out.println(ip+":"+port);
Socket dataSocket = new Socket(ip, port);

sendLine("RETR " + filename.trim());
response = readLine();

if (!response.startsWith("150 ")) {
throw new IOException("SimpleFTP was not allowed to send the file: "
+ response);
}
response = readLine();


BufferedInputStream input = new BufferedInputStream(dataSocket.getInputStream());

FileOutputStream out = new FileOutputStream(f);
BufferedOutputStream output = new BufferedOutputStream(out);

byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = input.read(buffer)) != -1) {

output.write(buffer, 0, bytesRead);
output.flush();
}


output.close();
input.close();

dataSocket.close();
}
catch (IOException e) {
e.printStackTrace();
}

}

Обсуждение

Форма ответа