首页>代码>基于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 前天
五折也挺好的  LV13 3月18日
orang801  LV2 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日
最近浏览更多
hrose12  LV21 前天
luhong  LV4 6月11日
KAIzx11  LV8 5月10日
sn764889012 4月16日
暂无贡献等级
xuelia 4月8日
暂无贡献等级
orang801  LV2 2月20日
ryadmin123  LV2 2月12日
cherrylxj  LV3 1月13日
微信网友_7041036943331328  LV7 2024年12月31日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友