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" ...
随机推荐
- 【单调栈】Bzoj 1012: 最大数maxnumber
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 6255 Solved: 2676[Submi ...
- 【锋利的JQuery-学习笔记】菜单栏及其2级菜单
效果图: 鼠标移动到菜单项后如下: html: <div id="nav" class="mainNav"> <ul class=" ...
- GCD异步加载网络图片
//image dispatch_queue_t network_queue; network_queue = dispatch_queue_create("com.myapp.networ ...
- Unity3D Script Execution Order ——Question
我 知道 Monobehaviour 上的 那些 event functions 是 在主线程 中 按 顺序调用的.这点从Manual/ExecutionOrder.html 上的 一张图就可以看出来 ...
- PHP 性能分析与实验(二)——PHP 性能的微观分析
[编者按]此前,阅读过了很多关于 PHP 性能分析的文章,不过写的都是一条一条的规则,而且,这些规则并没有上下文,也没有明确的实验来体现出这些规则的优势,同时讨论的也侧重于一些语法要点.本文就改变 P ...
- POJ 1915
#include<iostream> #include<stdio.h> #define MAXN 350 #include"queue" using na ...
- http://jingyan.baidu.com/article/08b6a591f0fafc14a9092275.html
http://jingyan.baidu.com/article/08b6a591f0fafc14a9092275.html http://jingyan.baidu.com/article/414e ...
- URL中的特殊字符处理笔记
URL中的特殊字符 有些符号在URL中是不能直接传递的,如果要在URL中传递这些特殊符号,那么就要使用他们的编码了.编码的格式为:%加字符的ASCII码,即一个百分号%,后面跟对应字符的ASCII ...
- javaWEB邮件测试
新建一个工具类: Mail.java 该类的主要关键点是:1.设置系统属性.也就是你是用什么协议来进行邮件发送的,邮件协议有很多在种,比如impt,smpt,prop等协议, 我现在测试用的是smpt ...
- Xamarin.Android 入门之:Xamarin快速入门
一. 准备工作 1.新建一个项目取名为phoneword 2.在项目创建好之后,让我们展开“Resources”文件夹然后找到并打开该文件夹下的“layout”文件夹,双击main.axml在Andr ...