1. Install Vsftpd service on 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 a user directory
sudo mkdir /home/uftp
5. Set user password
sudo passwd uftp
6. Set the /etc/vsftpd.conf configuration file
sudo vim /etc/vsftpd.conf
a. Modify the permissions to upload files:
There will be a problem here. By default, the default permission is 077, which means the corresponding 700 in Linux. The calculation rule is linux's permission 777 - local_umask
If the local_umask file permission is not set, the uploaded file will be unable to access it through the server (missing permissions)
b. Add at the end of the configuration file:
userlist_deny=NO userlist_enable=YES userlist_file=/etc/allowed_users seccomp_sandbox=NO local_enable=YES
c. Set the permissions for uploading files:
write_enable = YES
If you do not set it to YES, uploading files is not allowed by default.
7. Create /etc/allowed_users
Add allow tasks to be added to the file.
8. Restart the service
sudo service vsftpd restart
9. Use XFTP to test the connection
Use XFtp to create a new connection. Here you need to pay attention to the protocol ftp you selected. The port is 21. After filling in the account password, you can connect to our server.
Test upload file:
A txt file is uploaded here, and we can see that the permissions of the file are 666. This is mainly because we set the permissions after uploading the file local_umask.
2. Java FtpClient implements file upload
1. Maven dependency
<!-- Apache Tool Components--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> </dependency> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> </dependency>
2. Create a FtpClient object to remotely connect to the server
FTPClient ftp = new FTPClient(); //Link remote service ftp.connect("192.168.148.128", 21);The connection result can be viewed at debug:
3. FtpClient login server
ftp.login("uftp", "**"); //Return the login result status int reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return ; }We can check whether the user logged in successfully by judging replyCode and replyString!
4. Modify file upload path and file type
//Modify the upload file path ftp.changeWorkingDirectory("/home/uftp/images/");//Modify the file type ftp.setFileType(FTP.BINARY_FILE_TYPE);5. Upload files
//Get the input stream of uploaded files FileInputStream fileInputStream = new FileInputStream(new File("D:/123.jpg"));//Push the file to the server ftp.storeFile("hello.jpg", fileInputStream);After the upload file is completed, the upload is successful by checking the return result.
6. Log out
//Login ftp.logout();
The above steps are to use FtpClient to complete a file upload operation.
3. Use Nginx to view server pictures
1. nginx configuration file
Add image address resolution in server
location ~ .*/.(gif|jpg|jpeg|bmp|png|ico|txt)${ root /home/uftp/images; expires 7d; }2. Restart Nginx
./nginx -s reload
3. View pictures
http://192.168.148.128/hello.jpg
Summary: At this point, we completed a simple upload of the image to the server and displayed a small demo. The main problem encountered during the configuration process is about file permissions. If local_umask is not set in the configuration file in vsftpd, even if we successfully upload the file, we cannot use nginx to browse. Set write_enable = YES to ensure that the file can be uploaded.
The above is all the content of this article. I hope that the content of this article will be of some help to everyone’s study or work. I also hope to support Wulin.com more!