各位帮帮忙吧,关于FTPClient连接FTP服务器的!!! socket异常,看看服务有没有开。再看看发送的连接消息对不对。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 at ftp.scoket.FTPClient.readLine(FTPClient.java:227) at ftp.scoket.FTPClient.connect(FTPClient.java:51) at ftp.scoket.FTPClient.main(FTPClient.java:245)227行,你自己看看问题在哪里。 private String readLine() throws IOException { String line = reader.readLine(); if (DEBUG) { System.out.println(" < " + line); } return line; } 上面标置的就是227行,帮忙试一下了 package nc.ui.doc.doc_007;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import nc.itf.doc.DocDelegator;import nc.vo.doc.doc_007.DirVO;import nc.vo.pub.BusinessException;import nc.vo.pub.SuperVO;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPFile;import org.apache.commons.net.ftp.FTPReply;import org.apache.commons.net.ftp.FTP;public class FtpTool { private FTPClient ftp; private String romateDir = ""; private String userName = ""; private String password = ""; private String host = ""; private String port = "21"; public FtpTool(String url) throws IOException { //String url="ftp://user:password@ip:port/ftptest/psd"; int len = url.indexOf("//"); String strTemp = url.substring(len + 2); len = strTemp.indexOf(":"); userName = strTemp.substring(0, len); strTemp = strTemp.substring(len + 1); len = strTemp.indexOf("@"); password = strTemp.substring(0, len); strTemp = strTemp.substring(len + 1); host = ""; len = strTemp.indexOf(":"); if (len < 0)//没有设置端口 { port = "21"; len = strTemp.indexOf("/"); if (len > -1) { host = strTemp.substring(0, len); strTemp = strTemp.substring(len + 1); } else { strTemp = ""; } } else { host = strTemp.substring(0, len); strTemp = strTemp.substring(len + 1); len = strTemp.indexOf("/"); if (len > -1) { port = strTemp.substring(0, len); strTemp = strTemp.substring(len + 1); } else { port = "21"; strTemp = ""; } } romateDir = strTemp; ftp = new FTPClient(); ftp.connect(host, FormatStringToInt(port)); } public FtpTool(String host, int port) throws IOException { ftp = new FTPClient(); ftp.connect(host, port); } public String login(String username, String password) throws IOException { this.ftp.login(username, password); return this.ftp.getReplyString(); } public String login() throws IOException { this.ftp.login(userName, password); System.out.println("ftp用户: " + userName); System.out.println("ftp密码: " + password); if (!romateDir.equals("")) System.out.println("cd " + romateDir); ftp.changeWorkingDirectory(romateDir); return this.ftp.getReplyString(); } public boolean upload(String pathname, String filename) throws IOException, BusinessException { int reply; int j; String m_sfilename = null; filename = CheckNullString(filename); if (filename.equals("")) return false; reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); System.out.println("FTP server refused connection."); System.exit(1); } FileInputStream is = null; try { File file_in; if (pathname.endsWith(File.separator)) { file_in = new File(pathname + filename); } else { file_in = new File(pathname + File.separator + filename); } if (file_in.length() == 0) { System.out.println("上传文件为空!"); return false; } //产生随机数最大到99 j = (int)(Math.random()*100); m_sfilename = String.valueOf(j) + ".pdf"; // 生成的文件名 is = new FileInputStream(file_in); ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.storeFile(m_sfilename, is); ftp.logout(); } finally { if (is != null) { is.close(); } } System.out.println("上传文件成功!"); return true; } public boolean delete(String filename) throws IOException { FileInputStream is = null; boolean retValue = false; try { retValue = ftp.deleteFile(filename); ftp.logout(); } finally { if (is != null) { is.close(); } } return retValue; } public void close() { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } public static int FormatStringToInt(String p_String) { int intRe = 0; if (p_String != null) { if (!p_String.trim().equals("")) { try { intRe = Integer.parseInt(p_String); } catch (Exception ex) { } } } return intRe; } public static String CheckNullString(String p_String) { if (p_String == null) return ""; else return p_String; } public boolean downfile(String pathname, String filename) { String outputFileName = null; boolean retValue = false; try { FTPFile files[] = ftp.listFiles(); int reply = ftp.getReplyCode(); //////////////////////////////////////////////// if (!FTPReply.isPositiveCompletion(reply)) { try { throw new Exception("Unable to get list of files to dowload."); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } ///////////////////////////////////////////////////// if (files.length == 0) { System.out.println("No files are available for download."); }else { for (int i=0; i<files.length; i++) { System.out.println("Downloading file "+files[i].getName()+"Size:"+files[i].getSize()); outputFileName = pathname + filename + ".pdf"; //return outputFileName; File f = new File(outputFileName); ////////////////////////////////////////////////////// retValue = ftp.retrieveFile(outputFileName, new FileOutputStream(f)); if (!retValue) { try { throw new Exception ("Downloading of remote file"+files[i].getName()+" failed. ftp.retrieveFile() returned false."); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } ///////////////////////////////////////////////////////////// } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return retValue; } } 没环境,测试不了。还要安装ftp服务器。。 菜鸟求助:要隐藏table中指定的行 如何将一个这样的json数据转换成一个list 权限管理最佳实践:一,登录控制 求java大牛帮忙解答2 请问webwork2中如何得到页面文本框提交的日期? 急!!!!! 紧急求教:j2ee怎么实现MVC的?我觉得jsp无法notify啊 在jdom和jaxen时候xpath的问题? 请问:可以在EJB里打开一个SOCKET与另一个地点的SERVER上的SERVLET通信吗? 大佬们我在安装eclipse时 出现了这样的问题,求解!! sqlite 连 hibernate,如何配置啊? 求JUnit 测试Struts2的例子
at ftp.scoket.FTPClient.readLine(FTPClient.java:227)
at ftp.scoket.FTPClient.connect(FTPClient.java:51)
at ftp.scoket.FTPClient.main(FTPClient.java:245)
227行,你自己看看问题在哪里。
String line = reader.readLine();
if (DEBUG) {
System.out.println(" < " + line);
}
return line;
} 上面标置的就是227行,帮忙试一下了
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;import nc.itf.doc.DocDelegator;
import nc.vo.doc.doc_007.DirVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO;import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTP;public class FtpTool {
private FTPClient ftp; private String romateDir = ""; private String userName = ""; private String password = ""; private String host = ""; private String port = "21"; public FtpTool(String url) throws IOException {
//String url="ftp://user:password@ip:port/ftptest/psd";
int len = url.indexOf("//");
String strTemp = url.substring(len + 2);
len = strTemp.indexOf(":");
userName = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1); len = strTemp.indexOf("@");
password = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);
host = "";
len = strTemp.indexOf(":");
if (len < 0)//没有设置端口
{
port = "21";
len = strTemp.indexOf("/");
if (len > -1) {
host = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);
} else {
strTemp = "";
}
} else {
host = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);
len = strTemp.indexOf("/");
if (len > -1) {
port = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);
} else {
port = "21";
strTemp = "";
}
}
romateDir = strTemp;
ftp = new FTPClient();
ftp.connect(host, FormatStringToInt(port)); } public FtpTool(String host, int port) throws IOException {
ftp = new FTPClient();
ftp.connect(host, port);
} public String login(String username, String password) throws IOException {
this.ftp.login(username, password);
return this.ftp.getReplyString();
} public String login() throws IOException {
this.ftp.login(userName, password);
System.out.println("ftp用户: " + userName);
System.out.println("ftp密码: " + password);
if (!romateDir.equals(""))
System.out.println("cd " + romateDir);
ftp.changeWorkingDirectory(romateDir);
return this.ftp.getReplyString();
} public boolean upload(String pathname, String filename) throws IOException, BusinessException { int reply;
int j;
String m_sfilename = null;
filename = CheckNullString(filename);
if (filename.equals(""))
return false;
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.out.println("FTP server refused connection.");
System.exit(1);
}
FileInputStream is = null;
try {
File file_in;
if (pathname.endsWith(File.separator)) {
file_in = new File(pathname + filename);
} else {
file_in = new File(pathname + File.separator + filename);
}
if (file_in.length() == 0) {
System.out.println("上传文件为空!");
return false;
}
//产生随机数最大到99
j = (int)(Math.random()*100);
m_sfilename = String.valueOf(j) + ".pdf"; // 生成的文件名
is = new FileInputStream(file_in);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.storeFile(m_sfilename, is);
ftp.logout();
} finally {
if (is != null) {
is.close();
}
}
System.out.println("上传文件成功!");
return true;
}
public boolean delete(String filename) throws IOException {
FileInputStream is = null;
boolean retValue = false;
try {
retValue = ftp.deleteFile(filename);
ftp.logout();
} finally {
if (is != null) {
is.close();
}
}
return retValue;
} public void close() {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
} public static int FormatStringToInt(String p_String) {
int intRe = 0;
if (p_String != null) {
if (!p_String.trim().equals("")) {
try {
intRe = Integer.parseInt(p_String);
} catch (Exception ex) { }
}
}
return intRe;
} public static String CheckNullString(String p_String) {
if (p_String == null)
return "";
else
return p_String;
}
public boolean downfile(String pathname, String filename) {
String outputFileName = null;
boolean retValue = false;
try {
FTPFile files[] = ftp.listFiles();
int reply = ftp.getReplyCode(); ////////////////////////////////////////////////
if (!FTPReply.isPositiveCompletion(reply)) {
try {
throw new Exception("Unable to get list of files to dowload.");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/////////////////////////////////////////////////////
if (files.length == 0) {
System.out.println("No files are available for download.");
}else {
for (int i=0; i<files.length; i++) {
System.out.println("Downloading file "+files[i].getName()+"Size:"+files[i].getSize());
outputFileName = pathname + filename + ".pdf";
//return outputFileName;
File f = new File(outputFileName);
//////////////////////////////////////////////////////
retValue = ftp.retrieveFile(outputFileName, new FileOutputStream(f));
if (!retValue) {
try {
throw new Exception ("Downloading of remote file"+files[i].getName()+" failed. ftp.retrieveFile() returned false.");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/////////////////////////////////////////////////////////////
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retValue;
}
}