![]() ![]() Upload the file to upload by means of a pathįile localFile = new File("C:/tmp/file.zip") Set the type of file to be transferred - binary typeĬtFileType(FTP.BINARY_FILE_TYPE) We connect in the passive mode to avoid always occupying the connection port 20 We make the relevant calls for successful logging Class that will help us make the connection We begin with the declaration of our variables With this we will finish this small tutorial, in the end we will only close the file stream and the connection so we ensure that we are not leaving anything open and we will be releasing memory of our JVM.Ģ.- Implementer codeThe next class ConnectAndCopyFTP.java explains the connection to our FTP server and the copy of a file. ![]() ![]() Which receives as parameter the name of the file that is going to have in the FTP server and the stream of the same, this returns us a boolean object that if it means true that it was successful. The interesting thing about this part is the following line clientFTP.storeFile(localFile.getName(), inputStream) is the one that loads the file. The following is to declare what kind of file is the one that we are going to transmit in our example will be a binary type tFileType(FTP.BINARY_FILE_TYPE), finishing making our connection with our parameters, we are ready to upload the file to our server, we will not occupy this tutorial to explain the use of the class File since we will only occupy it of a very simple form declaring our object towards the route. Once you have this successful step with the following line clientFTP.enterLocalPassiveMode() We make a connection passively so we do not always have the port occupied. We first connect with nnect(server, port) line, for later login with clientFTP.login(user, password). We will start with the body of the class, first we need to create the new FTPClient object, after this statement we have to put our code block in a try catch, this because for different reasons it can give an error, either by network, by wrong user or password, blocked port. The next thing we have to do is declare our variables that will help us connect, we can have variables from a file.properties, from database or from constants declared in a class, for example we will leave them in the same class As local variables of the main method, which are: server (URL of our FTP server), user (user name with which we are going to sign), password (password of the user) and port (connection port). The first import of the FTP class is to get a value of a declared constant that will have to do with the type of file to transfer, and the second FTPClient import will be the one that will make the whole connection and load the file. The classes that should be imported from our dependency downloaded into our example are as follows, import .ftp.FTP import .ftp.FTPClient. If it is a maven project it is necessary to add this dependency since otherwise our application will not be able to be executed, otherwise it should be added to the project path the following library commons-net-3.3.jar Download (JAR). Usually used in port 20 or 21, a disadvantage is that the channel is not 100% secure since since it is signed in the server until the transfer is in plain text, a possible solution is to use SFTP.ġ.- Maven dependenceThe first thing to do is to add the Apache Commons Net API dependency in the pom.xml (File Transfer Protocol) Network protocol based on the client-server model for file transfer, with this can be attached to a server to upload or download. The following is an example of how to send any type of file to an FTP server, this will be done using the Apache Commons Net API library since it is an API that implements basic internet protocols such as: In applications oriented to provide services there are processes in which you have to share an output that in most cases is a file, a suitable and controlled way of doing this is by placing the file on an FTP server, this is guaranteed That the download of the file can only be done with a user with privileges, to do this process you need a class to help us perform that task.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |