springMVC多图片压缩上传的实现

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

    <!--配置文件的视图解析器,用于文件上传,其中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**************************************/
    }

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

 

转载于:https://www.cnblogs.com/xh_Blog/p/8269093.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值