FastDFS依賴jar包
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.1-RELEASE</version>
</dependency>
fdfs_client.conf
connect_timeout=30
network_timeout=60
base_path=/home/fastdfs
tracker_server=192.168.110.130:22122
log_level=info
use_connection_pool = false
connection_pool_max_idle_time = 3600
load_fdfs_parameters_from_tracker=false
use_storage_id = false
storage_ids_filename = storage_ids.conf
http.tracker_server_port=80
public class Test {
public static void main(String[] args) throws FileNotFoundException, IOException, Exception {
//加載配置文件 文件絕對路徑
ClientGlobal.init("D:\\eclipse\\ECLIProjects\\FastDFSdemo\\src\\main\\resources\\fdfs_client.conf");
//創建管理客戶端
TrackerClient trackerClient = new TrackerClient();
//創建管理服務端 通過管理客戶端構建
TrackerServer trackerServer = trackerClient.getConnection();
//創建存儲服務端
StorageServer storageServer=null;
//創建存儲客戶端 通過管理服務端和存儲服務端構建
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//開始上傳文件 文件絕對路徑 文件上傳后的擴展名 文件的擴展信息
String[] strs = storageClient.upload_file("E:\\user\\Pictures\\one.png", "png", null);
//打印file_id
for (String str : strs) {
System.out.println(str);
}
}
}
Console打印
group1
M00/00/00/wKgZhVyM91uAIh1UAABgkhgats8303.png
瀏覽器輸入 http://192.168.110.130/group1/M00/00/00/wKgZhVyM91uAIh1UAABgkhgats8303.png訪問圖片
文件上傳實例
把冗余操作封裝到工具類
public class FastDFSClient {
private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient1 storageClient = 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;
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);
}
}
config.properties
FILE_SERVER_URL=http://192.168.110.130/
@RestController
public class UploadController {
@Value("${FILE_SERVER_URL}")
private String fileServerUrl;
@RequestMapping("/upload")
public Result upload(MultipartFile file) {
//獲取文件名
String filename = file.getOriginalFilename();
//獲取文件后綴名 從最后一個點后一位截取
String extName = filename.substring(filename.lastIndexOf(".")+1);
try {
FastDFSClient client = new FastDFSClient("classpath:fdfs_client.conf");
String fileId = client.uploadFile(file.getBytes(), extName);
String url = fileServerUrl+fileId;//圖片完整地址
return new Result(true, url);
} catch (Exception e) {
e.printStackTrace();
return new Result(false, "上傳失敗");
}
}
}
class Result implements Serializable {
private boolean success;//是否成功
private String message;//返回信息
public Result(boolean success, String message) {
this.success = success;
this.message = message;
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var app = angular.module('appName', []);
app.service('uploadService', function($http){
//上傳文件
this.uploadFile=function(){
var formdata=new FormData();
//選擇文件上傳框name為file的第一個元素
formdata.append('file', file.files[0]);
//此處使用Angular請求 jQuery與之類似
return $http({
url:'../upload.do',
method:'post',
data:formdata,
headers:{'Content-Type':undefined},
transformRequest:angular.identity
});
}
});
app.controller('ctrlName', function($scope, uploadService){
$scope.imageEntity={};//初始化imageEntity
//上傳文件
$scope.uploadFile=function(){
uploadService.uploadFile().success(
function(response){
if (response.success) {
$scope.imageEntity.url=response.message
} else {
alert(response.message);
}
}
);
}
});
</script>
</head>
<body ng-app="appName" ng-controller="ctrlName">
<table>
<tr>
<td>圖片</td>
<td>
<table>
<tr>
<td>
<input type="file" id="file" />
<button type="button" ng-click="uploadFile()">
上傳
</button>
</td>
<td>
<img ng-src="{{imageEntity.url}}" width="200px" height="200px">
</td>
</tr>
</table>
</td>
</tr>
</table>
<script type="text/javascript">
var editor;
KindEditor.ready(function(K) {
editor = K.create('textarea[name="content"]', {
allowFileManager:true
});
});
</script>
</body>
本站文章版權歸原作者及原出處所有 。內容為作者個人觀點, 并不代表本站贊同其觀點和對其真實性負責,本站只提供參考并不構成任何投資及應用建議。本站是一個個人學習交流的平臺,網站上部分文章為轉載,并不用于任何商業目的,我們已經盡可能的對作者和來源進行了通告,但是能力有限或疏忽,造成漏登,請及時聯系我們,我們將根據著作權人的要求,立即更正或者刪除有關內容。本站擁有對此聲明的最終解釋權。