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;
}
}