1. 程式人生 > >(七)基於SSM+Redis+Nginx+FastDFS的部落格網站

(七)基於SSM+Redis+Nginx+FastDFS的部落格網站

 本篇介紹FastDFS,無需整合spring即可使用,前端上傳圖片到控制層,控制層呼叫fastDFS工具類實現上傳,上傳成功返回檔案地址到前端。

  • 配置檔案為fast_client.conf,fastDFS要求使用的檔名和屬性名,要一致。
    tracker_server = 120.78.191.143:22122
  • fastdfs相關連線資訊
    IMG_SERVER_IP=http://192.168.1.*/
    FASTDFS_CLIENT_FILE=resource/fdfs_client.conf
  • 控制層程式碼如下
    package com.tdrip.controller;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestPart;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.multipart.MultipartFile;
    
    import com.tdrip.model.db.OperatorModel;
    import com.tdrip.util.FastDFSClient;
    
    @RestController
    public class ImgUploadController {
    	
    	@Autowired
    	private HttpSession session;
    	@Value("${IMG_SERVER_IP}")
    	private String IMG_SERVER_IP;
    	@Value("${FASTDFS_CLIENT_FILE}")
    	private String FASTDFS_CLIENT_FILE;
    	
    	@RequestMapping("/imgupload")
    	public String imgUpload(HttpServletRequest request,@RequestPart(required = false, value = "file") MultipartFile file) {
    		OperatorModel model = (OperatorModel) session.getAttribute("admin");
    		if (null == model) {
    			return "";
    		}
    		StringBuffer picUrl = new StringBuffer(IMG_SERVER_IP);
    		try {
    			//獲得原始檔名
    			String oldName = file.getOriginalFilename();
    			//獲得副檔名
    			String extName = oldName.substring(oldName.lastIndexOf(".")+ 1);
    			//配置檔案,指定tracker_server的IP地址
    			String conf = "classpath:" + FASTDFS_CLIENT_FILE;
    			//建立FastDFS物件
    			FastDFSClient fastDFSClient = new FastDFSClient(conf);
    			//檔案路徑
    			picUrl.append(fastDFSClient.uploadFile(file.getBytes(), extName));
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		//返回檔案路徑
    		return picUrl.toString();
    	}
    }
    

  • FastDFS工具類
    package com.tdrip.util;
    
    import org.csource.common.NameValuePair;
    import org.csource.fastdfs.ClientGlobal;
    import org.csource.fastdfs.StorageClient1;
    import org.csource.fastdfs.StorageServer;
    import org.csource.fastdfs.TrackerClient;
    import org.csource.fastdfs.TrackerServer;
    
    public class FastDFSClient {
    
    	private TrackerClient trackerClient = null;
    	private TrackerServer trackerServer = null;
    	private StorageServer storageServer = null;
    	private StorageClient1 storageClient = null;
    	
    	/**
    	 * conf為配置檔案的路徑,配置檔案為fastdfs伺服器的ip地址和埠
    	 * @param conf
    	 * @throws Exception
    	 */
    	public FastDFSClient(String conf) throws Exception {
    		if (conf.contains("classpath:")) {
    			conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
    		}
    		ClientGlobal.init(conf);
    		trackerClient = new TrackerClient();
    		trackerServer = trackerClient.getConnection();
    		storageServer = null;
    		storageClient = new StorageClient1(trackerServer, storageServer);
    	}
    	
    	/**
    	 * 上傳檔案方法
    	 * <p>Title: uploadFile</p>
    	 * <p>Description: </p>
    	 * @param fileName 檔案全路徑
    	 * @param extName 副檔名,不包含(.)
    	 * @param metas 檔案擴充套件資訊
    	 * @return
    	 * @throws Exception
    	 */
    	public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
    		String result = storageClient.upload_file1(fileName, extName, metas);
    		return result;
    	}
    	
    	public String uploadFile(String fileName) throws Exception {
    		return uploadFile(fileName, null, null);
    	}
    	
    	public String uploadFile(String fileName, String extName) throws Exception {
    		return uploadFile(fileName, extName, null);
    	}
    	
    	/**
    	 * 上傳檔案方法
    	 * <p>Title: uploadFile</p>
    	 * <p>Description: </p>
    	 * @param fileContent 檔案的內容,位元組陣列
    	 * @param extName 副檔名
    	 * @param metas 檔案擴充套件資訊
    	 * @return
    	 * @throws Exception
    	 */
    	public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {
    		
    		String result = storageClient.upload_file1(fileContent, extName, metas);
    		return result;
    	}
    	
    	public String uploadFile(byte[] fileContent) throws Exception {
    		return uploadFile(fileContent, null, null);
    	}
    	
    	public String uploadFile(byte[] fileContent, String extName) throws Exception {
    		return uploadFile(fileContent, extName, null);
    	}
    }