栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

上传图片到本地服务器核心代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

上传图片到本地服务器核心代码

上传图片到本地服务器核心代码
  • 1.controller层
  • 2. service层
  • 3.postman测试

1.controller层
@RestController
@RequestMapping("/pic/upload")
public class PicUploadController {
    @Autowired
    private PicUploadFileSystemService uploadFileSystemService;
    
    @PostMapping
    @ResponseBody
    public PicUploadResult upload(@RequestParam("file") MultipartFile uploadFile) throws  Exception{
        return this.uploadFileSystemService.upload(uploadFile);
    }
}

注意:前端上传的参数名和@RequestParam(“file”)中的“file”一样。

2. service层
import com.dubbo.entity.PicUploadResult;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class PicUploadFileSystemService {
    //允许上传的格式
    private static final String[] IMAGE_TYPE = new String[]{".bmp",".jpg",".jpeg",".gif",".png"};

    public PicUploadResult upload(MultipartFile uploadFile){
        //校验图片格式
        boolean isLegal = false;
        for (String type :IMAGE_TYPE) {
            if(StringUtils.endsWithIgnoreCase(uploadFile.getOriginalFilename(),type)){
                isLegal = true;
                break;
            }
        }
        //封装result对象 并且将文件的byte数组放置到result对象中
        PicUploadResult fileUploadResult = new PicUploadResult();
        if(!isLegal){
            fileUploadResult.setStatus("error");
            return fileUploadResult;
        }
        //文件路径
        String fileName = uploadFile.getOriginalFilename();
        String filePath = getFilePath(fileName);
        //生成图片的绝对引用地址
        String picUrl = StringUtils.replace(StringUtils.substringAfter(filePath,"E:\code\itcast-haoke\haoke-upload"),"\","/");
        fileUploadResult.setName("http://image.haoke.com"+picUrl);
        File newFile = new File(filePath);
        try{
            uploadFile.transferTo(newFile);
        }catch (IOException e){
            e.printStackTrace();
            //上传失败
            fileUploadResult.setStatus("error");
            return fileUploadResult;
        }
        fileUploadResult.setStatus("done");
        fileUploadResult.setUid(String.valueOf(System.currentTimeMillis()));
        return fileUploadResult;
    }

    private String getFilePath(String fileName) {
        String baseFolder = "E:\code\itcast-haoke\haoke-upload"+File.separator+"images";
        Date nowDate = new Date();
        String fileFolder = baseFolder
                + File.separator
                +new SimpleDateFormat("yyyy").format(nowDate);
        File file = new File(fileFolder);
        if(!file.isDirectory()){
            //如果目录不存在则创建目录
            file.mkdirs();
        }
        //生成新的文件名
        String newFileName = System.currentTimeMillis()+ RandomUtils.nextInt(100,999)+"."+StringUtils.substringAfterLast(fileName,".");
        return fileFolder+File.separator+newFileName;
    }
}
3.postman测试

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/631353.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号