package com.hr.common.utils;
import org.springframework.web.multipart.MultipartFile;
import com.hr.common.pojo.PictureResult;
/**
*
* @ClassName PictureUtils
* @Description TODO(图片上传工具类 上传图片并返回状态以及路径)
* @author Administrator
* @Date 2018年8月22日 下午7:46:20
* @version 1.0.0
*/
public class PictureUtils {
public static PictureResult uploadImages(MultipartFile file) {
PictureResult picture = new PictureResult();
if(file != null) {
String filename=file.getOriginalFilename();
//System.out.println(“filename:”+filename);
//判断文件名是否为空
if(filename.trim().length()>0) {
//获取后缀名称,通过分隔符点
String substring = filename.substring(filename.lastIndexOf(“.”)+1);
// System.out.println(“后缀名是:”+substring);
try {
FastDFSClient dfClient=new FastDFSClient(“classpath:client.conf”);
//返回上传路径,把图片序列化到字节
String uploadFile = dfClient.uploadFile(file.getBytes(),substring);
// System.out.println("图片上传全路径"+uploadFile);
// System.out.println("图片上传全路径"+uploadFile);
System.out.println("filename:"+filename);
//判断文件名是否为空
picture.setUrl(uploadFile);
picture.setCode(0);
picture.setMes("图片上传成功");
} catch (Exception e) {
picture.setCode(1);
picture.setMes("图片上传失败");
}
}
}else {
picture.setCode(1);
picture.setMes("图片为空");
}
return picture;
}
}