分享

文件服务器

 WindySky 2017-03-14

图片服务器是为了减轻应用服务器的压力,对于电商很重要,并且可以实时读出尺寸的图片


1.控制图片的IO读和写

  1. @Controller  
  2. @RequestMapping("/fs/file")  
  3. public class FileController extends SpringController {  
  4.       
  5.     @Autowired  
  6.     private BaseService baseService;  
  7.       
  8.     /** 
  9.      * Springmvc图片上传,MultipartFile 
  10.      * 2015-8-3下午4:33:13 
  11.      * @param file 
  12.      */  
  13.     @RequestMapping("/upload")  
  14.     @ResponseBody  
  15.     public String upload(MultipartFile file) {  
  16.         String fileServerName = "";  
  17.         if (file != null) {  
  18.             fileServerName = baseService.uploadFile(file,BaseConstant.SAVE_TYPE_FILE);    
  19.         }  
  20.         return fileServerName;  
  21.     }  
  22.       
  23.     /** 
  24.      * 根据路径名称获取 
  25.      * 2015-8-3下午5:34:25 
  26.      * @param fileServerName 
  27.      * @return 
  28.      */  
  29.     @RequestMapping("/showFile")  
  30.     @ResponseBody  
  31.     public void showFile(String fileName) {  
  32.         if (StringUtils.isNotEmpty(fileName)) {  
  33.             if(StringUtils.contains(fileName, "_")) {                 
  34.                 String savePath = PropertyUtil.getPropertyValue(BaseConstant.CONFIG_PROPERTY_PATH, BaseConstant.FILE_SAVA_DISK)+fileName.split("_")[1];  
  35.                 String desPath = GMagickUtil.reduceFile(fileName.split("_")[0], savePath);  
  36.                 File file =new File(desPath);  
  37.                 FileUtil.renderFileToClient(file, BaseConstant.CONTENTTYPE_IMAGE);  
  38.                 file.delete();  
  39.             }else{  
  40.                 String savePath = PropertyUtil.getPropertyValue(BaseConstant.CONFIG_PROPERTY_PATH, BaseConstant.FILE_SAVA_DISK)+fileName;  
  41.                 FileUtil.renderFileToClient(new File(savePath), BaseConstant.CONTENTTYPE_IMAGE);  
  42.             }  
  43.         }  
  44.     }  
  45.       
  46. }  

2.图片的service

  1. /** 
  2.      * io保存上传的文件 
  3.      * 2015-8-6上午8:23:33 
  4.      * @param file 
  5.      * @param saveType 
  6.      * @return 
  7.      */  
  8.     public String uploadFile(MultipartFile file, String saveType){  
  9.         String fileServerName = "";  
  10.         Properties property = PropertyUtil.getProperty(BaseConstant.CONFIG_PROPERTY_PATH);  
  11.           
  12.         String savePath = "";  
  13.         if(BaseConstant.SAVE_TYPE_UEDITOR.equals(saveType)) {  
  14.             savePath = property.getProperty(BaseConstant.UEDITOR_SAVA_PATH) + FileUtil.getRelativePath();  
  15.         }else {  
  16.             savePath = property.getProperty(BaseConstant.FILE_SAVA_PATH) + FileUtil.getRelativePath();  
  17.         }  
  18.           
  19.         File dir = new File(property.getProperty(BaseConstant.FILE_SAVA_DISK) + savePath);  
  20.           
  21.         if (!dir.exists()) {  
  22.             dir.mkdirs();  
  23.         }  
  24.         //获取文件的路径名称,统一以jpg的格式进行保存  
  25.         String newFileName = UUID.getUUID() + "." + FilenameUtils.getExtension(file.getOriginalFilename());  
  26.           
  27.         File newFile = new File(dir + "/" +newFileName);  
  28.         try{  
  29.             file.transferTo(newFile);  
  30.             fileServerName = savePath + newFileName;  
  31.         } catch (IOException e) {  
  32.             e.printStackTrace();  
  33.         }     
  34.           
  35.         return fileServerName;  
  36.     }  


