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

2024-08-27 10:18

本文主要是介绍工作总结之----生成缩略图并上传,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

/**
     * 保存项目
     * @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);
        //压缩图片保存
    }

这篇关于工作总结之----生成缩略图并上传的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1111386

相关文章

Java中的xxl-job调度器线程池工作机制

《Java中的xxl-job调度器线程池工作机制》xxl-job通过快慢线程池分离短时与长时任务,动态降级超时任务至慢池,结合异步触发和资源隔离机制,提升高频调度的性能与稳定性,支撑高并发场景下的可靠... 目录⚙️ 一、调度器线程池的核心设计 二、线程池的工作流程 三、线程池配置参数与优化 四、总结:线程

Spring Boot 与微服务入门实战详细总结

《SpringBoot与微服务入门实战详细总结》本文讲解SpringBoot框架的核心特性如快速构建、自动配置、零XML与微服务架构的定义、演进及优缺点,涵盖开发环境准备和HelloWorld实战... 目录一、Spring Boot 核心概述二、微服务架构详解1. 微服务的定义与演进2. 微服务的优缺点三

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

JavaSE正则表达式用法总结大全

《JavaSE正则表达式用法总结大全》正则表达式就是由一些特定的字符组成,代表的是一个规则,:本文主要介绍JavaSE正则表达式用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录常用的正则表达式匹配符正则表China编程达式常用的类Pattern类Matcher类PatternSynta

SpringBoot集成LiteFlow工作流引擎的完整指南

《SpringBoot集成LiteFlow工作流引擎的完整指南》LiteFlow作为一款国产轻量级规则引擎/流程引擎,以其零学习成本、高可扩展性和极致性能成为微服务架构下的理想选择,本文将详细讲解Sp... 目录一、LiteFlow核心优势二、SpringBoot集成实战三、高级特性应用1. 异步并行执行2

Spring @Scheduled注解及工作原理

《Spring@Scheduled注解及工作原理》Spring的@Scheduled注解用于标记定时任务,无需额外库,需配置@EnableScheduling,设置fixedRate、fixedDe... 目录1.@Scheduled注解定义2.配置 @Scheduled2.1 开启定时任务支持2.2 创建

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF