首页>代码>基于ssm的爱购商城>/day10_OrderManage/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(); } } }
最近下载更多
微信网友_6806646771208192 LV2
1月3日
淞 LV6
2023年12月21日
tmf852 LV5
2023年12月18日
薛皓柏 LV2
2023年11月20日
yangyang0105 LV2
2023年11月14日
微微qwdrq LV3
2023年11月7日
3263394665 LV9
2023年9月20日
微信网友_6512020493586432 LV2
2023年9月16日
SDLFJL LV6
2023年8月28日
lvllvl LV4
2023年7月16日
最近浏览更多
自由吃柚子 LV2
3月26日
Ayachi
3月22日
暂无贡献等级
wwwwca LV1
3月21日
夜上清元 LV8
3月15日
SZEPEZS LV8
3月12日
pangzhihui LV11
3月3日
1134116035
2月24日
暂无贡献等级
fangen0005 LV25
2月19日
lihao0217 LV1
2月15日
liiiyou LV1
1月26日