本篇介绍FastDFS,无需整合spring即可使用,前端上传图片到控制层,控制层调用fastDFS工具类实现上传,上传成功返回文件地址到前端。
- 配置文件为fast_client.conf,fastDFS要求使用的文件名和属性名,要一致。
tracker_server = 120.78.191.143:22122
- fastdfs相关连接信息
IMG_SERVER_IP=http://192.168.1.*/ FASTDFS_CLIENT_FILE=resource/fdfs_client.conf
- 控制层代码如下
package com.tdrip.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import com.tdrip.model.db.OperatorModel; import com.tdrip.util.FastDFSClient; @RestController public class ImgUploadController { @Autowired private HttpSession session; @Value("${IMG_SERVER_IP}") private String IMG_SERVER_IP; @Value("${FASTDFS_CLIENT_FILE}") private String FASTDFS_CLIENT_FILE; @RequestMapping("/imgupload") public String imgUpload(HttpServletRequest request,@RequestPart(required = false, value = "file") MultipartFile file) { OperatorModel model = (OperatorModel) session.getAttribute("admin"); if (null == model) { return ""; } StringBuffer picUrl = new StringBuffer(IMG_SERVER_IP); try { //获得原始文件名 String oldName = file.getOriginalFilename(); //获得文件扩展名 String extName = oldName.substring(oldName.lastIndexOf(".")+ 1); //配置文件,指定tracker_server的IP地址 String conf = "classpath:" + FASTDFS_CLIENT_FILE; //创建FastDFS对象 FastDFSClient fastDFSClient = new FastDFSClient(conf); //文件路径 picUrl.append(fastDFSClient.uploadFile(file.getBytes(), extName)); } catch (Exception e) { e.printStackTrace(); } //返回文件路径 return picUrl.toString(); } }
- FastDFS工具类
package com.tdrip.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; /** * conf为配置文件的路径,配置文件为fastdfs服务器的ip地址和端口 * @param conf * @throws Exception */ 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); } }