📄 jmyftpclient.java.bak
字号:
while(nTmpRead > 0 )
{
out.write(buf,0,nTmpRead );
nRead += nTmpRead;
nTmpRead = fSrc.read( buf );
}
fSrc.close();
// flush and clean up
out.flush();
out.close();
// and close the data socket
try
{
data.close();
}
catch (IOException ignore)
{
return -1;
}
tmode=-1;
// check the control response
String[] validCodes2 = {"226", "250"};
reply = control.readReply();
control.validateReply(reply, validCodes2);
return nRead;
}
/**
* Put data onto the FTP server. It
* is placed in the current directory.
*
* @param data array of bytes
* @param remoteFile name of remote file in
* current directory
*/
public void put(InputStream in, String remoteFile)
throws IOException, FTPException {
put(in, remoteFile, false);
}
/**
* Put data onto the FTP server. It
* is placed in the current directory. Allows
* appending if current file exists
*
* @param data array of bytes
* @param remoteFile name of remote file in
* current directory
* @param append true if appending, false otherwise
*/
public void put(InputStream in, String remoteFile, boolean append)
throws IOException, FTPException {
// get an output stream
data = control.createDataSocket();
DataOutputStream out = new DataOutputStream(data.getOutputStream());
// send the command to store
String cmd = append ? "APPE " : "STOR ";
String reply = control.sendCommand(cmd + remoteFile);
// Can get a 125 or a 150
String[] validCodes1 = {"125", "150"};
control.validateReply(reply, validCodes1);
byte[] buf=new byte[4096];
int len;
tmode=1;
tbytes=in.available();
sbytes=0;
// write stream
len=in.read(buf);
while(len>-1)
{
out.write(buf,0,len);
sbytes+=len;
len=in.read(buf);
}
// flush and clean up
out.flush();
out.close();
// and close the data socket
try {
data.close();
}
catch (IOException ignore) {}
tmode=-1;
// check the control response
String[] validCodes2 = {"226", "250"};
reply = control.readReply();
control.validateReply(reply, validCodes2);
}
/**
* GetFile
* */
public int get( String strSrcFile , String strDstFile ) throws IOException, FTPException
{
// get an input stream to read data from
data = control.createDataSocket();
DataInputStream in = new DataInputStream(data.getInputStream());
BufferedInputStream bIn = new BufferedInputStream(in);
RandomAccessFile fDst = new RandomAccessFile( strDstFile, "rw");
// send the retrieve command
String reply = control.sendCommand("RETR " + strSrcFile);
// Can get a 125 or a 150
String[] validCodes1 = {"125", "150"};
control.validateReply(reply, validCodes1);
// do the retrieving
int chunksize = 1024*32;
byte [] abyBuf = new byte[chunksize]; // read chunks into
int nRead = 0; // size of chunk read
int nTmpRead =0;
// read from socket & write to file
while (( nTmpRead = bIn.read( abyBuf , 0, chunksize)) >= 0)
{
fDst.write( abyBuf , 0 , nRead );
nRead += nTmpRead;
}
// close streams
try {
bIn.close();
data.close();
return -1;
}
catch (IOException ignore) {}
fDst.close();
// check the control response
String[] validCodes2 = {"226", "250"};
reply = control.readReply();
control.validateReply(reply, validCodes2);
return nRead;
}
/**
* Get data from the FTP server. Uses the currently
* set transfer mode. Retrieve as a byte array. Note
* that we may experience memory limitations as the
* entire file must be held in memory at one time.
*
* @param remoteFile name of remote file in
* current directory
*/
public byte[] get(String remoteFile)
throws IOException, FTPException {
// get an input stream to read data from
data = control.createDataSocket();
DataInputStream in = new DataInputStream(data.getInputStream());
BufferedInputStream bIn = new BufferedInputStream(in);
// send the retrieve command
String reply = control.sendCommand("RETR " + remoteFile);
// Can get a 125 or a 150
String[] validCodes1 = {"125", "150"};
control.validateReply(reply, validCodes1);
// do the retrieving
int chunksize = 4096;
byte [] chunk = new byte[chunksize]; // read chunks into
byte [] resultBuf = new byte[chunksize]; // where we place chunks
byte [] temp = null; // temp swap buffer
int count; // size of chunk read
int bufsize = 0; // size of resultBuf
// read from socket & write to file
while ((count = bIn.read(chunk, 0, chunksize)) >= 0) {
// new buffer to hold current buf + new chunk
temp = new byte[bufsize+count];
// copy current buf to temp
System.arraycopy(resultBuf, 0, temp, 0, bufsize);
// copy new chunk onto end of temp
System.arraycopy(chunk, 0, temp, bufsize, count);
// re-assign temp buffer to buf
resultBuf = temp;
// update size of buffer
bufsize += count;
}
// close streams
try {
bIn.close();
data.close();
}
catch (IOException ignore) {}
// check the control response
String[] validCodes2 = {"226", "250"};
reply = control.readReply();
control.validateReply(reply, validCodes2);
return resultBuf;
}
/**
* Run a site-specific command on the
* server. Support for commands is dependent
* on the server
*
* @param command the site command to run
* @return true if command ok, false if
* command not implemented
*/
public boolean site(String command)
throws IOException, FTPException {
// send the retrieve command
String reply = control.sendCommand("SITE " + command);
// Can get a 200 (ok) or 202 (not impl). Some
// FTP servers return 502 (not impl)
String[] validCodes = {"200", "202", "502"};
control.validateReply(reply, validCodes);
// return true or false? 200 is ok, 202/502 not
// implemented
if (reply.substring(0, 3).equals("200"))
return true;
else
return false;
}
/**
* List a directory's contents
*
* @param mask the file mask to use
*/
public String list(String mask)
throws IOException, FTPException {
return list(mask, false);
}
/**
* List a directory's contents
*
* @param mask the file mask to use
* @param full true if detailed listing required
* false otherwise
*/
public String list(String mask, boolean full)
throws IOException, FTPException {
// first set type to ascii if binary
boolean isBinary = false;
if (transferType.equals(FTPTransferType.BINARY)) {
isBinary = true;
setType(FTPTransferType.ASCII);
}
// get an input stream to read data from
data = control.createDataSocket();
InputStreamReader in = new InputStreamReader(data.getInputStream());
BufferedReader bIn = new BufferedReader(in);
// send the retrieve command
String command = full ? "LIST ":"NLST ";
String reply = control.sendCommand(command + mask);
// Can get a 125 or a 150
String[] validCodes1 = {"125", "150"};
control.validateReply(reply, validCodes1);
// get the listing ... this comes on the data channel
// do the retrieving
int chunksize = 4096;
char [] chunk = new char[chunksize]; // read chunks into
char [] resultBuf = new char[chunksize]; // where we place chunks
char [] temp = null; // temp swap buffer
int count; // size of chunk read
int bufsize = 0; // size of resultBuf
// read from socket & write to file
while ((count = bIn.read(chunk, 0, chunksize)) >= 0) {
// new buffer to hold current buf + new chunk
temp = new char[bufsize+count];
// copy current buf to temp
System.arraycopy(resultBuf, 0, temp, 0, bufsize);
// copy new chunk onto end of temp
System.arraycopy(chunk, 0, temp, bufsize, count);
// re-assign temp buffer to buf
resultBuf = temp;
// update size of buffer
bufsize += count;
}
// close streams. NOTE! For NLST we close our end before
// reading the reply! wu-ftpd requires this.
try {
bIn.close();
data.close();
}
catch (IOException ignore) {}
// check the control response
String[] validCodes2 = {"226", "250"};
reply = control.readReply();
control.validateReply(reply, validCodes2);
// reset type
if (isBinary)
setType(FTPTransferType.BINARY);
String result = null;
if (resultBuf.length > 0)
result = new String(resultBuf);
return result; // this is the String with the listing in it
}
/**
* Switch debug of responses on or off
*
* @param on true if you wish to have responses to
* stdout, false otherwise
*/
public void debugResponses(boolean on) {
control.debugResponses(on);
}
/**
* Get the current transfer type
*
* @return the current type of the transfer,
* i.e. BINARY or ASCII
*/
public FTPTransferType getType() {
return transferType;
}
/**
* Set the transfer type
*
* @param type the transfer type to
* set the server to
*/
public void setType(FTPTransferType type)
throws IOException, FTPException {
// determine the character to send
String typeStr = FTPTransferType.ASCII_CHAR;
if (type.equals(FTPTransferType.BINARY))
typeStr = FTPTransferType.BINARY_CHAR;
// send the command
String reply = control.sendCommand("TYPE " + typeStr);
control.validateReply(reply, "200");
// record the type
transferType = type;
}
/**
* Delete the specified remote file
*
* @param remoteFile name of remote file to
* delete
*/
public void delete(String remoteFile)
throws IOException, FTPException {
String reply = control.sendCommand("DELE " + remoteFile);
control.validateReply(reply, "250");
}
/**
* Rename a file or directory
*
* @param from name of file or directory to rename
* @param to intended name
*/
public void rename(String from, String to)
throws IOException, FTPException {
String reply = control.sendCommand("RNFR " + from);
control.validateReply(reply, "350");
reply = control.sendCommand("RNTO " + to);
control.validateReply(reply, "250");
}
/**
* Delete the specified remote working directory
*
* @param dir name of remote directory to
* delete
*/
public void rmdir(String dir)
throws IOException, FTPException {
String reply = control.sendCommand("RMD " + dir);
control.validateReply(reply, "250");
}
/**
* Create the specified remote working directory
*
* @param dir name of remote directory to
* create
*/
public void mkdir(String dir)
throws IOException, FTPException {
String reply = control.sendCommand("MKD " + dir);
control.validateReply(reply, "257");
}
/**
* Change the remote working directory to
* that supplied
*
* @param dir name of remote directory to
* change to
*/
public void chdir(String dir)
throws IOException, FTPException {
String reply = control.sendCommand("CWD " + dir);
control.validateReply(reply, "250");
}
/**
* Get the current remote working directory
*
* @return the current working directory
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -