FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。

导入jar包:fastdfs_client_v1.20.jar

配置文件:fdfs.conf

#fastDFS配置信息
charset = UTF-8
tracker_server = 139.196.152.232:22122
http.tracker_http_port = 80
connect_timeout = 5
network_timeout = 30
http.anti_steal_token = no
http.secret_key = FastDFS1234567890

源码:

接口

/**
*
*/
package com.bsh.common.service; import org.csource.common.NameValuePair;
import org.csource.fastdfs.FileInfo; /**
* <b>FastDFS文件存储服务接口</b>
*
*
*
*/
public interface FdfsFileService {
/**
* 上传文件(文件路径方式)
* @param file_path 文件路径
* @param file_ext_name 文件后缀
* @param meta_list 文件附属相关信息 (例如:图片的width:120、author:XXX等)
* @return url 文件url
*/
public String upload_file(String file_path,String file_ext_name, NameValuePair[] meta_list) throws Exception; /**
* 上传文件(字节流方式)
* @param file_path 文件路径
* @param file_ext_name 文件后缀
* @param meta_list 文件附属相关信息 (例如:图片的width:120、author:XXX等)
* @return url 文件url
*/
public String upload_file(byte file_buff[],String file_ext_name, NameValuePair[] meta_list) throws Exception; /**
* 上传文件-可修改(文件路径方式)
* @param file_path 文件路径
* @param file_ext_name 文件后缀
* @param meta_list 文件附属相关信息 (例如:图片的width:120、author:XXX等)
* @return url 文件url
*/
public String upload_appender_file(String file_path,String file_ext_name, NameValuePair[] meta_list) throws Exception; /**
* 上传文件-可修改(字节流方式)
* @param file_path 文件路径
* @param file_ext_name 文件后缀
* @param meta_list 文件附属相关信息 (例如:图片的width:120、author:XXX等)
* @return url 文件url
*/
public String upload_appender_file(byte file_buff[],String file_ext_name, NameValuePair[] meta_list) throws Exception; /**
* 修改可修改文件(appender上传)
* @param group_name
* @param remote_filename
* @param file_buff
* @return 0 修改成功
*/
public int modify_appender_file(String group_name, String remote_filename, byte file_buff[]) throws Exception; /**
* 下载文件
* @param group_name 文件组
* @param remote_filename 文件路径(上传文件时返回的值)
* @return 文件字节流
*/
public byte[] download_file(String remote_filename) throws Exception; /**
* 文件删除
* @param group_name 文件组
* @param remote_filename 文件路径
* @return 0 删除成功
*/
public int delete_file(String remote_filename) throws Exception; /**
* 文件信息
* @return FileInfo 文件存储信息
*/
public FileInfo get_file_info(String remote_filename) throws Exception; /**
* 文件附属相关信息
* @return NameValuePair 数组
*/
public NameValuePair[] get_metadata(String remote_filename) throws Exception; }

  

实现类

