基于Fastdfs上传图片
前端代码:
<el-form-item prop="logo" label="店铺Logo">
<el-upload
class="upload-demo"
action="http://localhost:80/file/upload"
:on-preview="handlePreview"
:on-remove="handleRemove"
:on-error="handlerError"
:on-success="handleSuccess"
:file-list="fileList"
list-type="picture">
<el-button size="small" type="primary">点击上传</el-button>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>
</el-form-item>
return {
fileList: [],
},
前端方法:
//图片预览
handlePreview(file) {
console.log(file);
},
//文件删除
handleRemove(file, fileList) {
var filePath =file.response.resultObj;
this.$http.delete("/common/fastDfs/?path="+filePath)
.then(res=>{
if(res.data.success){
this.$message({
message: '删除成功!',
type: 'success'
});
}else{
this.$message({
message: '删除失败!',
type: 'error'
});
}
})
},
//文件上传失败回调
handlerError(err, file, fileList){
console.log(err);
},
//文件上传成功回调
handleSuccess(response, file, fileList){
this.shop.logo = response.message;
},
后台接口:
@RestController
@RequestMapping("/file")
public class FileController {
//读取yml文件
@Value("${pethome.fastdfs.host}")
private String fastDfsHost;
/**
* 文件上传
* 1:接收文件,获取全名并上传
* @param file
* @return
*/
@RequestMapping("/upload")
public JSONResult fileUpload(MultipartFile file) throws IOException {
//获取后缀
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
//上传文件
String filePath = fastDfsHost + FastdfsUtil.upload(file.getBytes(), extension);
return JSONResult.success(filePath);
}
}
工具类
public class FastdfsUtil {
//加载配置文件
public static String CONF_FILENAME = FastdfsUtil.class.getClassLoader() .getResource("fdfs_client.conf").getFile();
/**
* 上传文件
* @param file :byte数组格式的文件内容
* @param extName :文件扩展名
* @return
*/
public static String upload(byte[] file, String extName) {
try {
//初始化配置
ClientGlobal.init(CONF_FILENAME);
//创建Tracker客户端
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
//创建Storage 客户端
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//设置而外的参数
/**
NameValuePair nvp [] = new NameValuePair[]{
new NameValuePair("width", "100"),
new NameValuePair("height", "100")
}; **/
//执行上传
String fileIds[] = storageClient.upload_file(file,extName,null);
String path = "/"+fileIds[0]+"/"+fileIds[1];
System.out.println("文件上传成功,path="+path);
return path;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 上传文件
* @param extName
* @return
*/
public static String upload(String path,String extName) {
try {
ClientGlobal.init(CONF_FILENAME);
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
String fileIds[] = storageClient.upload_file(path, extName,null);
System.out.println(fileIds.length);
return "/"+fileIds[0]+"/"+fileIds[1];
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 下载文件
* @param groupName
* @param fileName
* @return
*/
public static byte[] download(String groupName,String fileName) {
try {
ClientGlobal.init(CONF_FILENAME);
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
return storageClient.download_file(groupName, fileName);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 删除文件
* @param groupName
* @param fileName
*/
public static boolean delete(String groupName,String fileName){
try {
ClientGlobal.init(CONF_FILENAME);
TrackerClient tracker = new TrackerClient();
TrackerServer trackerServer = tracker.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(trackerServer,
storageServer);
int i = storageClient.delete_file(groupName,fileName);
System.out.println( i==0 ? "删除成功" : "删除失败:"+i);
return i == 0;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("删除异常,"+e.getMessage());
}
}
}
yml配置
#文件服务器地址
pethome:
fastdfs:
host: "http://115.159.88.63"
依赖
<dependency>
<groupId>cn.bestwu</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27</version>
</dependency>