在项目中使用FastDfs的一些配置

1.在maven中引入FastDfs 的包



  org.csource.fastdfs
  fastdfs


  commons-fileupload
  commons-fileupload

2.由于文件上传是可以复用 的,所以创建一个工具类

public class FastDFSClient {

    private TrackerClient trackerClient = null;
    private TrackerServer trackerServer = null;
    private StorageClient1 storageClient1 =null;
    private StorageServer storageServer = null;

    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;
        storageClient1 = new StorageClient1(trackerServer,storageServer);
    }

    /**
     * 上传文件方法
     * 

Title: uploadFile

*

Description:

* @param fileName 文件全路径 * @param extName 文件扩展名,不包含(.) * @param metas 文件扩展信息 * @return * @throws Exception */ public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception{ String result = storageClient1.upload_file1(fileName, extName, metas); return result; } public String uploadFile(String fileName,String extName) throws Exception{ return uploadFile(fileName,extName,null); } public String uploadFile(String fileName) throws Exception{ return uploadFile(fileName,null,null); } /** * 上传文件方法 *

Title: uploadFile

*

Description:

* @param fileContent 文件的内容,字节数组 * @param extName 文件扩展名 * @param metas 文件扩展信息 * @return * @throws Exception */ public String uploadFile(byte[] fileContent,String extName,NameValuePair[] metas) throws Exception{ String result = storageClient1.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); } }

进行调用:

3.配置 web.xml, 写上文件上传 的配置:



   
   
   

4.配置application.properties 中 FastDfs 服务器IP

FILE_SERVER_URL=http://192.168.25.133/   ( FastDfs若是与dubbo 一起使用,虚拟机需要采用NAT 模式且要使用一个网段。例如 xxx.xxx.25. xx )

在项目中使用FastDfs的一些配置_第1张图片

5.加入fdf_client.conf 配置文件

# connect timeout in seconds
# default value is 30s
connect_timeout=30

# network timeout in seconds
# default value is 30s
network_timeout=60

# the base path to store log files
base_path=/home/fastdfs

# tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
tracker_server=192.168.25.133:22122

#standard log level as syslog, case insensitive, value list:
### emerg for emergency
### alert
### crit for critical
### error
### warn for warning
### notice
### info
### debug
log_level=info

# if use connection pool
# default value is false
# since V4.05
use_connection_pool = false

# connections whose the idle time exceeds this time will be closed
# unit: second
# default value is 3600
# since V4.05
connection_pool_max_idle_time = 3600

# if load FastDFS parameters from tracker server
# since V4.05
# default value is false
load_fdfs_parameters_from_tracker=false

# if use storage ID instead of IP address
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# default value is false
# since V4.05
use_storage_id = false

# specify storage ids filename, can use relative or absolute path
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# since V4.05
storage_ids_filename = storage_ids.conf


#HTTP settings
http.tracker_server_port=80

#use "#include" directive to include HTTP other settiongs
##include http.conf

6.Controller 的例子

@RestController
public class UploadController {

	@Value("${FILE_SERVER_URL}")
    //获取服务器IP
	private String file_server_url;

	@RequestMapping("/upload")
	public Result upload(MultipartFile file){

		String originalFilename = file.getOriginalFilename();//获取文件名
		String extName=originalFilename.substring( originalFilename.lastIndexOf(".")+1);//得到扩展名

		try {
			util.FastDFSClient client=new FastDFSClient("classpath:config/fdfs_client.conf");
			String fileId = client.uploadFile(file.getBytes(), extName);
			String url=file_server_url+fileId;//图片完整地址
			return new Result(true, url);

		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "上传失败");
		}

	}


}

7.页面,采用angular.js







controller:
//上传图片
	$scope.uploadFile=function(){
		uploadService.uploadFile().success(
			function(response){
				if(response.success){
					$scope.entity.pic= response.message;
				}else{
					alert(response.message);					
				}
			}		
		).error(
			function(){
				alert("上传出错");
			}	
		);	
	}



service:
//上传文件
	this.uploadFile=function(){
		var formdata=new FormData();
		formdata.append('file',file.files[0]);//file 文件上传框的name
		
		return $http({
			url:'../upload.do',		
			method:'post',
			data:formdata,
			headers:{ 'Content-Type':undefined },
			transformRequest: angular.identity			
		});
		
	}

 

你可能感兴趣的:(fastdfs)