According to URL, user name, password, upload files to FTP file server

1. According to initialize the URL, user name, password

public class FtpTools {
public static String hostname = null;
public static Integer Port = null;
public static String username = null;
public static String password = null;
public static void the init (String hostname1, Integer portl, String username1, String password1) {
// ftp server address
hostname = hostname1;
// ftp server port number defaults to 21
port = port1; // 21;
// ftp login account
username = username1; //
// ftp password
password = password1; //
}
public FTPClient ftpClient = null static;
/ **
* ftp server initialization
* /
public static void initFtpClient () {
ftpClient FTPClient new new = ();
ftpClient.setControlEncoding ( "UTF-. 8");
{the try
System.out.println ( "Connecting ... ftp server:" + hostname + ":" + Port);
ftpClient.connect (hostname, Port); // ftp server connected
ftpClient.login (username, password); / / login ftp server
; int replyCode = ftpClient.getReplyCode () // successful login server
(! FTPReply.isPositiveCompletion (replyCode)) iF {
System.out.println ( "Connect failed The ... ftp server:" + hostname + ": "Port +);
}
System.out.println (" Connect successfu ... FTP server: "+ hostname +": "+ Port);
} the catch (a MalformedURLException E) {
e.printStackTrace ();
} the catch (IOException E) {
e.printStackTrace ();
}
}

/ **
* Upload file
* @param pathname ftp service Save Address
* @param fileName uploaded to the ftp filename
name (absolute address) * @param originfilename files to be uploaded *
* @return
* /
public static boolean uploadFile (String pathname , String fileName, String originfilename) {
boolean Flag = false;
InputStream inputStream = null;
the try {
System.out.println ( "start upload file");
inputStream = new new FileInputStream (new new file (originfilename));
initFtpClient ();
ftpClient .setFileType (ftpClient.BINARY_FILE_TYPE);
CreateDirecroty (pathname);
ftpClient.makeDirectory (pathname);
ftpClient.changeWorkingDirectory (pathname);
ftpClient.storeFile (fileName, inputStream);
inputStream.close ();
ftpClient.logout ();
In Flag to true =;
System.out.println ( "successful upload files");
} the catch (Exception E) {
System.out.println ( "files that failed");
E .printStackTrace ();
} the finally {
IF (ftpClient.isConnected ()) {
the try {
ftpClient.disconnect ();
} the catch (IOException E) {
e.printStackTrace ();
}
}
IF (! inputStream = null) {
the try {
inputStream.close ();
} the catch (IOException E) {
e.printStackTrace ();
}
}
}
return to true;
}
/ **
* upload
* @param pathname ftp service save address
* @param fileName ftp uploads to the file name
* @Param inputStream input stream file
* @return
* /
public Boolean the uploadFile (pathname String, String fileName, the InputStream inputStream) {
Boolean = In Flag to false;
the try {
System.out.println ( "Start upload files");
initFtpClient ();
ftpClient.setFileType (ftpClient.BINARY_FILE_TYPE);
CreateDirecroty (pathname);
ftpClient.makeDirectory (pathname);
ftpClient.changeWorkingDirectory (pathname);
ftpClient.storeFile (fileName, inputStream);
inputStream.close ();
ftpClient.logout ();
In Flag to true =;
System.out.println ( "successful upload files");
} the catch (Exception E) {
System.out.println ( "files that failed");
e.printStackTrace ();
} {the finally
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOException e){
e.printStackTrace();
}
}
if(null != inputStream){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
//改变目录路径
public static boolean changeWorkingDirectory(String directory) {
boolean flag = true;
try {
flag = ftpClient.changeWorkingDirectory(directory);
if (flag) {
System.out.println("进入文件夹" + directory + " 成功!");

The else {}
System.out.println ( "into the folder" + directory + "start creating folders failed!");
}
} The catch (IOException IOE) {
ioe.printStackTrace ();
}
return Flag;
}

// create a multi-layer directory file, ftp server if the file already exists, is not created, if not, create
public static Boolean CreateDirecroty (String Remote) throws IOException {
Boolean = Success to true;
String = Remote Directory + "/" ;
// If the remote directory does not exist, create a remote server directories recursively
IF (directory.equalsIgnoreCase ( "/") && changeWorkingDirectory (new new String (directory))!!) {
int Start = 0;
int End = 0;
IF ( directory.startsWith ( "/")) {
Start =. 1;
} the else {
Start = 0;
}
End = directory.indexOf ( "/", Start);
String path = "";
String Paths = "";
the while (to true ) {
String = SubDirectory new new String (remote.substring (Start, End) .getBytes ( "GBK"), "ISO-8859-1");
path = path + "/" + subDirectory;
if (!existFile(path)) {
if (makeDirectory(subDirectory)) {
changeWorkingDirectory(subDirectory);
} else {
System.out.println("创建目录[" + subDirectory + "]失败");
changeWorkingDirectory(subDirectory);
}
} else {
changeWorkingDirectory(subDirectory);
}

+ = Paths Paths "/" + SubDirectory;
Start End = +. 1;
End = directory.indexOf ( "/", Start);
// Check whether the directory is created for all
IF (End <= Start) {
BREAK;
}
}
}
Success return;
}

// ftp server determines whether the file exists
public static Boolean existfile (String path) throws IOException {
Boolean = In Flag to false;
FTPFile [] = ftpFileArr ftpClient.listFiles (path);
IF (ftpFileArr.length> 0) {
In Flag = to true;
}
In Flag return;
}
// Create a directory
public static Boolean MakeDirectory (String the dir) {
Boolean = In Flag to true;
the try {
In Flag = ftpClient.makeDirectory (the dir);
IF (In Flag) {
System.out.println ( "Create folder" + dir + "success!");

The else {}
System.out.println ( "Create Folder" + dir + "failed!");
}
} The catch (Exception E) {
e.printStackTrace ();
}
return In Flag;
}

/ * ** * download file
* @param pathname FTP server directory files *
* @param filename file name *
* @param localpath path after the file download *
* @return * /
public Boolean the downloadFile (pathname String, String filename, String localPath) {
Boolean = in Flag to false;
the OutputStream = null OS;
the try {
System.out.println ( "start Download file");
initFtpClient ();
// switch FTP directory
ftpClient.changeWorkingDirectory (pathname);
FTPFile [] = ftpFiles ftpClient.listFiles ();
for (file FTPFile : ftpFiles) {
if(filename.equalsIgnoreCase(file.getName())){
File localFile = new File(localpath + "/" + file.getName());
os = new FileOutputStream(localFile);
ftpClient.retrieveFile(file.getName(), os);
os.close();
}
}
ftpClient.logout();
flag = true;
System.out.println("下载文件成功");
} catch (Exception e) {
System.out.println("下载文件失败");
e.printStackTrace();
} finally{
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOException e){
e.printStackTrace();
}
}
if(null != os){
try {
os.close();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
return Flag;
}

/ ** * Delete Files *
* @param pathname to save the FTP server directory *
* @param filename the file name to be deleted *
* @return * /
public static boolean deleteFile (String pathname, String filename) {
boolean Flag = false;
the try {
System.out.println ( "start deleting files");
initFtpClient ();
// switch the FTP directory
ftpClient.changeWorkingDirectory (pathname);
ftpClient.dele (filename);
ftpClient .logout ();
In Flag to true =;
System.out.println ( "file is removed successfully");
} the catch (Exception E) {
System.out.println ( "failed to delete file");
e.printStackTrace ();
} the finally {
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOException e){
e.printStackTrace();
}
}
}
return flag;
}
}

 

After initialization, you can directly call the operation corresponding to the FTP file server, delete the file or folder, create a folder, etc.

example:

First initialize:

FtpTools.init ( "###", 21, "###", "###"); // 21, the default port number is 21

File upload method call on ok

FtpTools.uploadFile("/HD/export",pdfFilePath,path+"\\"+ pdfFilePath);

 

Guess you like

Origin www.cnblogs.com/JavaYB/p/11978767.html