使用FastDfs上传图片
搭建服务器
引入fastDfs客户端的项目
调用代码:
工具类:
代码语言:javascript复制package cn.e3mall.common.util;
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);
}
}
测试类:
代码语言:javascript复制package cn.e3mall.fast;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.junit.Test;
import cn.e3mall.common.util.FastDFSClient;
public class FastDfsTest {
@Test
public void testUpload() throws Exception{
//创建配置文件 内容为tracker服务器的地址
ClientGlobal.init("D:/workspace/e3-manager-web/src/main/resources/conf/client.conf");
//使用全局对象加载配置文件
TrackerClient trackerClient =new TrackerClient();
//创建TrackerClient对象
TrackerServer trackerServer=trackerClient.getConnection();
//通过TrackerClient获得TrackerServer对象
StorageServer storageServer =null;
//创建StorageServer的引用 可以为null
StorageClient storageClient =new StorageClient(trackerServer,storageServer);
//创建StorageClient上传文件
String[] strings = storageClient.upload_file("E:/monkey.jpg", "jpg", null);
for (String string : strings) {
System.out.println(string);
}
}
@Test
public void testFastDfsClient() throws Exception{
//配置文件路径
FastDFSClient fastDFSClient=new FastDFSClient("D:/workspace/e3-manager-web/src/main/resources/conf/client.conf");
String string = fastDFSClient.uploadFile("G:/Picture/2015103157654285.jpg");
System.out.println(string);
}
}
代码语言:javascript复制package cn.e3mall.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import cn.e3mall.common.util.FastDFSClient;
import cn.e3mall.common.util.JsonUtils;
@Controller
public class PictureController {
@Value("${IMAGE_SERVER_URL}")
private String IMAGE_SERVER_URL;
@RequestMapping("https://img.yuanmabao.com/zijie/pic/upload")
@ResponseBody
public String uploadFile(MultipartFile uploadFile){
try {
FastDFSClient fastDFSClient =new FastDFSClient("classpath:conf/client.conf");
//取文件扩展名
String originalFilename=uploadFile.getOriginalFilename();
String extName=originalFilename.substring(originalFilename.lastIndexOf(".") 1);
//得到图片地址和文件名
String path = fastDFSClient.uploadFile(uploadFile.getBytes(), extName);
String url=IMAGE_SERVER_URL path;
Map result=new HashMap<>();
result.put("error", 0);
result.put("url", url);
return JsonUtils.objectToJson(result);
} catch (Exception e) {
e.printStackTrace();
Map result=new HashMap<>();
result.put("error", 1);
result.put("message", "图片上传失败");
return JsonUtils.objectToJson(result);
}
}
}
富文本编辑器:
纯js开发,跟后台语言没有关系。
使用方法:
第一步:在jsp中引入KindEditor的css和js代码。
第二步:在表单中添加一个textarea控件。是一个富文本编辑器的载体。类似数据源。
第三步:初始化富文本编辑器。使用官方提供的方法初始化。
第四步:取富文本编辑器的内容。
表单提交之前,把富文本编辑器的内容同步到textarea控件中。