工作总结之----生成缩略图并上传

/**
     * 保存项目
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/saveData", method = RequestMethod.POST)
    @ResponseBody
    public HashMap saveDataNews(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HashMap map = new HashMap();
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
        //缩略图
        MultipartFile multipartFile1 = multipartHttpServletRequest.getFile("file1");
        Long si = multipartFile1.getSize();
        if(si>0){
            Properties properties = ConfigUtil.PROPERTIES;
            String path = properties.getProperty("imageResourcrsPath");
            path = path + "/image";
            // 获取上传的图片文件
            String fileName = multipartFile1.getOriginalFilename();
            File file = new File(fileName);
            File f = new File(path);
            if (!f.exists()) {
                f.mkdirs();
            }
            // 防止出现重名文件--uuid重命名
            File newFile = FileUtil.renameFile(file);
            // 服务器存的真实名字
            String newFileName = newFile.getName();
            // 服务器路径(路径+真实名字)
            StringBuffer newPaths = new StringBuffer(path);
            newPaths.append("\\");
            newPaths.append(newFileName);
            // 路径+真实名字
            String newPath = newPaths.toString();
            // 服务器中文件
            File targetFile = new File(path, newFileName);
            try {
                multipartFile1.transferTo(targetFile);
                processUploadedFile(newPaths.toString(),newFileName);


            } catch (Exception a) {
                a.printStackTrace();
            }
            StringBuffer thuPath = new StringBuffer();
            String str = "/resource/image/small-";
            thuPath = thuPath.append(str).append(newFileName);
            //图片服务器存储路径
            String thumbnailPath=thuPath.toString();
            //图片名称
            String thumbnailName="small-" + fileName;
            Thumbnail thum =new Thumbnail();
            thum.setThumbnailPath(thumbnailPath);
            thum.setThumbnailName(thumbnailName);
            newsListService.insertSelective(thum);//保存缩略图信息
            
        }
        Integer code = 1;
        map.put("code", code);
        return map;


    }
    
    
    /**
* <p>
* Description: 上传文件重命名
* </p>

* @param file
*            文件名
* @return 文件
* @author : gaoying
* @update :
* @date : 2015-7-26
*/
public static File renameFile(File file) {
String body = "";
String ext = "";
Date date = new Date();
int pot = file.getName().lastIndexOf(".");
if (pot != -1) {
// body = date.getTime() + "";
body = UUID.randomUUID().toString().replace("-", "");
ext = file.getName().substring(pot);
} else {
body = (new Date()).getTime() + "";
ext = "";
}
String newName = body + ext;
file = new File(file.getParent(), newName);
return file;
}


//生成缩略图的操作
    private void processUploadedFile(String path, String name) throws Exception {
        long time = System.currentTimeMillis();
        GMOperation op = new GMOperation();
        //待处理图片的绝对路径
        op.addImage(path);
        //图片压缩比,有效值范围是0.0-100.0,数值越大,缩略图越清晰  s
        op.quality(100.0);
        //width 和height可以是原图的尺寸,也可以是按比例处理后的尺寸
        op.addRawArgs("-resize", "500");
        //宽高都为100
        //op.addRawArgs("-resize", "100x100");
        op.addRawArgs("-gravity", "center");
        //op.resize(100, null);
        //从属性文件中读取缩略图的保存路径
        Properties properties = ConfigUtil.PROPERTIES;
        String smallpath = properties.getProperty("imageResourcrsPath");
        smallpath = smallpath + "/image";
        File smallFile = new File(smallpath);
        if(!smallFile.exists()) {
            smallFile.mkdir();
        }


        op.addImage(smallFile.getAbsolutePath() + "/" + "small-" +  name);


        // 如果使用ImageMagick,设为false,使用GraphicsMagick,就设为true,默认为false
        ConvertCmd convert = new ConvertCmd(true);
        String osName = System.getProperty("os.name").toLowerCase();
        if (osName.contains("win")) {
            //linux下不要设置此值,不然会报错
            convert.setSearchPath(properties.getProperty("thumbnailPath"));
        }
        convert.run(op);
        //压缩图片保存
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值