当前位置: 技术问答>java相关
关于用ftp和com做远程数据传输的程序。要求能够在远程的不同数据库之间传输数据,以及传输文件,如excel和txt文件。
来源: 互联网 发布时间:2015-08-02
本文导语: 要求写成通用程序,不局限于某一种数据库。请大家提供一些思路! | ftp 到有一个!! import java.net.*; import java.io.*; import java.util.*; class FtpClient { static final boolean debug = false; ...
要求写成通用程序,不局限于某一种数据库。请大家提供一些思路!
|
ftp 到有一个!!
import java.net.*;
import java.io.*;
import java.util.*;
class FtpClient {
static final boolean debug = false;
public static final int FTP_PORT = 21;
static int FTP_SUCCESS = 1;
static int FTP_TRY_AGAIN = 2;
static int FTP_ERROR = 3;
/** socket for data transfer */
private Socket dataSocket = null;
private boolean replyPending = false;
private boolean binaryMode = false;
private boolean passiveMode = false;
/** user name for login */
String user = null;
/** password for login */
String password = null;
/** last command issued */
String command;
/** The last reply code from the ftp daemon. */
int lastReplyCode;
/** Welcome message from the server, if any. */
public String welcomeMsg;
/** Array of strings (usually 1 entry) for the last reply
from the server. */
protected Vector serverResponse = new Vector(1);
/** Socket for communicating with server. */
protected Socket serverSocket = null;
/** Stream for printing to the server. */
public PrintWriter serverOutput;
/** Buffered stream for reading replies from server. */
public InputStream serverInput;
/** Return server connection status */
public boolean serverIsOpen() {
return serverSocket != null;
}
/**Set Passive mode Trasfers*/
public void setPassive(boolean mode) {
passiveMode = mode;
}
public int readServerResponse() throws IOException {
StringBuffer replyBuf = new StringBuffer(32);
int c;
int continuingCode = -1;
int code = -1;
String response;
if (debug) System.out.println("readServerResponse start");
try{
while (true) {
//if (debug) System.out.println("readServerResponse outer while loop: "+ serverInput.available());
while ((c = serverInput.read()) != -1) {
if (c == 'r') {
if ((c = serverInput.read()) != 'n')
replyBuf.append('r');
}
replyBuf.append((char)c);
if (c == 'n')
break;
}
if (debug) System.out.println("Now past inner while loop");
response = replyBuf.toString();
replyBuf.setLength(0);
if (debug) {
System.out.print(response);
}
try {
code = Integer.parseInt(response.substring(0, 3));
} catch (NumberFormatException e) {
code = -1;
} catch (StringIndexOutOfBoundsException e) {
/* this line doesn't contain a response code, so
we just completely ignore it */
continue;
}
serverResponse.addElement(response);
if (continuingCode != -1) {
/* we've seen a XXX- sequence */
if (code != continuingCode ||
(response.length() >= 4 && response.charAt(3) == '-')) {
continue;
} else {
/* seen the end of code sequence */
continuingCode = -1;
break;
}
} else if (response.length() >= 4 && response.charAt(3) == '-') {
continuingCode = code;
continue;
} else {
break;
}
}
}catch(Exception e){e.printStackTrace();}
if (debug) System.out.println("readServerResponse done");
return lastReplyCode = code;
}
/** Sends command cmd to the server. */
public void sendServer(String cmd) {
if (debug) System.out.println("sendServer start");
serverOutput.println(cmd);
if (debug) System.out.println("sendServer done");
}
/** Returns all server response strings. */
public String getResponseString() {
String s = new String();
for(int i = 0;i
import java.net.*;
import java.io.*;
import java.util.*;
class FtpClient {
static final boolean debug = false;
public static final int FTP_PORT = 21;
static int FTP_SUCCESS = 1;
static int FTP_TRY_AGAIN = 2;
static int FTP_ERROR = 3;
/** socket for data transfer */
private Socket dataSocket = null;
private boolean replyPending = false;
private boolean binaryMode = false;
private boolean passiveMode = false;
/** user name for login */
String user = null;
/** password for login */
String password = null;
/** last command issued */
String command;
/** The last reply code from the ftp daemon. */
int lastReplyCode;
/** Welcome message from the server, if any. */
public String welcomeMsg;
/** Array of strings (usually 1 entry) for the last reply
from the server. */
protected Vector serverResponse = new Vector(1);
/** Socket for communicating with server. */
protected Socket serverSocket = null;
/** Stream for printing to the server. */
public PrintWriter serverOutput;
/** Buffered stream for reading replies from server. */
public InputStream serverInput;
/** Return server connection status */
public boolean serverIsOpen() {
return serverSocket != null;
}
/**Set Passive mode Trasfers*/
public void setPassive(boolean mode) {
passiveMode = mode;
}
public int readServerResponse() throws IOException {
StringBuffer replyBuf = new StringBuffer(32);
int c;
int continuingCode = -1;
int code = -1;
String response;
if (debug) System.out.println("readServerResponse start");
try{
while (true) {
//if (debug) System.out.println("readServerResponse outer while loop: "+ serverInput.available());
while ((c = serverInput.read()) != -1) {
if (c == 'r') {
if ((c = serverInput.read()) != 'n')
replyBuf.append('r');
}
replyBuf.append((char)c);
if (c == 'n')
break;
}
if (debug) System.out.println("Now past inner while loop");
response = replyBuf.toString();
replyBuf.setLength(0);
if (debug) {
System.out.print(response);
}
try {
code = Integer.parseInt(response.substring(0, 3));
} catch (NumberFormatException e) {
code = -1;
} catch (StringIndexOutOfBoundsException e) {
/* this line doesn't contain a response code, so
we just completely ignore it */
continue;
}
serverResponse.addElement(response);
if (continuingCode != -1) {
/* we've seen a XXX- sequence */
if (code != continuingCode ||
(response.length() >= 4 && response.charAt(3) == '-')) {
continue;
} else {
/* seen the end of code sequence */
continuingCode = -1;
break;
}
} else if (response.length() >= 4 && response.charAt(3) == '-') {
continuingCode = code;
continue;
} else {
break;
}
}
}catch(Exception e){e.printStackTrace();}
if (debug) System.out.println("readServerResponse done");
return lastReplyCode = code;
}
/** Sends command cmd to the server. */
public void sendServer(String cmd) {
if (debug) System.out.println("sendServer start");
serverOutput.println(cmd);
if (debug) System.out.println("sendServer done");
}
/** Returns all server response strings. */
public String getResponseString() {
String s = new String();
for(int i = 0;i