zoukankan      html  css  js  c++  java
  • 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**************************************/
        }

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

  • 相关阅读:
    Docker生态会重蹈Hadoop的覆辙吗?
    刘志强博士:专业涵养 奉献情怀
    Sublime Text3前端必备插件
    JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof使用详解
    jvm的stack和heap,JVM内存模型,垃圾回收策略,分代收集,增量收集(转)
    Eclipse安装MAT插件
    tomcat内存泄漏存入dump文件
    CSS中behavior属性语法简介
    get/post时中文乱码问题的解决办法
    Java序列化的机制和原理
  • 原文地址:https://www.cnblogs.com/xh_Blog/p/8269093.html
Copyright © 2011-2022 走看看