吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1948|回复: 8
收起左侧

[Java 转载] Springboot使用MultipartFile上传图片

  [复制链接]
a525 发表于 2020-9-17 16:44
菜鸡程序员进阶之路——初入职场,要写一个图片上传的功能接口,搞定它。
Controller层接收参数,因为我是要同时上传多张图片,所以用数组接收 MultipartFile[] ,一张图片就只用MultipartFile就行了。
翠花,上代码.
@PostMapping("uploadFiles")
@ResponseBody
public List<String> uploadFiles(@ApiParam("只支持jpg图片格式,可以一次上传多张图片")
                                @RequestParam("files") MultipartFile[] files){
    List<String> strings = fileService.uploadFiles(files);
    return strings;
}
controller层很简单,只做参数的接收,处理数据到业务层
service层:
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class FileService {

    @Autowired
    private MongoTemplate mongoTemplate;

    private static final List ALLOW_IMAGE_TYPE = Arrays.asList("image/jpeg");

    /**
     * 图片上传
     * @Param files
     * @return
     */
    public List<String> uploadFiles(MultipartFile[] files) {

        List<String> list = new ArrayList<>();
          for (int i = 0; i < files.length; i++) {
            //判断图片类型
            String imageType = files[i].getContentType();
            if (!ALLOW_IMAGE_TYPE.contains(imageType)){
                log.info("图片类型错误!");
                throw new EqException(ExceptionEnum.IMAGE_TYPE_ERROR);
            }
            //解析图片内容
            BufferedImage bufferedImage = null;
            try {
                bufferedImage = ImageIO.read(files[i].getInputStream());
            } catch (IOException e) {
                log.info("解析图片失败!",e.getMessage());
                throw new EqException(ExceptionEnum.IMAGE_TYPE_ERROR);
            }
            if (bufferedImage == null){
                log.info("解析图片失败!");
                throw new EqException(ExceptionEnum.IMAGE_TYPE_ERROR);
            }
            //设置上传图片名称
              long time = new Date().getTime();
              String imageName = time + ".jpg";
            //得到上传文件夹对象
            File imagePathFile = new File(EqConstants.IMG_PATH);
            //上传图片
            try {
                files[i].transferTo(new File(imagePathFile,imageName));

            } catch (IOException e) {
                log.info("图片上传失败!");
                throw new EqException(ExceptionEnum.FILE_UPLOAD_ERROR);
            }
              list.add(EqConstants.IMG_URL+imageName);
        }
        return list;
    }
}
service层将图片解析,然后输出到自定义的目录,输出完毕后把图片的路径返回。
自定义的文件存放路径和返回路径前缀
public class EqConstants {
    //上传路径
    public static final String IMG_PATH = "D:\\develop\\nginx-1.18.0\\file\\images";
    //访问路径
//    public static final String IMG_PATH = projectPath()+"\\images";
    public static final String IMG_URL = "http://image.equip.com/images/";

    /**
     * 获取当前项目路径
     */
    public static String projectPath(){
        String projectPath = System.getProperty("user.dir");
        System.out.println("projectPath==" + projectPath);
        return projectPath;
    }
}
最后,让我们用Postman来测试一下。
选一张男人的最爱!
上传成功,返回连接
OK!测试成功
image.png
image.png

免费评分

参与人数 2吾爱币 +2 热心值 +2 收起 理由
天缀星光 + 1 + 1 为图而赞!
lxwan + 1 + 1 楼主为了给坛友发福利也是煞费苦心

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Ye_Kong 发表于 2020-9-17 17:09
学习了,谢谢楼主分享
drundragon 发表于 2020-9-18 09:18
lby520 发表于 2020-9-18 14:49
 楼主| a525 发表于 2020-9-18 17:14
天缀星光 发表于 2020-9-17 17:40
图不错……代码拿走了,感谢!

代码好看就给个分啊
 楼主| a525 发表于 2020-9-18 17:15
lby520 发表于 2020-9-18 14:49
代码不错,图拿走了谢谢。

克制住!
头像被屏蔽
悟辛 发表于 2020-9-25 13:47
提示: 作者被禁止或删除 内容自动屏蔽
一只少年 发表于 2020-10-4 12:42
图不错,谢谢分享代码
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-30 07:50

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表