package com.bsh.common.service.impl;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient; import com.bsh.common.service.FdfsFileService; /**
* FastDFS文件存储服务实现
*
*
*/
public class FdfsFileServiceImpl implements FdfsFileService { private static FdfsFileServiceImpl service;
private String conf_file_path = "fdfs.conf";
private static final String GROUP = "allies"; private FdfsFileServiceImpl(){} /**
* Spring初始化服务
*/
public void init() {
service = this;
service.conf_file_path = this.conf_file_path;
try {
              //配置文件路径
String filePath = FdfsFileService.class.getResource("/").toURI().getPath() + service.conf_file_path;
ClientGlobal.init(filePath);
} catch (Exception e) {
System.err.println("FastDFS初始化失败");
e.printStackTrace();
}
} /**
* 初始化服务
*/
public static FdfsFileService getInstance(String conf_file_path) {
if (service == null) {
service = new FdfsFileServiceImpl();
}
service.conf_file_path = conf_file_path;
try {
ClientGlobal.init(FdfsFileService.class.getResource("/").getPath() + service.conf_file_path);
} catch (Exception e) {
System.err.println("FastDFS初始化失败");
e.printStackTrace();
}
return service;
} /**
* 获得客服端连接
*
* @return
* @throws Exception
*/
private StorageClient getClient() throws Exception {
return new StorageClient(new TrackerClient().getConnection(), null);
} @Override
public String upload_file(String file_path, String file_ext_name,
NameValuePair[] meta_list) throws Exception {
StorageClient client = null;
try {
client = service.getClient();
} catch (Exception e) {
throw new Exception("FastDFS获取连接失败", e);
}
String[] fileIds = null;
try {
fileIds = client.upload_file(file_path, file_ext_name, meta_list);
} catch (Exception e) {
throw new Exception("FastDFS文件上传失败", e);
}
return fileIds[1];
} @Override
public String upload_file(byte file_buff[], String file_ext_name,
NameValuePair[] meta_list) throws Exception {
StorageClient client = null;
try {
client = service.getClient();
} catch (Exception e) {
throw new Exception("FastDFS获取连接失败", e);
} String[] fileIds = null;
try {
fileIds = client.upload_appender_file(file_buff, file_ext_name,
meta_list);
} catch (Exception e) {
throw new Exception("FastDFS文件上传失败", e);
} return fileIds[1];
} @Override
public byte[] download_file(String remote_filename)
throws Exception {
StorageClient client = null;
try {
client = service.getClient();
} catch (Exception e) {
throw new Exception("FastDFS获取连接失败", e);
}
return client.download_file(GROUP, remote_filename);
} @Override
public int delete_file(String remote_filename)
throws Exception {
StorageClient client = null;
try {
client = service.getClient();
} catch (Exception e) {
throw new Exception("FastDFS获取连接失败", e);
}
return client.delete_file(GROUP, remote_filename);
} @Override
public FileInfo get_file_info(String remote_filename)
throws Exception {
StorageClient client = null;
try {
client = service.getClient();
} catch (Exception e) {
throw new Exception("FastDFS获取连接失败", e);
}
return client.get_file_info(GROUP, remote_filename);
} @Override
public NameValuePair[] get_metadata(String remote_filename) throws Exception {
StorageClient client = null;
try {
client = service.getClient();
} catch (Exception e) {
throw new Exception("FastDFS获取连接失败", e);
}
return client.get_metadata(GROUP, remote_filename); } public void setConf_file_path(String conf_file_path) {
this.conf_file_path = conf_file_path;
} @Override
public String upload_appender_file(String file_path,
String file_ext_name, NameValuePair[] meta_list) throws Exception {
StorageClient client = null;
try {
client = service.getClient();
} catch (Exception e) {
throw new Exception("FastDFS获取连接失败", e);
}
String[] fileIds = null;
try {
fileIds = client.upload_appender_file(file_path, file_ext_name, meta_list);
} catch (Exception e) {
throw new Exception("FastDFS文件上传失败", e);
}
return fileIds[1];
} @Override
public String upload_appender_file(byte[] file_buff,
String file_ext_name, NameValuePair[] meta_list) throws Exception {
StorageClient client = null;
try {
client = service.getClient();
} catch (Exception e) {
throw new Exception("FastDFS获取连接失败", e);
}
String[] fileIds = null;
try {
fileIds = client.upload_appender_file(file_buff, file_ext_name, meta_list);
} catch (Exception e) {
throw new Exception("FastDFS文件上传失败", e);
}
return fileIds[1];
} @Override
public int modify_appender_file(String group_name, String appdender_filename,
byte[] file_buff) throws Exception {
StorageClient client = null;
try {
client = service.getClient();
} catch (Exception e) {
throw new Exception("FastDFS获取连接失败", e);
}
return client.modify_file(group_name, appdender_filename, 0, file_buff);
} }

  

调用工具类

package com.bsh.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile; import com.bsh.common.service.FdfsFileService;
import cn.osworks.aos.core.asset.AOSPropertiesHandler; /**
* 文件上传工具
*
*/
@Component
@Lazy(false)
public class UploadUtils { static FdfsFileService fdfsFileService; public static FdfsFileService getFdfsFileService() {
return fdfsFileService;
} @Autowired
public void setFdfsFileService(FdfsFileService fdfs) {
fdfsFileService = fdfs;
} //dfs新方法
public static String save(MultipartFile file) {
String picUrl = "";
String[] extNameStr=file.getOriginalFilename().split("\\.");
String extName=extNameStr[1];
if (!file.isEmpty()) {
try {
picUrl = fdfsFileService.upload_file(file.getBytes(), extName, null);
                   //返回地址加上图片服务器域名
picUrl=AOSPropertiesHandler.getProperty("pic_server")+"/"+picUrl.split("/")[3];
} catch (Exception e) {
e.printStackTrace();
}
} else {
return picUrl;
}
return picUrl;
} }

  

