directory

Implementation strategy:

1. Upload files from the client to a certain location on the server using the Java upload technology

2. The server uploads files to a shared folder, which is easy to expand

3. Configure the nginx server

4. Location /0/ to achieve wireless extension

5. The larger the shared folder of the content system configured by root, the better

6. Files submitted by users The Java background bar saves the files to the root folder

7. When a shared folder is almost full, we create a new location

8. In this way, when users access the file server, they will automatically find their own library address


Implementation strategy:

1. Upload files from the client to a certain location on the server using the Java upload technology

package com.supermap.file;



import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.supermap.uitl.DateTimeUitl;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpSession;

/** * File upload class **@author yushen
 *
 */
@Controller
public class UploadController {
	
	/** * Create log ** /
    private static final Log LOGGER = LogFactory.getLog(UploadController.class);
    
    // Get the base file service path address (incoming server ID, server file service path) 0 represents the first file server
    @Value("${image.location.path}")
    private String Basicspath;	
    

	@Value("${image.location.library}")
	// Get the inventory address
	private String resourcelibrary;
	
	
    /** * File upload method **@param file
     * @param session
     * @return* /
    @RequestMapping("/Upload")
    @ResponseBody
    public String Upload(@RequestParam("file") MultipartFile file,HttpSession session) {
    	// Obtain the user ID through session to generate the user ID library path
        Integer user = (Integer) session.getAttribute("user");
        if(user == null) {
        	return "Upload failed, no user info!";
        }
        
    	// Log
    	LOGGER.info(new StringBuffer("User:+user)+"Upload files!");
    	
    	// The file is judged to be empty
    	if (file.isEmpty()) {
            return "Upload failed, please select file";
        }
    	
    	// Get the original file name
        String fileOriginalName = file.getOriginalFilename();
        // Set the file prefix
        String fliePrefix = "";
        // Set the file suffix
        String fileSuffix = "";
        
        // Verify file name security
        if(fileOriginalName.indexOf(".") != -1) {// The judge contains the file with the ending character
        	// Only JPG and PNG files can be uploaded
        	if(fileOriginalName.split("\ \.") [1].equals("png") || fileOriginalName.split("\ \.") [1].equals("jpg")) {
        		fliePrefix = UUID.randomUUID().toString().replace("-"."");
        		fileSuffix = fileOriginalName.split("\ \.") [1];
        	} else {
        		return "Upload failed, upload file type is not PNG and JPG!"; }}// The system sets the path
        String newfilePath = resourcelibrary + DateTimeUitl.nowTimeA +"/" + user + "/" ;
        // Unique service address
        String newfilePathOnly = Basicspath +newfilePath;
        
        
        // Check whether the address exists
        File fileUIS = new File(newfilePathOnly);  
        if(! fileUIS.exists()){// Create a folder if no folder exists
        	fileUIS.mkdirs();  
        } 
        String OnlyStr = newfilePathOnly + fliePrefix + "." + fileSuffix;
        // Set the address to which the file is loaded
        File dest = new File(OnlyStr);
        try {
            file.transferTo(dest);
            LOGGER.info("File."+fliePrefix + "." + fileSuffix+"Upload successful");
            return "Upload successful, file address:" + newfilePath+fliePrefix + "." + fileSuffix;
        } catch (IOException e) {
            LOGGER.error(e.toString(), e);
        }
        return "Upload failed!"; }}Copy the code

Configuration file application.properties

Path =f:/serverImagel/ # image.location.library=0/Copy the code

Change the address if it doesn’t fit in a file

Path =R:/serverImagel/ # image.location.library=1/Copy the code

 

2. The server uploads files to a shared folder, which is easy to expand

3. Configure the nginx server

server { listen 80; # set the static image server address location /0/ {# set the static image server address location /0/ {# set the static image server address location /0/ {# set the static image server address location /0/ {# for convenience after extending the file server library with a separate 0. }}Copy the code

4. Location /0/ to achieve wireless extension

5. The larger the shared folder of the content system configured by root, the better

6. Files submitted by users The Java background bar saves the files to the root folder

7. When a shared folder is almost full, we create a new location

Location /1/ {# for convenience after the extension file server does not have a library with a separate 0 # This address can be placed there as you wish it is best to use the shared disk mode, so that you can extend the root E:\serverImagel; }Copy the code

Of course, if the company’s user volume is particularly large, you can write a small program, according to the user’s file growth level to write a heartbeat monitoring

Let the code automatically modify the configuration file (one for Java, one for nginx) when a server drive is near full.

After each nginx increment is complete, the number of rows added last time is recorded, and the corresponding position of the number of rows to be added next time is calculated

This implements the Tracker.

 

8. In this way, when users access the file server, they will automatically find their own library address

Uploading files to the Web

<! DOCTYPEhtml>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="Width = device - width, initial - scale = 1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <form action="http://localhost:8080/2/serviceUpload" 
    enctype="multipart/form-data"  method="POST" >Uploading files:<input type="file" name="file"/><br>
        <input type="submit" value="Submit" />
    </form>
</body>
</html>
Copy the code

Visit address:

http://localhost/0/20181204/1000000001/e0973b2185094765af4d9054aa90f059.jpg

The domain name plus the address returned by the server, this address into their server database can be

Finally, data synchronization:

This is a disk backup and you can find it on the Internet

SyncToy, Microsoft’s official file backup software, supports 1) security and 2) stability

Comparison is Microsoft’s own software is much better than ordinary software if you don’t have other commonly used software can try this. There are many online tutorials to look at

 

 

 

 

Recently, the project needs a file server, but after looking at the file server in the market, I feel that the construction steps and API documents are too tedious to see, so I researched and built one myself. I feel that it is convenient to use by myself. If you are interested, we can talk about it together

qq:1251767927