fastDFS是C语言编写的一款开源的分布式文件存储系统,我们可以使用fastDFS搭建一套高性能的文件服务器集群以满足文件上传和下载的需求
基本jar
commons-io.jar
commons-uploadfile.jar
1、form表单的提交方式一定时post
2、form表单 enctype的值一定是 multipart/form-data
3、form表单中一定有一个input的type类型是 file
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8">property>
<property name="maxUploadSize" value="5242880">property>
bean>
//文件上传方法
$scope.uploadfile=function(){
uploadService.uploadfile().success(function(response){
if(response.success){
//alert(response.message)
$scope.image.url= response.message;
}else{
alert(response.message);
}
})
}
this.uploadfile=function(){
var formData = new FormData();//FormData:html5中的对象,可以为我们封装文件数据
formData.append("file", file.files[0]);
return $http({
url:"../upload/uploadFile",
method:"post",
data:formData,
//headers:{'Content-Type':undefined},
//浏览器会帮我们把Content-Type设置为multipart/form-data;
headers:{'Content-Type':undefined},
//序列化文件对象
transformRequest:angular.identity
})
}
<input type="file" id="file" />
<button ng-click="uploadfile()" class="btn btn-primary" type="button" >
上传
button>
...
package utils;
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;
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);
}
/**
* 上传文件方法
* 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 = 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);
}
/**
* 上传文件方法
* 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 = 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);
}
}
package com.pinyougou.shop.controller;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.pinyougou.entity.Result;
import utils.FastDFSClient;
@RestController
@RequestMapping("/upload")
public class UpLoadController {
//动态的从application.properties配置文件中获取属性
@Value("${file_server_url}")
private String file_server_url;
@RequestMapping("/uploadFile")
public Result uploadFile(MultipartFile file) {
try {
FastDFSClient fastDFSClient = new FastDFSClient("classpath:config/fdfs_client.conf");
//获取文件真实姓名
String originalFilename = file.getOriginalFilename();
//获取文件扩展名
String extName = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
//上传文件
String uploadFile = fastDFSClient.uploadFile(file.getBytes(), extName);
return new Result(true, file_server_url+uploadFile);
} catch (Exception e){
e.printStackTrace();
return new Result(false, "上传失败");
}
}
}