Hi, welcome,
FTPClient encapsulates all the functionality necessary to store and retrieve files from an FTP server. This class takes care of all low level details of interacting with an FTP server and provides a convenient higher level interface. As with all classes derived from
SocketClient
, you must first connect to the server with connect
before doing anything, and finally disconnect
after you're completely finished interacting with the server. Then you need to check the FTP reply code to see if the connection was successful.package UploadApplication;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
//To use FTP methods you have to download the following libraries
//Zip
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
- public class TestUpload {
- public static void main(String[] args) {
- String server = "RemoteServer.com";
- int port = 21;
- String user = "User";
- String pass = "password";
- FTPClient ftpClient = new FTPClient();
- try {
- ftpClient.connect(server, port);
- ftpClient.login(user, pass);
- ftpClient.enterLocalPassiveMode();
- ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
- File firstLocalFile = new File("PATHTOFOLDER");
- for (final File fileEntry : firstLocalFile.listFiles()) {
- if (fileEntry.isDirectory()) {
- //do your operatons;
- } else {
- String firstRemoteFile = fileEntry.getName();
- InputStream inputStream = new FileInputStream(fileEntry);
- System.out.println("Start uploading file-->"+fileEntry.getName());
- boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
- inputStream.close();
- if (done) {
- System.out.println("The first file is uploaded successfully.");
- }
- }
- }
- } catch (IOException ex) {
- System.out.println("Error: " + ex.getMessage());
- ex.printStackTrace();
- } finally {
- try {
- if (ftpClient.isConnected()) {
- ftpClient.logout();
- ftpClient.disconnect();
- }
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
- }
/* Steps to add libraries
-->Goto projects
-->right click on Libraries
-->Select Add jar file/folders
--->Browse to downloaded files and click ok
*/
***********************************************************************************************
Explanation for the client program
Line 3:Specify The Remote server address(URL) to which the file has to be uploaded.
Line 4:Default port number for FTP clients
Line 5:Specify User name
Line 6:Specify PASSWORD
Line 7: Creating instance of FTPClient in next three lines we will connect to the Server
Line 14: Specify the path of the folder from which the files will be selected and uploaded.
Line 15:Loop through each file present in the specified library
Upload each file with the same name.
Line 23:Uploads file to Remote server with the specified name.
*************************************************************************
Leave your doubts or suggestions in comments section...
how to put in a specific file in the server
ReplyDeleteBermuda yurtdışı kargo
ReplyDeleteBonaire yurtdışı kargo
Bolivya yurtdışı kargo
Birleşik Arap Emirlikleri yurtdışı kargo
Bhutanya yurtdışı kargo
JJ7B