3.压缩GraphicsMagick工具

  1. public class GMagickUtil {  
  2.       
  3.     /** 
  4.      * 根据不同的需求生成不同的尺寸 
  5.      * 2015-8-4上午9:41:01 
  6.      * @param fileType 
  7.      * @param savePath 
  8.      *  
  9.      */  
  10.     public static String reduceFile(String fileType, String srcPath) {  
  11.         String desPath = FileUtil.getTempDir() + UUID.getUUID() + BaseConstant.FILE_EXTENSION;  
  12.         try {  
  13.             IMOperation operation = new IMOperation();  
  14.             operation.addImage(srcPath);  
  15.             operation.addRawArgs("-quality", BaseConstant.REDUCE_PERCENT);    
  16.               
  17.             if(StringUtil.containsKey(BaseConstant.THUMB_VALUE, fileType)){  
  18.                 String[] strs = fileType.split("x");  
  19.                 operation.resize(Integer.valueOf(strs[0]), Integer.valueOf(strs[1]), '!');  
  20.             }  
  21.             operation.addImage(desPath);  
  22.             //ConvertCmd false使用ImageMagic,true使用GraphicsMagick    
  23.             ConvertCmd cmd = new ConvertCmd(true);            
  24.             cmd.run(operation);  
  25.         } catch (Exception e) {  
  26.               
  27.             e.printStackTrace();  
  28.         }  
  29.         return desPath;  
  30.     }  
  31.       
  32.     /** 
  33.      * 取图片中心,按尺寸获取 
  34.      * 2015-8-4上午9:41:40 
  35.      * @param srcPath 
  36.      * @param desPath 
  37.      * @param rectw 
  38.      * @param recth 
  39.      * 
  40.      */  
  41.     public static void cropImageCenter(String srcPath, String desPath, int rectw, int recth) {    
  42.         IMOperation operation = new IMOperation();    
  43.         operation.addImage(srcPath);    
  44.         operation.resize(rectw, recth, '^').gravity("center").extent(rectw, recth);    
  45.         operation.addImage(desPath);    
  46.         ConvertCmd convert = new ConvertCmd(true);    
  47.         try {  
  48.             convert.run(operation);  
  49.         } catch (Exception e) {  
  50.             e.printStackTrace();  
  51.         }  
  52.     }  
  53.       
  54. }  

4.一些常量

  1. public class BaseConstant {  
  2.       
  3.     /**配置文件路径*/  
  4.     public static final String CONFIG_PROPERTY_PATH = "/conf/file.properties";  
  5.       
  6.     /**文件服务器名称*/  
  7.     public static final String FILE_SERVER_NAME = "file.servername";  
  8.     /**文件保存的盘符*/  
  9.     public static final String FILE_SAVA_DISK = "file.disk";  
  10.     /**文件保存路径名称*/  
  11.     public static final String FILE_SAVA_PATH = "file.savepath";  
  12.     /**百度编辑器路径名称*/  
  13.     public static final String UEDITOR_SAVA_PATH = "ueditor.savepath";  
  14.       
  15.     /**保存类型*/  
  16.     public static final String SAVE_TYPE_FILE = "file";  
  17.     public static final String SAVE_TYPE_UEDITOR = "ueditor";  
  18.       
  19.     /**压缩允许值*/  
  20.     public static final String[] THUMB_VALUE = new String[]{"80x80", "100x100", "120x120", "150x150", "200x200", "250x250"};  
  21.       
  22.     /**图片压缩比例*/  
  23.     public static final String REDUCE_PERCENT = "60";  
  24.       
  25.     /**压缩图片格式*/  
  26.     public static final String FILE_EXTENSION = ".jpg";  
  27.       
  28.     /**图片输出流*/  
  29.     public static final String CONTENTTYPE_IMAGE = "image/jpeg";  




    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约