国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
文件服務(wù)器

圖片服務(wù)器是為了減輕應(yīng)用服務(wù)器的壓力,對于電商很重要,并且可以實時讀出尺寸的圖片


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.      * 根據(jù)路徑名稱獲取 
  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.         //獲取文件的路徑名稱,統(tǒng)一以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.      * 根據(jù)不同的需求生成不同的尺寸 
  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.     /**文件服務(wù)器名稱*/  
  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";  




本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
C#獲取文件名稱、路徑、后綴名
java 從網(wǎng)絡(luò)Url中下載文件
Java實現(xiàn)MySQL數(shù)據(jù)庫備份(二)
阿里云OSS圖片上傳類。
文件操作 File
項目經(jīng)驗分享——Java常用工具類集合
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服