File Space -- Java DIY, filespacejavadiy

Source: Internet
Author: User
Tags md5 digest sql using

File Space -- Java DIY, filespacejavadiy
1 Overview

The File Space system is applicable to groups. It mainly provides storage and management services for File sharing and personal files. It can be divided into a personal file storage management platform and a team file sharing platform.

The Personal File storage platform provides related file classification storage services for individuals. After a user logs on to the Personal File storage platform, you can choose to add a new file, create a new category, and modify the original file, view related files and manage personal information.

The team space mainly presents the materials added to the personal part for sharing.

Individual users can browse shared content by the team, and then add their favorite materials to their favorites when they have logged on.

Projects are managed by semi-closed teams. Therefore, you can add projects as administrators.

For detailed system functions, see Figure 1-1:

 

Figure: 1-1

2. Requirement Analysis

 

2.1 Overview of system requirements

 

In today's era, sharing is a must for our teams. This system provides the team with file sharing. By providing users with a file storage management platform, it attracts users to upload files and then share uploaded files.

 

2.2 Case Analysis

 

The system includes common users and administrators. In addition, visitors can only view shared summary information. In the system, administrator users are inherited from common users, and management functions for system users are added on the basis of common users. Common users mainly have system login and logout, management of personal information, management of personal files, management of file categories, and management of favorites. In addition, the file downloading function is separately listed, you can download your own files or files in the shared area. For detailed use cases, see Figure 2-1 ).

 

Figure: 2-1

 

3 System Design

 

3.1 Technical Route

 

The Struts2 web application framework based on the Model-View-Controller (MVC) Model is used as a whole, and JSP is used as the View display to achieve frontend and backend separation. Data Storage Uses JDBC for persistent storage. The system uses the MYSQL database to store data and uses Tomcat7.0 as the system server. In terms of page representation, HTML5 standards and JQuery are comprehensively applied to increase the page effect. In addition, the system uses the Tomcat JDBC Pool that comes with Tomcat to increase data access performance.

 

3.2 overall System Architecture Design

 

The B/S web system is based on the MVC pattern. A four-layer architecture is designed for convenience of expansion: Page presentation layer (JSP), request processing and forwarding layer (Action), and Service layer (Service) and persistent storage layer (DAO ).

(Figure: 3-1) is the structural diagram of most B/S systems.

 

Figure: 3-1

3.3 Database Design

The database designs User tables, file tables, file type tables, and favorite tables according to system requirements. Each table indicates the basic attributes of the object, and the link in the figure indicates the foreign key relationship. Users have a one-to-many relationship with files, file types, and favorites. Files and favorites are also one-to-many relationships. The same file can be added to favorites by different users, however, each user can only add to favorites separately. In addition, the relationship between a file table and a type table is many-to-one. The line in Figure 3-2 shows the foreign key relationship.

 

Figure: 3-2

 

3.4 detailed system design

 

A. The page layout structure is shown in Figure 3-3:

 

Figure: 3-3

B. operations on a single page call the Jquery method and pass JSON data for partial refreshing.

C. To enter the homepage, You need to execute action to obtain the relevant file data. You can first enter a page and directly call the page.

D. use MD5 to encrypt the password.

E. Use the Interceptor to intercept the logon status before executing related actions.

F. Use the database connection pool to improve system performance.

G. encapsulate database operations using stored procedures.

H. Use Log4j to record operations at the DAO layer and handle errors.

I. Implement the logic reference function of the specific Action method.

 

 

4 system implementation

 

4 . 1 Paging Function implementation

The paging function uses the technology when the data displayed in the system is very large. The purpose is to allow the system to display the corresponding data records based on the page number and reduce the amount of data transferred, increase page access speed and improve user experience.

The paging Implementation defines the paging class (figure: 4-1). Then, you need to use the paging method to fill in the attribute values of the paging class to complete the paging.

 

 

 

Figure: 4-1 figure: 4-2

 

The paging class defines five attributes: the current page (currPage), the number of records displayed on each page (ItemOfPage ),

Total number of records (totalItem), total number of pages (totalPage), and corresponding page record set (items ).

The front-end data to be transmitted on the current page. If the front-end does not pass the page to be retrieved, the first page is displayed by default; the number of records displayed on each page is 10 by default, which cannot be modified by the user. The total number of records needs to be read from the database, to use pagination, you must first define the method for reading all the data in the database at the DAO layer, then call it at the service layer, and then enter it in the Pager object; the total number of pages must be calculated based on the total number of records and the number of lines displayed on each page, and then the results are filled in; finally, obtain the corresponding database records based on the attributes of the Pager object and enter the records in the Items list and pass them to the foreground for display.

Calculate the total number of pages:

TotalItem%Pager.GetItemOfPage() =0? (TotalItem/Pager.GetItemOfPage()):(TotalItem/Pager.GetItemOfPage() +1)

 

4.2 Implementation of the log function

Log4j is used for logs. First, import the log package (Figure 4-2) and configure the log4j. properties file (Figure 4-3 ). On the page where you want to add logs, use the method in the log4j package to obtain the log object and enter the corresponding log information.

Private static Logger logger=Logger.GetLogger(UserDaoImpl.Class);

 

 

Figure: 4-3

4.3 Implementation of MD5 encryption (password)

First, define the Encryption Class MD5 and the encryption static method toMD5 (), and then use MD5 to call the static method for encryption where encryption is needed.

The toMD5 () method uses the online blog tutorial to implement the following:

