图片服务器是为了减轻应用服务器的压力,对于电商很重要,并且可以实时读出尺寸的图片
1.控制图片的IO读和写
- @Controller
- @RequestMapping("/fs/file")
- public class FileController extends SpringController {
-
- @Autowired
- private BaseService baseService;
-
- /**
- * Springmvc图片上传,MultipartFile
- * 2015-8-3下午4:33:13
- * @param file
- */
- @RequestMapping("/upload")
- @ResponseBody
- public String upload(MultipartFile file) {
- String fileServerName = "";
- if (file != null) {
- fileServerName = baseService.uploadFile(file,BaseConstant.SAVE_TYPE_FILE);
- }
- return fileServerName;
- }
-
- /**
- * 根据路径名称获取
- * 2015-8-3下午5:34:25
- * @param fileServerName
- * @return
- */
- @RequestMapping("/showFile")
- @ResponseBody
- public void showFile(String fileName) {
- if (StringUtils.isNotEmpty(fileName)) {
- if(StringUtils.contains(fileName, "_")) {
- String savePath = PropertyUtil.getPropertyValue(BaseConstant.CONFIG_PROPERTY_PATH, BaseConstant.FILE_SAVA_DISK)+fileName.split("_")[1];
- String desPath = GMagickUtil.reduceFile(fileName.split("_")[0], savePath);
- File file =new File(desPath);
- FileUtil.renderFileToClient(file, BaseConstant.CONTENTTYPE_IMAGE);
- file.delete();
- }else{
- String savePath = PropertyUtil.getPropertyValue(BaseConstant.CONFIG_PROPERTY_PATH, BaseConstant.FILE_SAVA_DISK)+fileName;
- FileUtil.renderFileToClient(new File(savePath), BaseConstant.CONTENTTYPE_IMAGE);
- }
- }
- }
-
- }
2.图片的service
- /**
- * io保存上传的文件
- * 2015-8-6上午8:23:33
- * @param file
- * @param saveType
- * @return
- */
- public String uploadFile(MultipartFile file, String saveType){
- String fileServerName = "";
- Properties property = PropertyUtil.getProperty(BaseConstant.CONFIG_PROPERTY_PATH);
-
- String savePath = "";
- if(BaseConstant.SAVE_TYPE_UEDITOR.equals(saveType)) {
- savePath = property.getProperty(BaseConstant.UEDITOR_SAVA_PATH) + FileUtil.getRelativePath();
- }else {
- savePath = property.getProperty(BaseConstant.FILE_SAVA_PATH) + FileUtil.getRelativePath();
- }
-
- File dir = new File(property.getProperty(BaseConstant.FILE_SAVA_DISK) + savePath);
-
- if (!dir.exists()) {
- dir.mkdirs();
- }
- //获取文件的路径名称,统一以jpg的格式进行保存
- String newFileName = UUID.getUUID() + "." + FilenameUtils.getExtension(file.getOriginalFilename());
-
- File newFile = new File(dir + "/" +newFileName);
- try{
- file.transferTo(newFile);
- fileServerName = savePath + newFileName;
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- return fileServerName;
- }
3.压缩GraphicsMagick工具
- public class GMagickUtil {
-
- /**
- * 根据不同的需求生成不同的尺寸
- * 2015-8-4上午9:41:01
- * @param fileType
- * @param savePath
- *
- */
- public static String reduceFile(String fileType, String srcPath) {
- String desPath = FileUtil.getTempDir() + UUID.getUUID() + BaseConstant.FILE_EXTENSION;
- try {
- IMOperation operation = new IMOperation();
- operation.addImage(srcPath);
- operation.addRawArgs("-quality", BaseConstant.REDUCE_PERCENT);
-
- if(StringUtil.containsKey(BaseConstant.THUMB_VALUE, fileType)){
- String[] strs = fileType.split("x");
- operation.resize(Integer.valueOf(strs[0]), Integer.valueOf(strs[1]), '!');
- }
- operation.addImage(desPath);
- //ConvertCmd false使用ImageMagic,true使用GraphicsMagick
- ConvertCmd cmd = new ConvertCmd(true);
- cmd.run(operation);
- } catch (Exception e) {
-
- e.printStackTrace();
- }
- return desPath;
- }
-
- /**
- * 取图片中心,按尺寸获取
- * 2015-8-4上午9:41:40
- * @param srcPath
- * @param desPath
- * @param rectw
- * @param recth
- *
- */
- public static void cropImageCenter(String srcPath, String desPath, int rectw, int recth) {
- IMOperation operation = new IMOperation();
- operation.addImage(srcPath);
- operation.resize(rectw, recth, '^').gravity("center").extent(rectw, recth);
- operation.addImage(desPath);
- ConvertCmd convert = new ConvertCmd(true);
- try {
- convert.run(operation);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- }
4.一些常量
- public class BaseConstant {
-
- /**配置文件路径*/
- public static final String CONFIG_PROPERTY_PATH = "/conf/file.properties";
-
- /**文件服务器名称*/
- public static final String FILE_SERVER_NAME = "file.servername";
- /**文件保存的盘符*/
- public static final String FILE_SAVA_DISK = "file.disk";
- /**文件保存路径名称*/
- public static final String FILE_SAVA_PATH = "file.savepath";
- /**百度编辑器路径名称*/
- public static final String UEDITOR_SAVA_PATH = "ueditor.savepath";
-
- /**保存类型*/
- public static final String SAVE_TYPE_FILE = "file";
- public static final String SAVE_TYPE_UEDITOR = "ueditor";
-
- /**压缩允许值*/
- public static final String[] THUMB_VALUE = new String[]{"80x80", "100x100", "120x120", "150x150", "200x200", "250x250"};
-
- /**图片压缩比例*/
- public static final String REDUCE_PERCENT = "60";
-
- /**压缩图片格式*/
- public static final String FILE_EXTENSION = ".jpg";
-
- /**图片输出流*/
- public static final String CONTENTTYPE_IMAGE = "image/jpeg";
|