[置顶] 【FastDFS】maven项目使用FastDFS上传和读取图片
2017-06-14 15:28
543 查看
一、前言
在前一篇博客中,小编简单的向大家介绍了一下FastDFS的安装,把安装中会有的问题也简答说了一下。不过这些都是运维部门要做到,对于开发人员,我们只需要知道FastDFS的服务器地址就可以了,然后利用FastDFS提供的jar工具,来上传和读取操作。这篇博客,小编就向大家介绍一下这个。二、环境准备
FastDFS服务器fastdfs-client-java-master.zip
maven项目
Eclipse
三、具体操作
3.1 fastdfs-client安装到本地仓库
为了方便应用程序的访问FastDFS,官网提供了fastdfs-client-java,以便更好的与应用程序结合使用。下载fastdfs-client-java源码添加到项目工程里面
这个jar包在中央仓库是没有的,我们可以将源码下载下来,使用maven install安装到本地仓库。
3.2 添加标签
在我们需要的项目的pom.xml文件中,添加标签。<dependency> <groupId>org.csource</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27-SNAPSHOT</version> </dependency>
添加的标签要借鉴导入的fastdfs-client-java项目中的pom文件中的内容:
自己项目整理后的效果:
保存后,就会在项目中,添加引用:
四、FastDFS服务器的使用
使用方法:1、 把FastDFS提供的jar包添加到工程中
2、 初始化全局配置。加载一个配置文件。
3、 创建一个TrackerClient对象。
4、 创建一个TrackerServer对象。
5、 声明一个StorageServer对象,null。
6、 获得StorageClient对象。
7、直接调用StorageClient对象方法上传文件即可。
在使用之前,我们需要在配置文件中表明tracker的地址是什么,所以我们建立一个名为client.conf的配置文件:
tracker_server=192.168.137.11:22122
测试用例:
@Test public void testUpload() throws Exception { // 1、把FastDFS提供的jar包添加到工程中 // 2、初始化全局配置。加载一个配置文件。 ClientGlobal.init("D:\\workspaces-itcast\\JaveEE18\\taotao-manager\\taotao-manager-web\\src\\main\\resources\\properties\\client.conf"); // 3、创建一个TrackerClient对象。 TrackerClient trackerClient = new TrackerClient(); // 4、创建一个TrackerServer对象。 TrackerServer trackerServer = trackerClient.getConnection(); // 5、声明一个StorageServer对象,null。 StorageServer storageServer = null; // 6、获得StorageClient对象。 StorageClient storageClient = new StorageClient(trackerServer, storageServer); // 7、直接调用StorageClient对象方法上传文件即可。 String[] strings = storageClient.upload_file("D:\\Documents\\Pictures\\images\\2f2eb938943d.jpg", "jpg", null); for (String string : strings) { System.out.println(string); } }
在正式的项目中我们会封装一个专门操作FastDFS的使用类:
package com.taotao.fastdfs; 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:")) { String url = this.getClass().getResource("/").getPath(); url = url.substring(1); conf = conf.replace("classpath:", url); } ClientGlobal.init(conf); trackerClient = new TrackerClient(); trackerServer = trackerClient.getConnection(); storageServer = null; storageClient = new StorageClient1(trackerServer, storageServer); } public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception { return storageClient.upload_file1(fileName, extName, metas); } public String uploadFile(String fileName, String extName) throws Exception { return storageClient.upload_file1(fileName, extName, null); } public String uploadFile(String fileName) throws Exception { return storageClient.upload_file1(fileName, null, null); } public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception { return storageClient.upload_file1(fileContent, extName, metas); } public String uploadFile(byte[] fileContent, String extName) throws Exception { return storageClient.upload_file1(fileContent, extName, null); } public String uploadFile(byte[] fileContent) throws Exception { return storageClient.upload_file1(fileContent, null, null); } }
四、小结
通过对FastDFS的了解,如何导入相应的jar包,这个操作也是小编第一次使用,在maven项目中也有很好的操作。加油!相关文章推荐
- MongoDB篇——Java Maven项目中使用MongoDB GridFS上传音频图片
- 商城项目实战20:使用FastDFS-Client客户端进行上传图片的测试
- 百度富文本从下载使用到运行成功(运用在maven项目中,图片和视频上传到阿里云)
- springmvc中使用webuploader上传多张图片;maven中读取配置文件中的属性(路径)
- ionic 项目中使用ngCordova插件$cordovaCamera筛选手机图库图片显示出来并上传
- 使用asp.net将图片上传并存入SqlServer中,然后从SqlServer中读取并显示出来
- 09(maven+SSH)网上商城项目实战之使用jersey实现应用服务器和图片服务器分离
- Java-Maven项目引入UEditor图片上传组件jar包类库的5种方式
- 使用asp.net将图片上传并存入SqlServer中,然后从SqlServer中读取并显示出来
- 低版本net项目使用ueditor 图片上传配置
- fastdfs 新添加一个项目的图片目录 实现独立的上传下载功能
- 在YII项目中使用ckeditor和ckfinder快速部署文本编辑器并实现图片上传
- jsp、js限制本地上传图片大小、类型(js经过测试,项目中可以使用)
- [置顶] dubbo 分布式框架,git版本控制,ida使用方法,maven项目管理
- [置顶] IDEA下Maven项目中使用Mybatis及Mybatis Generator
- 使用Maven打包项目并上传到Linux服务器
- Java项目中使用FTP与SFTP上传图片
- 使用maven打包项目并上传到Linux服务器
- 在使用 AjaxFileUpload 上传文件时,在项目发布到 iis 后,图片不能预览
- Java-Maven项目引入UEditor图片上传组件jar包类库的5种方式