springmcv实现图片上传

详细的图片上传步骤

  1. 校验图片格式
  2. 设置文件路径
  3. 校验图片是否合法(防止伪装)
  4. 保存图片

实例如下:

  • 因为前端是使用富文本插件,需要的是text/plain类型,而我们springmvc默认返回的响应类型是:application/json,所以我们需要进行设置:produces = MediaType.TEXT_PLAIN_VALUE
package com.taotao.manage.controller;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.taotao.manage.bean.PicUploadResult;
import com.taotao.manage.service.PropertiesService;

@Controller
@RequestMapping("pic")
public class PicUploadController {
    private static final Logger LOGGER = LoggerFactory
            .getLogger(PicUploadController.class);
    private static final ObjectMapper mapper = new ObjectMapper();


//  历史遗留类型,springmvc返回的响应类型是application/json,而富文本插件需要的是text/plain,所以我们如果不手动返回的json格式的话会使富文本插件不认识返回值,所以需要我们手动转json格式 
//  MediaType.TEXT_PLAIN_VALUE 将ResponseBody的返回的响应类型编程text/plain
    // 允许上传的格式
    private static final String[] IMAGE_TYPE = new String[] { ".bmp", ".jpg",
            ".jpeg", ".gif", ".png" };


    //引发的问题:spring的子父容器
    //spring提供注解,可以获取资源文件中的内容,因为value是spring配置文件创建的容器里的,只能访问自己容器内的资源,springmvc容器里访问不了,所以放这里不行
/*  @Value("${image.restore_path}")
    private String restore_path;

    @Value("${image.access_path}")
    private String access_path;*/
    //第一种方式:可以把applicationContext-jdbc(spring容器)里的读取配置文件的配置,放到taotao-manage-servlet.xml(springmvc容器)里

    //第二种方式:这里通过依赖注入的方式,创建一个service,专门用来获取配置文件的属性,再通过依赖注入把service对象放到springmvc容器里,这样就可以获取资源
    @Autowired
    private PropertiesService propertiesService;


    @RequestMapping(value = "upload", method = RequestMethod.POST, produces = MediaType.TEXT_PLAIN_VALUE)
    @ResponseBody
    public String upload(@RequestParam("uploadFile") MultipartFile multipartFile,HttpServletResponse response)
            throws IllegalStateException, IOException {
        //校验图片格式

        boolean isLegal = false;
        for(String type:IMAGE_TYPE){
            if (StringUtils.endsWithIgnoreCase(multipartFile.getOriginalFilename(), type)) {
                isLegal = true;
                break;
            }
        }

        //封装Result对象,并将文件的byte数组放置到result对象中
        PicUploadResult fileUploadResult  = new PicUploadResult();
        //状态
        fileUploadResult.setError(isLegal?0:1);
        //文件的新路径
        String filePath = getFilePath(multipartFile.getOriginalFilename());

        //日记对象的使用
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Pic file upload .[{}] to [{}] .",multipartFile.getOriginalFilename(),filePath);
        }

        //生成图片的绝对引用地址
        String picUrl = StringUtils.replace(StringUtils.substringAfterLast(filePath, propertiesService.restore_path), "\\", "/");
        //存放图片的地址
        fileUploadResult.setUrl(propertiesService.access_path + picUrl);
        //存放图片到本地
        File newFile = new File(filePath);
        multipartFile.transferTo(newFile);


        //校验图片是否合法
        isLegal = false;
        try{
            //再度校验,防止伪装
            BufferedImage image = ImageIO.read(newFile);
            if (image != null) {
                //基本上只有图片才有宽和高
                fileUploadResult.setWidth(image.getWidth() + "");
                fileUploadResult.setHeight(image.getHeight() + "");
                isLegal = true;
            }
        }catch(IOException e){
            e.printStackTrace();
        }

        fileUploadResult.setError(isLegal ? 0 : 1);

        if (!isLegal) {
            newFile.delete();
        }

        //将java对象序列化为json字符串
        return mapper.writeValueAsString(fileUploadResult);
    }

    private String getFilePath(String originalFilename) {
        String baseFolder = propertiesService.restore_path + File.separator + "images";

        DateTime dateTime = new DateTime(new Date());
        String fileFolder = baseFolder + File.separator + dateTime.toString("yyyy") + File.separator + 
                dateTime.toString("MM") + File.separator + dateTime.toString("dd");


    /*  Calendar calendar = Calendar.getInstance();
        String fileFolder = baseFolder + File.separator + calendar.get(Calendar.YEAR) + File.separator + 
                calendar.get(Calendar.MONTH) + File.separator + calendar.get(Calendar.DAY_OF_MONTH);*/
        //创建文件对象
        File file = new File(fileFolder);
        if (!file.isDirectory()) {
            file.mkdirs();
        }
        //生成新的文件名:
        UUID uuid = UUID.randomUUID();
        String fileName = uuid.toString().replaceAll("-", "") + "." + StringUtils.substringAfter(originalFilename, ".");

        /*String fileName  = new DateTime(new Date()).toString("yyyyMMddhhmmssSSSS")
                + RandomUtils.nextInt(100, 9999) + "." + StringUtils.substringAfter(originalFilename, ".");*/
        return fileFolder + File.separator + fileName;
    }
}

代码中PropertiesService问题:
是为了解决@value注解只能访问自己容器内资源的问题:
- @value直接放在PicUploadController中,此时PicUploadController是springmvc容器再管理的,而@value实现的注入是在spring大容器中才可以,所以这里就会导致注入不了;
- 将@value放在PropertiesService中,PropertiesService是由spring容器管理的,所以可以实现注入。
- 如下:

package com.taotao.manage.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class PropertiesService {
    @Value("${image.restore_path}")
    public String restore_path;
    @Value("${image.access_path}")
    public String access_path;
}

代码中的PicUploadResult,是我们需要返回前台做回显的对象,具体设置如下:

package com.taotao.manage.bean;

public class PicUploadResult {

    private Integer error;

    private String url;

    private String width;

    private String height;

    public Integer getError() {
        return error;
    }

    public void setError(Integer error) {
        this.error = error;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getWidth() {
        return width;
    }

    public void setWidth(String width) {
        this.width = width;
    }

    public String getHeight() {
        return height;
    }

    public void setHeight(String height) {
        this.height = height;
    }



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值