SSM project practice: Implementing a simple network disk system (idea + reference source code)

1. Main functions of the project

1. Similar to Baidu Cloud, users can create new multi-layer folders in the network disk, and can upload files, download files, delete files, delete folders (all files in it will also be deleted)
2. Provide file classification function, Files can be divided into videos, pictures, music, etc. according to their suffix names.
3. Users can share files, and the shared files can be viewed and downloaded in the sharing area.

structure:

write picture description here

Effect:

write picture description here

write picture description here

write picture description here

Second, the data table design

MySQL database is used here.
In order to realize the function, we need three data tables: the data table
user_inf table that records user information, folder information and file information
write picture description here
Among them , status is used to record whether the user is a newly registered user

The dir_inf table
write picture description here
parent_dir is used to record the parent folder id, or null if the folder is in the root directory.
dir_user_id records the uploading user of the folder, which is a foreign key column and is associated with the user_id column of user_inf.
dir_path is used to record the absolute path of the folder, mainly for the convenience of file download.

The file_inf table
write picture description here
file_status records whether the file is public.
file_dir_id records the id of the folder to which the file belongs, which is a foreign key column and is associated with the dir_id column of the dir_inf table.
file_upload_user_id records the file upload user id, which is a foreign key column and is associated with the user_id column of the user_inf table.

3. Analysis of key points of the project:

1. Class settings

Mainly divided into three types: controller class, entity class and system tool class

(1) Controller class

Contains view controller class (responsible for managing the main URL and returning the JSP view file in WEB-INF), file manager class (mainly used for various operations on network disk data), login and registration classes (no more details)

(2) Entity class

The entity class corresponds to the above three tables: User class, CloudFile class, and Folder class. Each class should establish a corresponding Mapper interface and XML file.

(3) Tools

MyBatisUtil class: use the static initialization block to read the mybatis-config.xml configuration file, and provide a static method that returns SqlSessionFactory to avoid repeatedly reading the xml configuration file every time the database needs to be called (the singleton mode of Spring can also be used)
Others Tool class: record the string of the user file storage root directory, a static method for obtaining the system time, a static method for processing SQL statements containing special characters, and so on.

2. User registration and login

After the new user registers, insert data into the user_inf table and set the status to the new registration state. When logging in for the first time, the server creates a new folder in the directory where the user files are stored, and names it the user name of the user. When logging in, the user object needs to be stored in the session.

3. User file storage path:

You can create a new folder named UserData in the Tomcat root directory or other paths. Each folder in UserData corresponds to a user. middle.

4. File management JSP page:

In the view controller, you first need to obtain the user object in the session, find the id of the root directory according to the user object, find the folders and files in the root directory through the id value, encapsulate it into a List and store it in the request, and pass The loop label body prints out the form in a loop, and introduces the URL corresponding to all the following functions into the form, and the parameters are obtained by the EL expression. After the user enters a subfolder, the parent folder id needs to be recorded and stored in the request, so that the user can return to the previous directory.

5. User creates a new folder:

Define a URL specially used to create a new folder. The URL requires two parameters: the folder name and the parent folder id. The folder name is filled in by the user, and the parent folder id is obtained by the JSP page. Request attribute and hide it in the form middle. The front page and the background need to check whether the user has entered a folder name and whether the folder name is legal.
Background processing process:
(1) Find the corresponding parent folder information in the database according to the parent folder id and encapsulate it into an object.
(2) Create a new folder object, set the object's parent folder attributes, user attributes, etc.
(3) Obtain the absolute path attribute in the parent folder object, and create a new folder through the mkdirs() method of the File class
(4) Put the Insert new folder object into database

6. Upload files (support to select multiple files at the same time):

The front JSP page creates a form. The form element is a file upload box and supports multiple file selection. The parent folder ID is set by the request attribute obtained by the JSP page and hidden in the form. The background uses MultipartFile[] to receive files.
Background processing process:
(1) Check whether the user has selected a file
(2) Find the corresponding parent folder information in the database according to the parent folder id and encapsulate it into an object.
(3) Traverse the MultipartFile[] array, create a new file object, set the properties of the file object through methods such as getOriginalFilename provided by the MultipartFile interface and insert it into the file data table
(4) Obtain the absolute path through the parent folder object, which is provided through the MultipartFile interface The transferTo method creates the file on the server.
Deleting a file is similar to uploading a file, so I won't go into details here.

7. Delete the folder

The difficulty here is how to delete if it contains multiple subfolders and subfiles. My idea is to use SQL fuzzy query to complete: because there is an absolute path in the data table, so when searching for all subfiles in a folder The folder can be completed by like '${path}%', and then find the files in it by the found subfolder id, and delete it. For folders actually stored on the server, recursive deletion is required. For recursive deletion, you can refer to this blog: https://www.cnblogs.com/zhenyu-go/p/5554979.html .
The background only needs to receive one parameter: the id of the folder to be deleted.
The specific process:
(1) Find and encapsulate the folder object according to the target folder id
(2) First find the files under the folder through the folder id and delete (excluding files in subfolders)
(3 ) ) Fuzzy query the list of all subfolders through the absolute path of the folder
(4) Traverse the list, delete the folder and the files in it according to the id of the folder in the list element
(5) Delete the target folder
(6) Recursively delete the actual folder (cannot be deleted directly through the delete of the File class)

8. File sharing and file classification

File sharing: Set the status to public through the file id, and print out all files in a loop on the file sharing JSP page. This place is relatively simple
. File classification can be found by file suffix.

4. Reference source code for this example:

I am a freshman, the code may be a bit messy, please understand, if you have better ideas or suggestions, please leave a message.
Link: https://pan.baidu.com/s/1RipG9h6YY4OxM5JIB_kr3w Password: t1gb

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325979996&siteId=291194637