SpringMvc上传图片文件

一.Poml文件

<dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>

二.springmvc.xml配置

<!--上传-->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8" />
        <!--100M-->
        <property name="maxUploadSize" value="104857600" />
        <property name="maxInMemorySize" value="4096" />
    </bean>

三.上传接口

   @ResponseBody
    @PostMapping("/uploadImg")
    public TResult upload(@RequestParam("file") MultipartFile imageFile, HttpServletRequest request) {
        TResult result = new TResult();
        String root = request.getServletContext().getRealPath(PIC_ROOT);
        String uploadUrl = root + "\\pic\\";
        //文件原名
        //String filename = imageFile.getOriginalFilename();
        String filename = java.util.UUID.randomUUID().toString();
        String pic_type = imageFile.getContentType();
        System.out.println(pic_type);

        if (pic_type.equals("image/jpeg")) {
            filename = filename.concat(".jpg");
        } else if (pic_type.equals("image/png")) {
            filename = filename.concat(".png");
        } else if (pic_type.equals("image/bmp")) {
            filename = filename.concat(".bmp");
        } else if (pic_type.equals("image/gif")) {
            filename = filename.concat(".gif");
        } else if (pic_type.equals("application/octet-stream")) { //二进制流(部分手机上传图片形式:华为EMUI4)
            filename = filename.concat(".png");
        } else {
            TResultEncap.setErrResult(result, ReturnCode.ERR11018);
            return result;
        }

        File dir = new File(uploadUrl);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        logger.trace("文件上传到: " + uploadUrl + filename);
        File targetFile = new File(uploadUrl + filename);
        if (!targetFile.exists()) {
            try {
                targetFile.createNewFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        try {
            imageFile.transferTo(targetFile);
        } catch (IllegalStateException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        ImageInputStream iis = null;
        try {
            iis = ImageIO.createImageInputStream(targetFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);

        //非图片文件
        if (!iter.hasNext()) {
            targetFile.delete();
            TResultEncap.setErrResult(result, ReturnCode.ERR11018);
            return result;
        }

        //图片压缩
        ScaleImageUtils scaleImageUtils = new ScaleImageUtils();
        try {
            scaleImageUtils.resizeByWidth(1280, uploadUrl + filename, targetFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        ImageReader reader = iter.next();

        try {
            System.out.println("Format: " + reader.getFormatName());
            iis.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String filepath="/pic/"+filename;
        result.setStringData(filepath);
        return result;
    }

猜你喜欢

转载自blog.csdn.net/u010520146/article/details/83791062