东郭1544 发表于 2016-6-7 10:37:17

JAVA FTP上传

java 代码

[*]package common.ftpOperate;   
[*]  
[*]import java.io.FileInputStream;   
[*]import java.io.IOException;   
[*]import java.io.InputStream;   
[*]import org.apache.commons.net.ftp.FTP;   
[*]import org.apache.commons.net.ftp.FTPClient;   
[*]import org.apache.commons.net.ftp.FTPConnectionClosedException;   
[*]import org.apache.commons.net.ftp.FTPReply;   
[*]public class FtpUpload {   
[*]    String hostName = "211.137.79.177";   
[*]  
[*]    String userName = "rbtdiy";   
[*]  
[*]    String password = "rbtdiy";   
[*]  
[*]    String remoteDir = "rbtdiy";   
[*]    String port = "rbtdiy";   
[*]  
[*]    public FtpUpload() {   
[*]  
[*]           
[*]  
[*]  
[*]            if (remoteDir == null || remoteDir.equalsIgnoreCase("")) {   
[*]  
[*]                remoteDir = null;   
[*]            }   
[*]  
[*]           
[*]    }   
[*]  
[*]    public boolean UploadFile(String localfilename, String remotefilename) {   
[*]        FTPClient ftp = new FTPClient();   
[*]        int reply;   
[*]        try {   
[*]            ftp.connect(hostName,Integer.parseInt(port));   
[*]            reply = ftp.getReplyCode();   
[*]            if (!FTPReply.isPositiveCompletion(reply)) {   
[*]                ftp.disconnect();   
[*]                return false;   
[*]            }   
[*]        } catch (IOException e) {   
[*]            if (ftp.isConnected()) {   
[*]                try {   
[*]                    ftp.disconnect();   
[*]                } catch (IOException f) {   
[*]                    return false;   
[*]                }   
[*]            }   
[*]        }   
[*]  
[*]        try {   
[*]            if (!ftp.login(userName, password)) {   
[*]                ftp.logout();   
[*]                return false;   
[*]            }   
[*]               
[*]            ftp.setFileType(FTP.BINARY_FILE_TYPE);   
[*]            //ftp.setFileType(FTP.ASCII_FILE_TYPE);   
[*]            ftp.enterLocalPassiveMode();   
[*]            //ftp.changeWorkingDirectory(remoteDir);   
[*]  
[*]            ftp.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);   
[*]            InputStream input = new FileInputStream(localfilename);   
[*]            if (input == null) {   
[*]                System.out.println("本地文件不存在");   
[*]  
[*]            }   
[*]            ftp.storeFile(remotefilename, input);   
[*]            input.close();   
[*]            ftp.logout();   
[*]        } catch (FTPConnectionClosedException e) {   
[*]            if (ftp.isConnected()) {   
[*]                try {   
[*]                    ftp.disconnect();   
[*]                } catch (IOException f) {   
[*]                    return false;   
[*]                }   
[*]            }   
[*]            return false;   
[*]        } catch (IOException e) {   
[*]            if (ftp.isConnected()) {   
[*]                try {   
[*]                    ftp.disconnect();   
[*]                } catch (IOException f) {   
[*]                    return false;   
[*]                }   
[*]            }   
[*]            return false;   
[*]        } finally {   
[*]            if (ftp.isConnected()) {   
[*]                try {   
[*]                    ftp.disconnect();   
[*]                } catch (IOException f) {   
[*]                    return false;   
[*]                }   
[*]            }   
[*]        }   
[*]        return true;   
[*]    }   
[*]  
[*]    public static void main(String args[]) throws Exception {   
[*]        FtpUpload ftpup = new FtpUpload();   
[*]        ftpup.UploadFile("c:/java2html.java", "java2html.java");       
[*]        }   
[*]           
[*]    }   
[*]}  
页: [1]
查看完整版本: JAVA FTP上传