fastdfs-client-java 文件上传的更多相关文章

  1. 小兔Java教程 - 三分钟学会Java文件上传

    今天群里正好有人问起了Java文件上传的事情,本来这是Java里面的知识点,而我目前最主要的精力还是放在了JS的部分.不过反正也不麻烦,我就专门开一贴来聊聊Java文件上传的基本实现方法吧. 话不多说 ...

  2. 2013第38周日Java文件上传下载收集思考

    2013第38周日Java文件上传&下载收集思考 感觉文件上传及下载操作很常用,之前简单搜集过一些东西,没有及时学习总结,现在基本没啥印象了,今天就再次学习下,记录下自己目前知识背景下对该类问 ...

  3. java文件上传-原始的Servlet方式

    前言: 干了这几个项目,也做过几次文件上传下载,要么是copy项目以前的代码,要么是百度的,虽然做出来了,但学习一下原理弄透彻还是很有必要的.刚出去转了一圈看周围有没有租房的,在北京出去找房子是心里感 ...

  4. JAVA文件上传 ServletFileUpLoad 实例

    1.  jsp <%@ page language="java" contentType="text/html" pageEncoding="u ...

  5. java文件上传工具包

    java 文件上传工具包 主要有两个方法:单文件上传和多文件上传 @Slf4j public class UploadFileUtil { //上传单张图片 public String uploadP ...

  6. SpringBoot集成FastDFS依赖实现文件上传

    前言 对FastDFS文件系统安装后的使用. FastDFS的安装请参考这篇:Docker中搭建FastDFS文件系统(多图) 本文环境:IDEA + JDK1.8 + Maven 本文项目代码:ht ...

  7. java文件上传Demo

    说到文件上传我们要做到: 1.引入两个包:commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar 2.将form改为上传文件模式:enctype=" ...

  8. fastDFS与Java整合上传下载

    由于项目需要整合个文件管理,选择使用的是fastDFS. 整合使用还是很方便的. 准备 下载fastdfs-client-java源码 源码地址 密码:s3sw 修改pom.xml 第一个plugin ...

  9. java文件上传-使用apache-fileupload组件

    目前文件上传的(框架)组件:Apache----fileupload .Orialiy – COS – 2008() .Jsp-smart-upload – 200M. 用fileupload上传文件 ...

  10. java 文件上传

    java 上传文件  如果不依赖框架的话  要利用 Apache 中几个jar文件来处理   1.  给表单设置enctype属性,其值为 "multipart/form-data" ...

随机推荐

  1. watch your tone

    老板要求邮件注意语气... 木想到混了这么久这种事情还要老板提醒

  2. OD鲜为人知的小技巧--搜索通配符(关键字)

    我看过一些OD教程,关于通配符这一点很少有人讲解(大概是我看的教程少吧)  近日通过看<黑客反汇编揭秘(第二版)>第165页了解到,原来OD还有这样方便的功能,那就是搜索通配符: Olly ...

  3. Directx3D SimpleSample Sample

    在d3d 2010 june这个版本里的samples 不知道为什么SimpleSample Sample 这个 它的documents基本等于没有 Starting point for new Di ...

  4. [nowCoder] 局部最小值位置

    定义局部最小的概念.arr长度为1时,arr[0]是局部最小.arr的长度为N(N>1)时,如果arr[0]<arr[1],那么arr[0]是局部最小:如果arr[N-1]<arr[ ...

  5. 跨线程调用控件之MethodInvoker

    原文:http://www.cnblogs.com/cm8448940/archive/2008/07/10/1240045.html 使用到两个控件,一个按钮button1,一个标签label1. ...

  6. linux网站推荐

    推荐几个Liux中文学些网站. http://www.chinaunix.net/http://linux.cn/http://www.linuxidc.com/

  7. Linux shell 脚本小记

    if结构 #!/bin/env bash -gt ] then echo "$1 is positive" -lt ] then echo "$1 is negative ...

  8. lintcode:插入区间

    题目: 插入区间 给出一个无重叠的按照区间起始端点排序的区间列表. 在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间). 样例 插入区间[2, 5] 到 [ ...

  9. springmvc环境的搭建

    最近应公司要求,用了2天时间学了springmvc的搭建,就简单总结一下: springmvc和struts2的比较,因为我是学过struts的,它们都是基于mvc模式而设计的web层框架 它们最大的 ...

  10. ARM CPU与Intel x86 CPU性能比较

    Qualcomm ARM CPU与Intel x86 CPU性能比较 随着移动互联网时代的到来,Qualcomm(高通).Texas Instruments(德州仪器)等基于ARM架构的CPU受到越来 ...