项目中使用图片服务器FastDFS
来源:互联网 发布:淘宝贷款多久放款 编辑:程序博客网 时间:2024/06/02 04:50
图片服务器的搭建:
参考上一篇文章。
上传步骤:
1、加载配置文件,配置文件中的内容就是tracker服务的地址。
配置文件内容:tracker_server=192.168.25.133:22122
2、创建一个TrackerClient对象。直接new一个。
3、使用TrackerClient对象创建连接,获得一个TrackerServer对象。
4、创建一个StorageServer的引用,值为null
5、创建一个StorageClient对象,需要两个参数TrackerServer对象、StorageServer的引用
6、使用StorageClient对象上传图片。
7、返回数组。包含组名和图片的路径。
具体实现:
1.需要下载一个fastdfsClient项目1.25版本,导入项目,安装到maven仓库。
2.在manager-web项目的pom.xml中添加fastdfsClient的依赖。
3.在resources目录下创建client.conf文件
tracker_server=192.168.25.133:22122
4.编写一个测试类FastDFSTest 。
public class FastDFSTest {@Testpublic void testFileUpload() throws Exception {// 1、加载配置文件,配置文件中的内容就是tracker服务的地址。ClientGlobal.init("D:/workspaces-itcast/e3-manager-web/src/main/resources/resource/client.conf");// 2、创建一个TrackerClient对象。直接new一个。TrackerClient trackerClient = new TrackerClient();// 3、使用TrackerClient对象创建连接,获得一个TrackerServer对象。TrackerServer trackerServer = trackerClient.getConnection();// 4、创建一个StorageServer的引用,值为nullStorageServer storageServer = null;// 5、创建一个StorageClient对象,需要两个参数TrackerServer对象、StorageServer的引用StorageClient storageClient = new StorageClient(trackerServer, storageServer);// 6、使用StorageClient对象上传图片。//扩展名不带“.”String[] strings = storageClient.upload_file("D:/Documents/Pictures/images/200811281555127886.jpg", "jpg", null);// 7、返回数组。包含组名和图片的路径。for (String string : strings) {System.out.println(string);}}}
这里提供一个工具类简化代码:
FastDFSClient.java
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:")) {conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());}ClientGlobal.init(conf);trackerClient = new TrackerClient();trackerServer = trackerClient.getConnection();storageServer = null;storageClient = new StorageClient1(trackerServer, storageServer);}/** * 上传文件方法 * <p>Title: uploadFile</p> * <p>Description: </p> * @param fileName 文件全路径 * @param extName 文件扩展名,不包含(.) * @param metas 文件扩展信息 * @return * @throws Exception */public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {String result = storageClient.upload_file1(fileName, extName, metas);return result;}public String uploadFile(String fileName) throws Exception {return uploadFile(fileName, null, null);}public String uploadFile(String fileName, String extName) throws Exception {return uploadFile(fileName, extName, null);}/** * 上传文件方法 * <p>Title: uploadFile</p> * <p>Description: </p> * @param fileContent 文件的内容,字节数组 * @param extName 文件扩展名 * @param metas 文件扩展信息 * @return * @throws Exception */public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {String result = storageClient.upload_file1(fileContent, extName, metas);return result;}public String uploadFile(byte[] fileContent) throws Exception {return uploadFile(fileContent, null, null);}public String uploadFile(byte[] fileContent, String extName) throws Exception {return uploadFile(fileContent, extName, null);}}使用工具类上传:
@Testpublic void testFastDfsClient() throws Exception {FastDFSClient fastDFSClient = new FastDFSClient("D:/workspaces-itcast/e3-manager-web/src/main/resources/resource/client.conf");String file = fastDFSClient.uploadFile("D:/Documents/Pictures/images/2f2eb938943d.jpg");System.out.println(file);}
基于ssm框架的使用方法:
1.在manager-web项目中添加依赖
pom.xml
<dependencies><!-- 文件上传组件 --><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId></dependency></dependencies>2.配置springmvc.xml
<!-- 定义文件上传解析器 --><bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 设定默认编码 --><property name="defaultEncoding" value="UTF-8"></property><!-- 设定文件上传的最大值5MB,5*1024*1024 --><property name="maxUploadSize" value="5242880"></property></bean>3.在resource目录下创建conf目录,添加client.conf文件
client.conf
tracker_server=192.168.25.133:22122
前端使用的是:KindEditor的多图片上传插件。
KindEditor 4.x 文档http://kindeditor.net/doc.php
请求的url:/pic/upload
参数:MultiPartFile uploadFile
返回值:
//成功时{ "error" : 0, "url" : "http://www.xxxx.com/path/too/file.ext"}//失败时{ "error" : 1, "message" : "错误信息"}
4.controller编写:
@Controllerpublic class PictureController {@Value("${IMAGE_SERVER_URL}")private String IMAGE_SERVER_URL;@RequestMapping("/pic/upload")@ResponseBodypublic Map fileUpload(MultipartFile uploadFile) {try {//1、取文件的扩展名String originalFilename = uploadFile.getOriginalFilename();String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);//2、创建一个FastDFS的客户端FastDFSClient fastDFSClient = new FastDFSClient("classpath:resource/client.conf");//3、执行上传处理String path = fastDFSClient.uploadFile(uploadFile.getBytes(), extName);//4、拼接返回的url和ip地址,拼装成完整的urlString url = IMAGE_SERVER_URL + path;//5、返回mapMap result = new HashMap<>();result.put("error", 0);result.put("url", url);return result;} catch (Exception e) {e.printStackTrace();//5、返回mapMap result = new HashMap<>();result.put("error", 1);result.put("message", "图片上传失败");return result;}}}
这里很可能出现游览器不兼容问题,需要把返回值对象转换成json对象。
提供一个转换工具类。
JsonUtils.java
import java.util.List;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.JavaType;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;/** * */public class JsonUtils { // 定义jackson对象 private static final ObjectMapper MAPPER = new ObjectMapper(); /** * 将对象转换成json字符串。 * <p>Title: pojoToJson</p> * <p>Description: </p> * @param data * @return */ public static String objectToJson(Object data) { try {String string = MAPPER.writeValueAsString(data);return string;} catch (JsonProcessingException e) {e.printStackTrace();} return null; } /** * 将json结果集转化为对象 * * @param jsonData json数据 * @param clazz 对象中的object类型 * @return */ public static <T> T jsonToPojo(String jsonData, Class<T> beanType) { try { T t = MAPPER.readValue(jsonData, beanType); return t; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 将json数据转换成pojo对象list * <p>Title: jsonToList</p> * <p>Description: </p> * @param jsonData * @param beanType * @return */ public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) { JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType); try { List<T> list = MAPPER.readValue(jsonData, javaType); return list;} catch (Exception e) {e.printStackTrace();} return null; } }指定响应结果的content-type:
阅读全文
0 0
- 项目中使用图片服务器FastDFS
- 使用FastDFS搭建图片服务器
- FastDFS-图片服务器的使用
- 使用图片服务器FastDFS实现图片上传
- 【FastDFS】maven项目使用FastDFS上传和读取图片
- 图片上传服务器fastDFS
- 图片服务器fastDFS
- FastDFS 搭建图片服务器
- fastdfs 图片服务器 使用java端作为客户端上传图片 / 【FastDFS专题】fastdfs使用实战(概念篇)
- 使用FastDFS搭建图片服务器(单实例)
- 使用FastDFS搭建图片服务器单实例篇
- 使用FastDFS搭建图片服务器单实例篇
- 使用FastDFS搭建图片服务器单实例篇
- 使用FastDFS搭建图片服务器单实例篇
- FastDFS服务器的使用
- fastdfs图片服务器的搭建
- centos64位中 fastDFS的安装以及nginx反向代理实现搭建图片服务器(三)之图片服务器的使用
- 学习淘淘商城第十九课(搭建单机版FastDFS图片服务器以及使用FastDFS-Client客户端进行简单测试)
- SQL中ON和WHERE的区别
- CodeForce 897 C. Nephren gives a riddle
- 解决svn "cannot set LC_CTYPE locale"的问题
- unique_ptr
- zookeeper+activemq+集群消息中间件搭建
- 项目中使用图片服务器FastDFS
- mybatis中自建的和mybatis中xml的类型别名
- 《spring cloud微服务实战》读书笔记——Spring Cloud Hystrix(二)工作流程
- 用netsh命令实现快速切换网络状态(IP,DNS等)
- 对Python的见解
- 图形数据库Neo4J简介
- log4j的使用
- 个人关于使用IntentService的思考
- Java 中怎么打印数组?