1 public final static String toMD5 (String s) {2 3 char hexDigits [] = {'0', '1', '2', '3', '4 ', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E ', 'F'}; 4 5 try {6 7 byte [] btInput = s. getBytes (); 8 9 // get the MessageDigest object of the MD5 Digest algorithm 10 11 MessageDigest mdInst = MessageDigest. getInstance ("MD5"); 12 13 // use the specified byte to update the summary 14 15 mdInst. update (btInput); 16 17 // obtain the ciphertext 18 19 byte [] md = mdInst. digest (); 20 21 // convert the ciphertext to a hexadecimal string in the form of 22 23 int j = md. length; 24 25 char str [] = new char [j * 2]; 26 27 int k = 0; 28 29 for (int I = 0; I <j; I ++) {30 31 byte byte0 = md [I]; 32 33 str [k ++] = hexDigits [byte0 >>> 4 & 0xf]; 34 35 str [k ++] = hexDigits [byte0 & 0xf]; 36 37} 38 39 return new String (str); 40 41} catch (Exception e) {42 43 e. printStackTrace (); 44 45 return null; 46 47} 48 49}
View Code 4.4 logon permission control implementation

The system's permission control is implemented through the interceptor.

First, define the interceptor and intercept it before executing the relevant Action. The implementation of the interceptor is inherited

MethodFilterInterceptor and rewrite the doIntercept method.

The key code is as follows:

1 HttpServletRequest request = ServletActionContext. getRequest (); 2 3 User user = (User) request. getSession (false ). getAttribute ("user"); 4 5 if (user = null) {6 7 // logger.info ("the user has not logged on! "); 8 9 return" re "; 10 11} else {12 13 // logger.info (" User Logon successful! "); 14 15 return invoker. invoke (); 16 17}
View Code

 

4.5 Implementation of Database Connection Pool

The database connection pool can effectively improve the speed of accessing the database.

This project uses Tomcat JDBC Pool that comes with Tomcat. You can configure the data source by adding context. xml to the project, and then obtain the database connection by obtaining the data source in context.

 

4.6 Implementation of the file upload function

The file upload front-end uses the input type of file, because the Chinese file name is difficult to transfer when downloading, so the name of the file to be uploaded must be an English name.

The background upload is implemented directly through

ServletActionContext.GetServletContext().GetRealPath("/Upload");

Obtain the File upload address (the upload folder under the project) and write the file to the server through the input/output stream of the file.

Finally, write the storage information related to the file to the database for download.

 

4.7 Implementation of the file downloading function

First, obtain the information about the file based on the id value of the file passed in the foreground, then define a file output stream and the related getter and setter methods, and determine the fileName and contentType, finally, configure the output result

<Result name ="Download"Type ="Stream">

<Param name ="InputName">FileDown</Param>

<Param name ="ContentDisposition">Attachment; filename = "$ {fileFileName }"</Param>

<Param name ="ContentType">FileContentType; charset = UTF-8</Param>

</Result>

 

4.8 Implementation of the file list view Function

The file list can be divided into two types: User-differentiated and user-independent lists, which correspond to the shared file display on the home page and Personal File display. The file display on the home page is large because of the large number of files, therefore, the file list in a personal file is displayed by page. Because the file category is required for each display, the number of files is not large and paging is not used.

Shared display of homepage files. For the pagination process, see the implementation of pagination.

The display of personal files is implemented by obtaining the list of related file objects through the user objects in the session and the id values of the file types passed in the foreground.

 

4.9 Database Design (User differentiation, file cascading deletion, and stored procedures)

For specific database design, see Database Design in system design.

Users are differentiated by adding a field in user attributes to determine whether the user is an administrator, and determining whether the user is an administrator or a common user based on the value of this field.

The file deletion function is not provided in the project.

When a user deletes a file type, the user deletes the file information based on the foreign key relationship. When the Administrator deletes the user, the files under the user will also be deleted.

 

Project Data Access uses stored procedures.

Use Navicat to define the stored procedure directly in the database, and then use

PrepareCall ("call XXX"); call this stored procedure to obtain data.

 

4.10 Implementation of the bread navigation function

The function of bread navigation is to tell visitors where they are currently on the website and how to return. When there is a typical single-line Layer 3 or above, using bread navigation can effectively improve user experience.

In this project, we use breadcrumb navigation to implement unlimited classification of file categories.

The page section of the project displays the information that is directly read from the Map set passed in the background.

The id (key) of all parent classes of the current category stored in Map and the corresponding category name (value ).

Because it needs to be stored in a certain order, Map uses LinkedHashMap.

The values in the key set in Map are implemented through the functions defined in the database.

This function obtains all the parent classes of the class by passing the id value of the current class and generates string output in a certain order.

This function is as follows:

 1 BEGIN 2  3 DECLARE sParentList varchar(1000); 4  5 DECLARE sParentTemp varchar(1000); 6  7 SET sParentTemp =cast(rootId as CHAR); 8  9 WHILE sParentTemp is not null DO10 11 IF (sParentList is not null) THEN12 13 SET sParentList = concat(sParentTemp,',',sParentList);14 15 ELSE16 17 SET sParentList = concat(sParentTemp);18 19 END IF;20 21 SELECT group_concat(parentid) INTO sParentTemp FROM tab_type where FIND_IN_SET(tid,sParentTemp)>0;22 23   END WHILE;24 25   RETURN sParentList;26 27 END
View Code

 

Source Code address: https://github.com/husky00/Java/tree/master/Java/FileSpace01

SQL address: http://files.cnblogs.com/files/husky/FileSpace.zip with mysql database, SQL using navicat Automatic Export!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.