Implementing file upload service with Java ftpclient

1、 Installing the vsftpd service for Ubuntu

1. Installation

sudo apt-get install vsftpd

2. Add user (uftp)

sudo useradd -d /home/uftp -s /bin/bash uftp

3. Set user password

sudo useradd -d /home/uftp -s /bin/bash uftp

4. Create user directory

sudo mkdir /home/uftp

5. Set user password

sudo passwd uftp

6. Set / etc / vsftpd Conf configuration file

sudo vim /etc/vsftpd. conf

a. Permission to modify uploaded files:

There will be a problem here. The default permission is 077 by default, that is, the 700 calculation rule in Linux is 777 - local_ umask

If local is not set_ Umask file permissions will cause the uploaded files to be inaccessible through the server (lack of permissions)

b. Add at the end of the profile:

c. Set file upload permission:

write_ enable = YES

If yes is not set here, uploading files is not allowed by default.

7. Create / etc / allowed_ users

Add allows tasks to be added to the file,

8. Restart service

sudo service vsftpd restart

9. Test connection using xftp

To create a new connection using xftp, you need to pay attention to the protocol FTP selected here. The port is 21. After filling in the account and password, you can connect to our server.

Test upload file:

A TXT file is uploaded here. We can see that the permission of the file is 666. This is mainly because we have set the permission local after uploading the file_ umask.

2、 Java ftpclient realizes file upload

1. Maven dependency

2. Create an ftpclient object to connect to the server remotely

The connection results can be seen in debug:

3. Ftpclient login server

We can judge replycode and replystring to see whether the user logs in successfully!

4. Modify file upload path and file type

5. Upload file

After uploading the file, judge whether the upload is successful by viewing the returned results

6. Log out

The above steps are to complete a file upload operation using ftpclient.

3、 View server images using nginx

1. Nginx profile

Add picture address resolution in server

2. Restart nginx

./ Nginx -s reload

3. View picture

http://192.168.148.128/hello.jpg

Summary: Here we have finished uploading a simple picture to the server and displaying a small demo. The main problem encountered in the configuration process is about file permissions. If the local in the configuration file in vsftpd is not set_ Umask, even if we upload the file successfully, we can't use nginx to browse and set write_ Enable = yes to ensure that the file can be uploaded.

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>