附件上传

Spring MVC图片上传与处理

1.上传图片附件并使用BufferedImage进行处理

@PostMapping(value = "uploadImg/{type1}/{type2}")
    @SYS_LOG(module = "用户", type = "新增", description = "上传文件")
    @ResponseBody
    public String uploadImg(@RequestParam("files") CommonsMultipartFile[] files,
            @PathVariable("type1") String type1 ,@PathVariable("type2") String type2) throws IOException {
        Date date = new Date();
        SimpleDateFormat sd = new SimpleDateFormat("yyyyMMdd");
        String dateStr = sd.format(date);
        String bigPath = "C:\\systemImg\\uploadImg\\big\\"+ type1+"\\"+type2+"\\"+dateStr ;// 大图片路径
        String smallPath = "C:\\systemImg\\uploadImg\\small\\"+ type1+"\\"+type2+"\\" + dateStr ;// 小图片路径
        String returnPath = ""; // 返回值
        
        try {
            File dirb = new File(bigPath);
            if (!dirb.exists()) {
                dirb.mkdirs();
            }
            File dirs = new File(smallPath);
            if (!dirs.exists()) {
                dirs.mkdirs();
            }
            if (files.length > 0 && files != null) {
                for (int i = 0; i < files.length; i++) {

                    Random random = new Random();
                    int randomNo = random.nextInt(10000);
                    CommonsMultipartFile file = files[i];
                    BufferedImage img = ImageIO.read(file.getInputStream());
                    if (img == null) {
                        continue;
                    } else {
                        bigPath = "C:\\systemImg\\uploadImg\\big\\"+ type1+"\\"+type2+"\\"+dateStr ;// 大图片路径
                        smallPath = "C:\\systemImg\\uploadImg\\small\\"+ type1+"\\"+type2+"\\" + dateStr ;// 小图片路径
                        bigPath += "\\"+ randomNo + "_" + file.getOriginalFilename();
                        smallPath += "\\" + randomNo + "_" + file.getOriginalFilename();
                        File localBigFile = new File(bigPath);
                        //File localSmallFile = new File(smallPath);
                        BufferedImage buffImg = null;  
                        buffImg = new BufferedImage(300, 200, BufferedImage.TYPE_INT_RGB);  
                        files[i].transferTo(localBigFile);
                        buffImg.getGraphics().drawImage(  
                                img.getScaledInstance(300, 200, img.SCALE_SMOOTH), 0,  
                                    0, null); 
                        String name=files[i].getFileItem().getName();
                        String type=name.substring(name.lastIndexOf(".")+1 );
                        ImageIO.write(buffImg, type, new File(smallPath)); 
                        returnPath += dateStr + "/" + randomNo + "_" + file.getOriginalFilename() + ",";
                    }
                }
                
            }
                return  dateStr + "/" + returnPath.substring(0, returnPath.length() - 1);

            }catch (IllegalStateException | IOException e) {
            e.printStackTrace();
            return "error";
        }
        
    }

注:type 1,type2为文件夹名

2.spring-mvc配置

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"/>
         <property name="maxUploadSize" value="10485760"/>
         <property name="maxInMemorySize" value="10240"/>
    </bean>

3.jar依赖

spring-context-4.3.14.RELEASE

 

转载于:https://www.cnblogs.com/Atseas/p/9656621.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值