首页>代码>基于SpringBoot的爱购商城>/day15_eshop/src/main/java/com/eshop/controller/admin/CommonImgController.java
package com.eshop.controller.admin;
import com.eshop.utils.CodeUtil;
import com.eshop.utils.ImgPathConfig;
import com.eshop.utils.UUIDUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
@Controller
@RequestMapping("/common")
public class CommonImgController {
@Autowired
private ImgPathConfig imgPathConfig;
@RequestMapping("/getImage")
public void getImage(String image, HttpServletResponse response) {
try {
File file = new File(imgPathConfig.getImgPath() , image);
if (file.exists()) {
ServletOutputStream os = response.getOutputStream();
FileInputStream is = new FileInputStream(file);
//边读边写
IOUtils.copy(is,os);
}
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping("/upload")
@ResponseBody
public String upload(@RequestParam(name = "file") MultipartFile mf) throws JsonProcessingException {
String path = imgPathConfig.getImgPath();
String imgName = mf.getOriginalFilename();
SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMdd");
String date = df2.format(new Date());
imgName = UUIDUtils.getId() + date+".jpg";
System.out.println(imgName);
try {
File file = new File(path,imgName);
mf.transferTo(file);//上传
} catch (Exception e) {
e.printStackTrace();
}
ObjectMapper objectMapper = new ObjectMapper();
String s = objectMapper.writeValueAsString(imgName);
return s;
}
@RequestMapping("/code")
@ResponseBody
public void code(HttpServletResponse response, HttpSession session){
try {
//1.将一个图片文件写进指定的输出流中
String codeText = CodeUtil.generateCodeAndPic(response.getOutputStream());
//2.将验证码文本存进session中
session.setAttribute("code",codeText);
} catch (IOException e) {
e.printStackTrace();
}
}
}
最近下载更多
hrose12 LV21
2025年6月14日
五折也挺好的 LV13
2025年3月18日
orang801 LV2
2025年2月20日
cys18318001508 LV12
2024年11月28日
daixinheng LV2
2024年11月26日
微信网友_6377331253415936 LV3
2024年11月24日
yvimib LV3
2024年10月26日
微信网友_6786215447367680 LV5
2024年10月8日
TY0165 LV20
2024年6月22日
A1314521anna LV1
2024年5月18日
最近浏览更多
微信网友_7825562983419904
2025年12月23日
暂无贡献等级
微信网友_7823843693924352 LV1
2025年12月23日
半夏bx LV16
2025年12月1日
奋斗的小蚂蚁 LV17
2025年11月7日
不爱吃香菜 LV1
2025年11月5日
Yimi1919 LV3
2025年9月8日
java萌新sc LV5
2025年8月26日
微信网友_7596468628459520
2025年7月14日
暂无贡献等级
段池卿 LV5
2025年7月12日
SpriteEX LV1
2025年7月8日

