我写了个ftp下载程序,在本机测试运行时是正常的。但是访问第三方ftp服务器时出现了问题:程序停在 ftpClient.cd("/home/jboss/a/"); 不动了。请高手八卦下,谢谢!程序片段如下:
……程序成功登录ftp服务器后…… try {
if(list.size()>0){ //list是文件集合
for(int i=0;i<list.size();i++){
filename = (String)list.get(i);
if (remoteDir.length() != 0) //remoteDir是ftp目录
ftpClient.cd(remoteDir); //程序卡在这不动了
ftpClient.binary();
TelnetInputStream is = ftpClient.get(filename);
File file_out = new File(localpath + File.separator + filename);
FileOutputStream os = new FileOutputStream(file_out);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
is.close();
os.close();
}
}
ftpClient.closeServer();
System.out.println("----登出服务器---------------");
} catch (Exception ex) {
throw new Exception("ftp download file error:" + ex.getMessage());
}
……程序成功登录ftp服务器后…… try {
if(list.size()>0){ //list是文件集合
for(int i=0;i<list.size();i++){
filename = (String)list.get(i);
if (remoteDir.length() != 0) //remoteDir是ftp目录
ftpClient.cd(remoteDir); //程序卡在这不动了
ftpClient.binary();
TelnetInputStream is = ftpClient.get(filename);
File file_out = new File(localpath + File.separator + filename);
FileOutputStream os = new FileOutputStream(file_out);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
is.close();
os.close();
}
}
ftpClient.closeServer();
System.out.println("----登出服务器---------------");
} catch (Exception ex) {
throw new Exception("ftp download file error:" + ex.getMessage());
}
解决方案 »
- extjs和jquery的区别
- 为什么Strut2.x要改变Struts1中Action的单例模式,而为每个request申请一个Action实例?
- 谁能告诉我这几个框架的区别和各自的特点啊
- jsf部署weblogic出错,这里有认识weblogic的人吗?
- 请问怎么在查找结果中继续查找
- JDO 如何处理一对多的关系呢?(谁来简单说一下)
- 请高手详细说一下apache soap的安装步骤,我这按照网上说的,出现错误!
- 高分求Struts的入门教程
- 请教初学者用什么JAVA编译器好?
- 大神们,求解websphere配置Sybase的数据源报这个错是什么意思
- 大家来帮帮忙.......Access denied for user 'root'@'localhost'
- 这样动态设置radiogroup 的值
665 * CD to a specific directory on a remote FTP server
666 *
667 * @param remoteDirectory path of the directory to CD to
668 *
669 * @exception <code>FtpProtocolException</code>
670 */
671 public void cd(String remoteDirectory) throws IOException {
672 if (remoteDirectory == null ||
673 "".equals(remoteDirectory))
674 return;
675 issueCommandCheck("CWD " + remoteDirectory);
676 }
/**
191 * Send a command to the FTP server.
192 *
193 * @param cmd String containing the command
194 * @return reply code
195 *
196 * @exception FtpProtocolException if an error occured
197 */
198 protected int issueCommand(String cmd) throws IOException {
199 command = cmd;
200
201 int reply;
202
203 while (replyPending) {
204 replyPending = false;
205 if (readReply() == FTP_ERROR)
206 throw new FtpProtocolException("Error reading FTP pending reply\n");
207 }
208 do {
209 sendServer(cmd + "\r\n");
210 reply = readReply();
211 } while (reply == FTP_TRY_AGAIN);
212 return reply;
213 }
214
215 /**
216 * Send a command to the FTP server and check for success.
217 *
218 * @param cmd String containing the command
219 *
220 * @exception FtpProtocolException if an error occured
221 */
222 protected void issueCommandCheck(String cmd) throws IOException {
223 if (issueCommand(cmd) != FTP_SUCCESS)
224 throw new FtpProtocolException(cmd + ":" + getResponseString());
225 }/**
228 * Read the reply from the FTP server.
229 *
230 * @return FTP_SUCCESS or FTP_ERROR depending on success
231 * @exception FtpProtocolException if an error occured
232 */
233 protected int readReply() throws IOException {
234 lastReplyCode = readServerResponse();
235
236 switch (lastReplyCode / 100) {
237 case 1:
238 replyPending = true;
239 /* falls into ... */
240
241 case 2:
242 case 3:
243 return FTP_SUCCESS;
244
245 case 5:
246 if (lastReplyCode == 530) {
247 if (!loggedIn) {
248 throw new FtpLoginException("Not logged in");
249 }
250 return FTP_ERROR;
251 }
252 if (lastReplyCode == 550) {
253 throw new FileNotFoundException(command + ": " + getResponseString());
254 }
255 }
256
257 /* this statement is not reached */
258 return FTP_ERROR;
259 }
ftpClient.openServer(hostName, port);---- port=21
ftpClient.login(userName, passWord);
都能登录成功在进入文件目录时就卡住了。但是 用命令就可以直接cd进去:
ftp> cd /home/jboss/a/
250 CWD command successful.