springMVC多图片压缩上传的实现

首先需要在配置文件中添加配置:

1
2
3
4
5
6
7
8
9
10
11
12
<!--配置文件的视图解析器,用于文件上传,其中ID是固定的:multipartResolver-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding">
        <value>UTF-8</value>
    </property>
    <property name="maxUploadSize">
        <value>32505856</value><!--单位:byte 上传文件大小限制为31M,31*1024*1024 -->
    </property>
    <property name="maxInMemorySize">
        <value>4096</value>
    </property>
</bean>

前台代码:

复制代码
        <form id="userForm" method="post"  enctype="multipart/form-data">  
                    <div>
                        <label class="my_input_title">图片1:</label>
                        <input type="file" name="file" class="" id="" value="" placeholder="请选择图片1"/>
                    </div>
                    
                    <div>
                        <label class="my_input_title">图片2:</label>
                        <input type="file" name="file" class="" id="" value="" placeholder="请选择图片2"/>
                    </div>
            </form>
复制代码

后台压缩工具类代码:

复制代码
public static void photoupload(xxx x, @RequestParam("file") CommonsMultipartFile files[],ResultJson rj){
        /******************************图片上传start**********************************/
        InputStream input = null;  
        ByteArrayOutputStream bos = null;  
        
        // 获得项目的路径
        //ServletContext sc = request.getSession().getServletContext();
        
        // 上传的文件要保存到的路径
        String path = ("E:/x/"+x.getxRealname()+"/");
        
        File file = new File(path);
        
        if (!file.exists()) file.mkdirs();

        for (int i = 0; i < files.length; i++) {
            try {
                if (!files[i].isEmpty()) {
                    // 获得原始文件名
                    String fileName = files[i].getOriginalFilename();
                    
                    System.out.println(i+"原始文件名:" + fileName);
    
                    bos = new ByteArrayOutputStream();  
                    
                    input = files[i].getInputStream(); 
                    
                    Image image = ImageIO.read(input);  
                    //  图片压缩  
                    BufferedImage tag = new BufferedImage(239,127, BufferedImage.TYPE_INT_RGB);  
      
                    tag.getGraphics().drawImage(image.getScaledInstance(239,127,Image.SCALE_SMOOTH),0,0,null);
    
                    if (files[i].getOriginalFilename().endsWith(".jpg")) {  
                        ImageIO.write(tag, "jpg", bos);  
                    } else if (files[i].getOriginalFilename().endsWith(".png")) {  
                        ImageIO.write(tag, "png", bos);  
                    } else {  
                        rj.setMsg("上传图片格式错误");  
                        rj.setObj("");  
                        rj.setSuccess(false);
                    }  
                    
                    byte[] bytes = bos.toByteArray();  // 新文件名
                    String newFileName = UUID.randomUUID() + fileName;
    
                    FileOutputStream fos = new FileOutputStream(path + newFileName);
                    fos.write(bytes);
                    input.close();
                    fos.close();
                    
                    System.out.println("上传图片到:" + path + newFileName);
                    if(i==0){
                        x.setXPath(path + newFileName);
                    }else{
                        x.setYPath(path + newFileName);
                    }
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        /*******************************图片上传end**************************************/
    }
复制代码

逻辑如上,自己可修改相关参数实现,如上即可实现图片压缩上传!

 

posted @   xh_Blog  阅读(406)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示