import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.StringTokenizer;
public class FTPClient {
public FTPClient() {
}
/**
* 连接到默认端口的FTP服务器和日志作为 匿名/匿名的。
*/
public synchronized void connect(String host) throws IOException {
connect("host", 21);
}
/**
* 连接到一个FTP服务器和日志匿名/匿名的
*/
public synchronized void connect(String ohotportst, int port) throws IOException {
connect(ohotportst, port, "anonymous", "anonymous");
}
/**
* 连接到一个FTP服务器和日志在与供应用户名和密码
*/
public synchronized void connect(String host, int port, String user,
String pass) throws IOException {
if (socket != null) {
throw new IOException(
"SimpleFTP is already connected. Disconnect first.");//SimpleFTP已连接。首先断开
}
socket = new Socket(host, port);
reader = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream()));
String response = readLine();
if (!response.startsWith("220 ")) {
throw new IOException(
"SimpleFTP received an unknown response when connecting to the FTP server: "//SimpleFTP收到了未知的反应时,连接到FTP服务器
+ response);
}
sendLine("USER " + user);
response = readLine();
if (!response.startsWith("331 ")) {
throw new IOException(
"SimpleFTP received an unknown response after sending the user: "//SimpleFTP收到了未知的反应后发出的用户
+ response);
}
sendLine("PASS " + pass);
response = readLine();
if (!response.startsWith("230 ")) {
throw new IOException(
"SimpleFTP was unable to log in with the supplied password: "//SimpleFTP无法登录所提供的密码
+ response);
}
// Now logged in.
}
/**
* 断开FTP服务器.
*/
public synchronized void disconnect() throws IOException {
try {
sendLine("QUIT");
} finally {
socket = null;
}
}
/**
* 返回到工作目录的FTP服务器是连接到.
*/
public synchronized String pwd() throws IOException {
sendLine("PWD");
String dir = null;
String response = readLine();
if (response.startsWith("257 ")) {
int firstQuote = response.indexOf('\"');
int secondQuote = response.indexOf('\"', firstQuote + 1);
if (secondQuote > 0) {
dir = response.substring(firstQuote + 1, secondQuote);
}
}
return dir;
}
/**
* 改变工作目录(如光盘) 。返回true如果成功的话
*/
public synchronized boolean cwd(String dir) throws IOException {
sendLine("CWD " + dir);
String response = readLine();
return (response.startsWith("250 "));
}
/**
* 发出了一个文件,存储在FTP服务器。返回true如果该文件 转移是成功的。该文件发出的被动模式,以避免的NAT或 防火墙的问题,客户端。
*/
public synchronized boolean stor(File file) throws IOException {
if (file.isDirectory()) {
throw new IOException("SimpleFTP cannot upload a directory.");//SimpleFTP不能上传目录
}
String filename = file.getName();
return stor(new FileInputStream(file), filename);
}
/**
* 发出了一个文件,存储在FTP服务器。返回true如果该文件 转移是成功的。该文件发出的被动模式,以避免的NAT或 防火墙的问题,客户端。
*/
public synchronized boolean stor(InputStream inputStream, String filename)
throws IOException {
BufferedInputStream input = new BufferedInputStream(inputStream);
sendLine("PASV");
String response = readLine();
if (!response.startsWith("227 ")) {
throw new IOException("SimpleFTP could not request passive mode: "//SimpleFTP不能要求被动模式
+ 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: "//SimpleFTP收到坏数据链接信息
+ response);
}
}
sendLine("STOR " + filename);
Socket dataSocket = new Socket(ip, port);
response = readLine();
if (!response.startsWith("125 ")) {
// if (!response.startsWith("150 ")) {
throw new IOException(
"SimpleFTP was not allowed to send the file: " + response);//SimpleFTP不允许传送档案
}
BufferedOutputStream output = new BufferedOutputStream(dataSocket
.getOutputStream());
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();
response = readLine();
return response.startsWith("226 ");
}
/**
* 输入二进制模式传送二进制文件.
*/
public synchronized boolean bin() throws IOException {
sendLine("TYPE I");
String response = readLine();
return (response.startsWith("200 "));
}
/**
* 输入ASCII模式发送文本文件。这通常是默认模式。 请务必使用二进制模式,如果您要传送图片或其他二进制 数据, ASCII模式很可能会损坏它们。
*/
public synchronized boolean ascii() throws IOException {
sendLine("TYPE A");
String response = readLine();
return (response.startsWith("200 "));
}
/**
* 发送一个命令到FTP服务器.
*/
private void sendLine(String line) throws IOException {
if (socket == null) {
throw new IOException("SimpleFTP is not connected.");//SimpleFTP没有连接
}
try {
writer.write(line + "\r\n");
writer.flush();
if (DEBUG) {
System.out.println("> " + line);
}
} catch (IOException e) {
socket = null;
throw e;
}
}
private String readLine() throws IOException {
String line = reader.readLine();
if (DEBUG) {
System.out.println("< " + line);
}
return line;
}
private Socket socket = null;
private BufferedReader reader = null;
private BufferedWriter writer = null;
private static boolean DEBUG = false;
public static void main(String args[]) {
FTPClient ftp = new FTPClient();
try {
ftp.connect("192.168.0.47",21,"user","pass");
} catch (IOException e) {
e.printStackTrace();
}
}
}
这段代码老报错:错误码如下:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder$CharsetSD.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at ftp.scoket.FTPClient.readLine(FTPClient.java:227)
at ftp.scoket.FTPClient.connect(FTPClient.java:51)
at ftp.scoket.FTPClient.main(FTPClient.java:245)
19 个解决方案
#1
socket异常,看看服务有没有开。
再看看发送的连接消息对不对。
再看看发送的连接消息对不对。
#2
服务器有开,您能具体到上面代码的某一处吗.
#3
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行,你自己看看问题在哪里。。。。
#4
各位帮我调试一下吧。
#5
private String readLine() throws IOException {
String line = reader.readLine();
if (DEBUG) {
System.out.println(" < " + line);
}
return line;
}
上面标置的就是227行,帮忙试一下了
String line = reader.readLine();
if (DEBUG) {
System.out.println(" < " + line);
}
return line;
}
上面标置的就是227行,帮忙试一下了
#6
我有做好的例子,给你发一个。
#7
楼上那位,可以给我发一个吗,小妹我太谢谢你了.
#8
我的邮箱是:sweet.beijing@163.com
#9
heheh
#10
楼上的笑啥
#11
是不是有放火墙阿?
#12
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;
}
}
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;
}
}
#13
原来是MM。。。。
#14
您看我上面那个程序有问题吗,你可以在您的机子上实现吗
#15
怎么没有人帮忙调试上面的程序呢...
#16
怎么没人回笨呢.先自己顶一下
#17
没环境,测试不了。还要安装ftp服务器。。
#18
怎么没人顶啊,那段程序现在对我来说很重要,帮忙高度一下吧
#19
哈哈
#20
#1
socket异常,看看服务有没有开。
再看看发送的连接消息对不对。
再看看发送的连接消息对不对。
#2
服务器有开,您能具体到上面代码的某一处吗.
#3
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行,你自己看看问题在哪里。。。。
#4
各位帮我调试一下吧。
#5
private String readLine() throws IOException {
String line = reader.readLine();
if (DEBUG) {
System.out.println(" < " + line);
}
return line;
}
上面标置的就是227行,帮忙试一下了
String line = reader.readLine();
if (DEBUG) {
System.out.println(" < " + line);
}
return line;
}
上面标置的就是227行,帮忙试一下了
#6
我有做好的例子,给你发一个。
#7
楼上那位,可以给我发一个吗,小妹我太谢谢你了.
#8
我的邮箱是:sweet.beijing@163.com
#9
heheh
#10
楼上的笑啥
#11
是不是有放火墙阿?
#12
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;
}
}
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;
}
}
#13
原来是MM。。。。
#14
您看我上面那个程序有问题吗,你可以在您的机子上实现吗
#15
怎么没有人帮忙调试上面的程序呢...
#16
怎么没人回笨呢.先自己顶一下
#17
没环境,测试不了。还要安装ftp服务器。。
#18
怎么没人顶啊,那段程序现在对我来说很重要,帮忙高度一下吧
#19
哈哈