Spring boot 图片上传

package com.hey.liyankun.admin.controller;


import com.hey.liyankun.admin.pojo.HeyImage;
import com.hey.liyankun.admin.service.ImageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

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

@RestController
@RequestMapping(value="/hey/boke/admin/")
public class UploadController {
       @Autowired
    private ImageService imageService;
    //获取 application.properties 中配置的上传文件的路径
    @Value("${file.path}")
    private String filePath;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @RequestMapping(value = "upload",method = RequestMethod.POST)
    public String fileUpload(MultipartFile file, ModelMap modelMap) throws IOException {
        //xxx.jpg  ===>>  .jpg ,获取文件名并且截取后缀
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        //随机生成文件名加上后缀
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date nowdate = new Date();
        String sdfdate = sdf.format(nowdate);
        String pro_date = sdfdate.substring(0,10);
        String[] pro_arr = pro_date.split("-");
        String parent_dirname = pro_arr[0]+pro_arr[1]+pro_arr[2];
        String post_date  = sdfdate.substring(10,19).trim();
        String[] post_arr = post_date.split(":");
        String sun_file = post_arr[0]+post_arr[1]+post_arr[2];
        String filename = parent_dirname.toString()+sun_file.toString()+suffix;
        logger.info("logback  日志 --->>> 访问文件上传接口");
        //把文件通过流的方式写入
        FileCopyUtils.copy(file.getInputStream(),new FileOutputStream(new File(filePath+filename)));
        System.out.println("图片路径:"+filePath+filename);
        modelMap.addAttribute("image",(filePath+filename));
        HeyImage heyImage = new HeyImage();
        heyImage.setUrl(filePath+filename);
        heyImage.setMimeType(suffix);
        Integer i = imageService.saveImage(heyImage);
        if(i==1){
            return filePath;
        }
        return null;
    }
}
@Autowired
    private ImageService imageService;
    //获取 application.properties 中配置的上传文件的路径
    @Value("${file.path}")
    private String filePath;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @RequestMapping(value = "upload",method = RequestMethod.POST)
    public String fileUpload(MultipartFile file, ModelMap modelMap) throws IOException {
        //xxx.jpg  ===>>  .jpg ,获取文件名并且截取后缀
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        //随机生成文件名加上后缀
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date nowdate = new Date();
        String sdfdate = sdf.format(nowdate);
        String pro_date = sdfdate.substring(0,10);
        String[] pro_arr = pro_date.split("-");
        String parent_dirname = pro_arr[0]+pro_arr[1]+pro_arr[2];
        String post_date  = sdfdate.substring(10,19).trim();
        String[] post_arr = post_date.split(":");
        String sun_file = post_arr[0]+post_arr[1]+post_arr[2];
        String filename = parent_dirname.toString()+sun_file.toString()+suffix;
        logger.info("logback  日志 --->>> 访问文件上传接口");
        //把文件通过流的方式写入
        FileCopyUtils.copy(file.getInputStream(),new FileOutputStream(new File(filePath+filename)));
        System.out.println("图片路径:"+filePath+filename);
        modelMap.addAttribute("image",(filePath+filename));
        HeyImage heyImage = new HeyImage();
        heyImage.setUrl(filePath+filename);
        heyImage.setMimeType(suffix);
        Integer i = imageService.saveImage(heyImage);
        if(i==1){
            return filePath;
        }
        return null;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值