?? ftpconnection.java
字號:
try {
clientAddr = InetAddress.getByName(dataSrvName);
}
catch(UnknownHostException ex) {
out.write(mFtpStatus.getResponse(553, request, mUser, null));
return;
}
// get data server port
int clientPort = 0;
try {
int hi = Integer.parseInt(st.nextToken());
int lo = Integer.parseInt(st.nextToken());
clientPort = (hi << 8) | lo;
}
catch(NumberFormatException ex) {
out.write(mFtpStatus.getResponse(552, request, mUser, null));
return;
}
mDataConnection.setPortCommand(clientAddr, clientPort);
out.write(mFtpStatus.getResponse(200, request, mUser, null));
}
/**
* <code>PWD <CRLF></code><br>
*
* This command causes the name of the current working
* directory to be returned in the reply.
*/
public void doPWD(FtpRequest request, FtpWriter out) throws IOException {
resetState();
String args[] = {mUser.getVirtualDirectory().getCurrentDirectory()};
out.write(mFtpStatus.getResponse(257, request, mUser, args));
}
/**
* <code>QUIT <CRLF></code><br>
*
* This command terminates a USER and if file transfer is not
* in progress, the server closes the control connection.
*/
public void doQUIT(FtpRequest request, FtpWriter out) throws IOException {
resetState();
out.write(mFtpStatus.getResponse(221, request, mUser, null));
ConnectionService conService = mConfig.getConnectionService();
if (conService != null) {
conService.closeConnection(mUser.getSessionId());
}
}
/**
* <code>REST <SP> <marker> <CRLF></code><br>
*
* The argument field represents the server marker at which
* file transfer is to be restarted. This command does not
* cause file transfer but skips over the file to the specified
* data checkpoint. This command shall be immediately followed
* by the appropriate FTP service command which shall cause
* file transfer to resume.
*/
public void doREST(FtpRequest request, FtpWriter out) throws IOException {
// argument check
if(!request.hasArgument()) {
out.write(mFtpStatus.getResponse(501, request, mUser, null));
return;
}
resetState();
mlSkipLen = 0;
String skipNum = request.getArgument();
try {
mlSkipLen = Long.parseLong(skipNum);
}
catch(NumberFormatException ex) {
out.write(mFtpStatus.getResponse(501, request, mUser, null));
return;
}
if(mlSkipLen < 0) {
mlSkipLen = 0;
out.write(mFtpStatus.getResponse(501, request, mUser, null));
return;
}
mbReset = true;
out.write(mFtpStatus.getResponse(350, request, mUser, null));
}
/**
* <code>RETR <SP> <pathname> <CRLF></code><br>
*
* This command causes the server-DTP to transfer a copy of the
* file, specified in the pathname, to the server- or user-DTP
* at the other end of the data connection. The status and
* contents of the file at the server site shall be unaffected.
*/
public void doRETR(FtpRequest request, FtpWriter out) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
// set state variables
long skipLen = (mbReset) ? mlSkipLen : 0;
resetState();
// argument check
if(!request.hasArgument()) {
out.write(mFtpStatus.getResponse(501, request, mUser, null));
return;
}
// get filename
String fileName = request.getArgument();
fileName = mUser.getVirtualDirectory().getAbsoluteName(fileName);
String physicalName = mUser.getVirtualDirectory().getPhysicalName(fileName);
File requestedFile = new File(physicalName);
String args[] = {fileName};
// check file existance
if( !(requestedFile.exists() && requestedFile.isFile()) ) {
out.write(mFtpStatus.getResponse(550, request, mUser, args));
return;
}
// check permission
if(!mUser.getVirtualDirectory().hasReadPermission(physicalName, true)) {
out.write(mFtpStatus.getResponse(550, request, mUser, args));
return;
}
// now transfer file data
out.write(mFtpStatus.getResponse(150, request, mUser, null));
Socket dataSoc = mDataConnection.getDataSocket();
if (dataSoc == null) {
out.write(mFtpStatus.getResponse(550, request, mUser, args));
return;
}
os = mUser.getOutputStream(dataSoc.getOutputStream());
// move to the appropriate offset
RandomAccessFile raf = new RandomAccessFile(requestedFile, "r");
raf.seek(skipLen);
is = new FileInputStream(raf.getFD());
// send file data to client
StreamConnector msc = new StreamConnector(is, os);
msc.setMaxTransferRate(mUser.getMaxDownloadRate());
msc.setObserver(this);
msc.connect();
if(msc.hasException()) {
out.write(mFtpStatus.getResponse(451, request, mUser, args));
return;
}
else {
mConfig.getStatistics().setDownload(requestedFile, mUser, msc.getTransferredSize());
}
out.write(mFtpStatus.getResponse(226, request, mUser, null));
}
catch(IOException ex) {
out.write(mFtpStatus.getResponse(425, request, mUser, null));
}
finally {
IoUtils.close(is);
IoUtils.close(os);
mDataConnection.closeDataSocket();
}
}
/**
* <code>RMD <SP> <pathname> <CRLF></code><br>
*
* This command causes the directory specified in the pathname
* to be removed as a directory (if the pathname is absolute)
* or as a subdirectory of the current working directory (if
* the pathname is relative).
*/
public void doRMD(FtpRequest request, FtpWriter out) throws IOException {
// reset state variables
resetState();
// argument check
if(!request.hasArgument()) {
out.write(mFtpStatus.getResponse(501, request, mUser, null));
return;
}
// get file names
String fileName = request.getArgument();
fileName = mUser.getVirtualDirectory().getAbsoluteName(fileName);
String physicalName = mUser.getVirtualDirectory().getPhysicalName(fileName);
File requestedFile = new File(physicalName);
String args[] = {fileName};
// check permission
if(!mUser.getVirtualDirectory().hasWritePermission(physicalName, true)) {
out.write(mFtpStatus.getResponse(450, request, mUser, args));
return;
}
// now delete
if(requestedFile.delete()) {
out.write(mFtpStatus.getResponse(250, request, mUser, args));
}
else {
out.write(mFtpStatus.getResponse(450, request, mUser, args));
}
}
/**
* <code>RNFR <SP> <pathname> <CRLF></code><br>
*
* This command specifies the old pathname of the file which is
* to be renamed. This command must be immediately followed by
* a "rename to" command specifying the new file pathname.
*/
public void doRNFR(FtpRequest request, FtpWriter out) throws IOException {
// reset state variable
resetState();
// argument check
if(!request.hasArgument()) {
out.write(mFtpStatus.getResponse(501, request, mUser, null));
return;
}
// set state variable
mbRenFr = true;
// get filename
String fileName = request.getArgument();
fileName = mUser.getVirtualDirectory().getAbsoluteName(fileName);
mstRenFr = mUser.getVirtualDirectory().getPhysicalName(fileName);
String args[] = {fileName};
out.write(mFtpStatus.getResponse(350, request, mUser, args));
}
/**
* <code>RNTO <SP> <pathname> <CRLF></code><br>
*
* This command specifies the new pathname of the file
* specified in the immediately preceding "rename from"
* command. Together the two commands cause a file to be
* renamed.
*/
public void doRNTO(FtpRequest request, FtpWriter out) throws IOException {
// argument check
if(!request.hasArgument()) {
resetState();
out.write(mFtpStatus.getResponse(501, request, mUser, null));
return;
}
// set state variables
if((!mbRenFr) || (mstRenFr == null)) {
resetState();
out.write(mFtpStatus.getResponse(100, request, mUser, null));
return;
}
// get filenames
String fromFileStr = mUser.getVirtualDirectory().getVirtualName(mstRenFr);
String toFileStr = request.getArgument();
toFileStr = mUser.getVirtualDirectory().getAbsoluteName(toFileStr);
String physicalToFileStr = mUser.getVirtualDirectory().getPhysicalName(toFileStr);
File fromFile = new File(mstRenFr);
File toFile = new File(physicalToFileStr);
String args[] = {fromFileStr, toFileStr};
resetState();
// check permission
if(!mUser.getVirtualDirectory().hasCreatePermission(physicalToFileStr, true)) {
out.write(mFtpStatus.getResponse(553, request, mUser, null));
return;
}
// now rename
if(fromFile.renameTo(toFile)) {
out.write(mFtpStatus.getResponse(250, request, mUser, args));
}
else {
out.write(mFtpStatus.getResponse(553, request, mUser, args));
}
}
/**
* <code>SITE <SP> <string> <CRLF></code><br>
*
* This command is used by the server to provide services
* specific to his system that are essential to file transfer
* but not sufficiently universal to be included as commands in
* the protocol.
*/
public void doSITE(FtpRequest request, FtpWriter out) throws IOException {
resetState();
SiteCommandHandler siteCmd = new SiteCommandHandler( mConfig, mUser );
out.write( siteCmd.getResponse(request) );
}
/**
* <code>SIZE <SP> <pathname> <CRLF></code><br>
*
* Returns the size of the file in bytes.
*/
public void doSIZE(FtpRequest request, FtpWriter out) throws IOException {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -