spring boot +MultipartFile上传文件实践

1.添加文件上传的依赖

        <!-- 上传 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3</version>
        </dependency>

2.创建消息资源文件用于记录存储文件地址(app.properties)

#文件上传地址
upload_bannerImg=D:/data/img/

3.创建加载app文件工具类,用于读取外部文件,这里举的例子读的就是app.properties文件



import java.io.IOException;
import java.util.Properties;

/**
 * <b>类名称:</b>AppPropertiesUtil
 * <b>类描述:</b>加载app文件工具类
 * <b>创建人:</b>ChenSong
 * <b>修改人:</b>ChenSong
 * <b>修改时间:</b>2016-8-2 上午11:06:58
 * <b>修改备注:</b>
 * @version v1.0<br/>
 */
public class ConfigPropertiesUtil {
	    private static Properties p = new Properties();  
	    static{  
	        try {  
	            p.load(ConfigPropertiesUtil.class.getClassLoader().getResourceAsStream("app.properties"));
	        } catch (IOException e) {  
	            e.printStackTrace();   
	        }  
	    }  

	    public static String getValue(String key){
	        return p.getProperty(key);
	    }
}

4.上传图片代码

package com.edu.cms.controller.banner;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.edu.cms.controller.banner.BannerController;
import com.edu.cms.util.ConfigPropertiesUtil;

import javax.imageio.ImageIO;

@RestController
@RequestMapping(value = "file")
public class FileUpload {
	final Logger logger = LoggerFactory.getLogger(BannerController.class);

	/**
	 * @Auther: yaohongan
	 * @Description 上传图片 
	 * @Date: 2019/3/21 17:19
	 */
	@RequestMapping(value = "/imgUpload", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> uploadImg(MultipartFile imgFile)
			throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			// 获取图片名称
			String imgName = imgFile.getOriginalFilename();
			// 截取图片后缀(.jps,.png)
			String extendnameh = imgName.substring(imgName.length() - 4, imgName.length());
			// 生成随机数来代替图片新名称,并把截取的后缀加上
			String fileName = UUID.randomUUID().toString() + extendnameh;
			// 获取图片尺寸
			InputStream inputStream = imgFile.getInputStream();
			BufferedImage bufferedImage = ImageIO.read(inputStream);
			// 得到图宽
			int width = bufferedImage.getWidth();
			// 得到图高
			int height = bufferedImage.getHeight();
			String imgSize=(height+"*"+width);
			// 在app.properties文件中根据upload_bannerImg来获取上传文件地址
			String fileUrl = ConfigPropertiesUtil.getValue("upload_bannerImg");
			// 将上传路径和路径名称封装到file里
			File file = new File(fileUrl + fileName);
			// 将图片上传到指定的路径上
			imgFile.transferTo(file);
			resultMap.put("ret", "0");
			resultMap.put("msg", "SUCCESS");
			resultMap.put("fileName", fileName);
			resultMap.put("imgSize",imgSize);
		} catch (IllegalStateException | IOException e) {
			// TODO Auto-generated catch block
			resultMap.put("ret", "-1");
			resultMap.put("msg", "图片上传失败");
			return resultMap;
		}
		return resultMap;
	}
}
发布了30 篇原创文章 · 获赞 20 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/YiWangJiuShiXingFu/article/details/88720338