圖片服務(wù)器是為了減輕應(yīng)用服務(wù)器的壓力,對于電商很重要,并且可以實時讀出尺寸的圖片
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;
- }
-
- /**
- * 根據(jù)路徑名稱獲取
- * 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();
- }
- //獲取文件的路徑名稱,統(tǒng)一以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 {
-
- /**
- * 根據(jù)不同的需求生成不同的尺寸
- * 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";
-
- /**文件服務(wù)器名稱*/
- 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";
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。