1. 程式人生 > >java springboot + nginx 自建 檔案叢集伺服器

java springboot + nginx 自建 檔案叢集伺服器

實現策略:

1.通過java 上傳技術實現檔案從客戶端上傳到伺服器某個位置

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;

/**
 * 檔案上傳類
 * 
 * @author yushen
 *
 */
@Controller
public class UploadController {
	
	/**
	 * 建立日誌
	 * 
	 */
    private static final Log LOGGER = LogFactory.getLog(UploadController.class);
    
    //獲取基礎檔案服務路徑地址(入伺服器id,伺服器檔案服務路徑)0代表第一個太檔案伺服器
    @Value("${image.location.path}")
    private String Basicspath;	
    

	@Value("${image.location.library}")
	//獲取庫存放地址
	private String resourcelibrary;
	
	
    /**
     * 檔案上傳方法
     * 
     * @param file
     * @param session
     * @return
     */
    @RequestMapping("/Upload")
    @ResponseBody
    public String Upload(@RequestParam("file") MultipartFile file,HttpSession session) {
    	//通過session 獲取使用者的id 生成使用者id庫路徑
        Integer user = (Integer) session.getAttribute("user");
        if(user == null) {
        	return "上傳失敗,沒有使用者資訊!";
        }
        
    	//記錄日誌
    	LOGGER.info(new StringBuffer("使用者:"+user)+"開始上傳檔案!");
    	
    	//檔案判斷為空
    	if (file.isEmpty()) {
            return "上傳失敗,請選擇檔案";
        }
    	
    	//獲取檔案原始名稱
        String fileOriginalName = file.getOriginalFilename();
        //設定檔案字首
        String fliePrefix = "";
        //設定檔案字尾
        String fileSuffix = "";
        
        //校驗檔名安全性
        if(fileOriginalName.indexOf(".") != -1) {//判斷事包含帶有結尾符的檔案
        	//只支援上傳jpg和png格式檔案圖片
        	if(fileOriginalName.split("\\.")[1].equals("png") || fileOriginalName.split("\\.")[1].equals("jpg")) {
        		fliePrefix = UUID.randomUUID().toString().replace("-", "");
        		fileSuffix = fileOriginalName.split("\\.")[1];
        	} else {
        		return "上傳失敗,上傳檔案型別不是png和jpg格式!";
        	}
        }
        
        //系統設定路徑
        String newfilePath = resourcelibrary + DateTimeUitl.nowTimeA +"/" + user + "/" ;
        //唯一服務地址
        String newfilePathOnly = Basicspath +newfilePath;
        
        
        //判斷地址是否存在
        File fileUIS = new File(newfilePathOnly);  
        if(!fileUIS.exists()){//如果不存在資料夾建立資料夾
        	fileUIS.mkdirs();  
        } 
        String OnlyStr = newfilePathOnly + fliePrefix + "." + fileSuffix;
        //設定檔案載入到的地址
        File dest = new File(OnlyStr);
        try {
            file.transferTo(dest);
            LOGGER.info("檔案:"+fliePrefix + "." + fileSuffix+"上傳成功");
            return "上傳成功,檔案地址:" + newfilePath+fliePrefix + "." + fileSuffix;
        } catch (IOException e) {
            LOGGER.error(e.toString(), e);
        }
        return "上傳失敗!";
    }

}

配置檔案 application.properties

#路徑/
image.location.path=f:/serverImagel/
#檔案存放庫地址
image.location.library=0/

一個檔案中放不下的時候就修改一下這個地址

#路徑/
image.location.path=R:/serverImagel/
#檔案存放庫地址
image.location.library=1/

 

2.伺服器上傳到的位置採用共享資料夾的方式實現,這樣易於擴充套件

3.通過nginx伺服器配置 

server {
			listen       80;
			#配置靜態圖片伺服器地址
			 location /0/ {#為方便之後擴充套件檔案伺服器沒一個庫用一個單獨的0開頭
				#這個地址放在那裡可以隨意最好採用共享磁碟的方式,這樣就可以無限擴充套件
				root   F:\serverImagel;
			   
			}
		}

4.location /0/的方式實現無線擴充套件

5.root 配置的內容系統的共享資料夾,越大越好

6.使用者提交檔案java 後臺吧檔案存放到 root後的資料夾中

7.當一個共享資料夾內部快放滿的時候,我們新建一個新的 location

location /1/ {#為方便之後擴充套件檔案伺服器沒一個庫用一個單獨的0開頭
				#這個地址放在那裡可以隨意最好採用共享磁碟的方式,這樣就可以無限擴充套件
				root   E:\serverImagel;
			   
			}

當然如果公司的使用者量特別大那麼可以寫個小程式,按照使用者的檔案增長水平寫一個心跳的監控

當發現伺服器某個碟符快滿的時候,就讓程式碼自動修改配置檔案 (一個修改java的,一個增加nginx的)

增加nginx的方式可以每次增加完畢後,記錄上次增加的行數,對應算出下一次本次需要增加行數的位置

這樣就實現了Tracker追蹤器。

 

8.這樣在使用者訪問檔案伺服器的時候回自動尋找自己的相簿地址

檔案上傳web

<!DOCTYPE html>
<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" >
        上傳檔案:<input type="file" name="file"/><br>
        <input type="submit" value="提交" />
    </form>
</body>
</html>

訪問地址:

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

域名加伺服器返回的地址,將這個地址存入到自己的伺服器資料庫中即可

最後就是資料同步:

這個就是磁碟備份從網上找找就好特別多

比如 微軟官方出品的檔案備份軟體 SyncToy  可以支援 1)安全2)穩定

比較是微軟自己的比起普通的軟體強了特別多大家如果沒別的常用軟體可以試試這個。網上教程也挺多的可以去瞧瞧

 

 

 

 

最近專案需要個檔案伺服器,但是看了下市場上的檔案伺服器,感覺搭建步驟看api文件等情況太繁瑣,不想看,於是就自己研究建了一個,感覺還是自己做出來的用的方便,有興趣的可以一起聊聊

qq:1251767927