fastdfs-client-java 文件上传
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 文件上传的更多相关文章
- 小兔Java教程 - 三分钟学会Java文件上传
今天群里正好有人问起了Java文件上传的事情,本来这是Java里面的知识点,而我目前最主要的精力还是放在了JS的部分.不过反正也不麻烦,我就专门开一贴来聊聊Java文件上传的基本实现方法吧. 话不多说 ...
- 2013第38周日Java文件上传下载收集思考
2013第38周日Java文件上传&下载收集思考 感觉文件上传及下载操作很常用,之前简单搜集过一些东西,没有及时学习总结,现在基本没啥印象了,今天就再次学习下,记录下自己目前知识背景下对该类问 ...
- java文件上传-原始的Servlet方式
前言: 干了这几个项目,也做过几次文件上传下载,要么是copy项目以前的代码,要么是百度的,虽然做出来了,但学习一下原理弄透彻还是很有必要的.刚出去转了一圈看周围有没有租房的,在北京出去找房子是心里感 ...
- JAVA文件上传 ServletFileUpLoad 实例
1. jsp <%@ page language="java" contentType="text/html" pageEncoding="u ...
- java文件上传工具包
java 文件上传工具包 主要有两个方法:单文件上传和多文件上传 @Slf4j public class UploadFileUtil { //上传单张图片 public String uploadP ...
- SpringBoot集成FastDFS依赖实现文件上传
前言 对FastDFS文件系统安装后的使用. FastDFS的安装请参考这篇:Docker中搭建FastDFS文件系统(多图) 本文环境:IDEA + JDK1.8 + Maven 本文项目代码:ht ...
- java文件上传Demo
说到文件上传我们要做到: 1.引入两个包:commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar 2.将form改为上传文件模式:enctype=" ...
- fastDFS与Java整合上传下载
由于项目需要整合个文件管理,选择使用的是fastDFS. 整合使用还是很方便的. 准备 下载fastdfs-client-java源码 源码地址 密码:s3sw 修改pom.xml 第一个plugin ...
- java文件上传-使用apache-fileupload组件
目前文件上传的(框架)组件:Apache----fileupload .Orialiy – COS – 2008() .Jsp-smart-upload – 200M. 用fileupload上传文件 ...
- java 文件上传
java 上传文件 如果不依赖框架的话 要利用 Apache 中几个jar文件来处理 1. 给表单设置enctype属性,其值为 "multipart/form-data" ...
随机推荐
- 2877: [Noi2012]魔幻棋盘 - BZOJ
DescriptionInput 第一行为两个正整数N,M,表示棋盘的大小. 第二行为两个正整数X,Y,表示棋盘守护者的位置. 第三行仅有一个正整数T,表示棋盘守护者将进行次操作. 接下来N行,每行有 ...
- 【转载】VMWare ESXi 5.0和vSphere Client安装和配置
免责声明: 本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除. 原文作者:张洪洋_ 原文地址:http://blog.sina.com.cn ...
- WinForm点击按钮在对应的panel里画图
panel在form1里,button在form1上方,panel在下面. 主要是在button1的click时间获取panel的画笔. 下面的不行,在panel里获取画笔,然后传到button1,根 ...
- poj 3170
两遍bfs ~ #include <cstdio> #include <cstdlib> //#include <cmath> #include <map&g ...
- 全球说:要给 OneAlert 点100个赞
客户背景 「全球说」 Talkmate,是北京酷语时代教育科技有限公司(酷语科技)旗下产品,酷语科技是一家诞生于中国的语言技术公司,致力于为全球用户提供一个全新的多语言学习和社交网络平台 . 全球说是 ...
- ZOJ 3261 Connections in Galaxy War(逆向并查集)
参考链接: http://www.cppblog.com/yuan1028/archive/2011/02/13/139990.html http://blog.csdn.net/roney_win/ ...
- crontab 指定执行用户
linux下可以通过配置crontab来定时执行任务,执行体可以是一条系统命令或自己写的一个脚本,同时可以指派用户来执行.配置crontab有两种方法.方法1.使用crontab命令,例如添加一个新的 ...
- HDU 1695 GCD (容斥原理+欧拉函数)
题目链接 题意 : 从[a,b]中找一个x,[c,d]中找一个y,要求GCD(x,y)= k.求满足这样条件的(x,y)的对数.(3,5)和(5,3)视为一组样例 . 思路 :要求满足GCD(x,y) ...
- POJ2251Dungeon Master
http://poj.org/problem?id=2251 题意 : 就是迷宫升级版,从以前的一个矩阵也就是一层,变为现在的L层," . "是可以走,但是“#”不可以走,从S走到 ...
- 重写equals()方法时,需要同时重写hashCode()方法
package com.wangzhu.map; import java.util.HashMap; /** * hashCode方法的主要作用是为了配合基于散列的集合一起正常运行,<br/&g ...