首页>代码>基于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();
        }
    }

}
最近下载更多
orang801  LV2 2月20日
taoshen95  LV16 2024年12月21日
微信网友_7041036943331328  LV7 2024年12月15日
daixinheng  LV2 2024年11月26日
微信网友_6377331253415936  LV3 2024年11月23日
lyh1989  LV34 2024年7月21日
lyt010628  LV4 2024年7月9日
张三12348613  LV3 2024年6月26日
全斐  LV6 2024年6月19日
MrDuan  LV2 2024年5月15日
最近浏览更多
412685265123 4月12日
暂无贡献等级
YiRenHun  LV11 3月23日
orang801  LV2 2月20日
xiaoaitx  LV8 1月1日
mianju 2024年12月19日
暂无贡献等级
ma406805131  LV19 2024年12月18日
微信网友_7041036943331328  LV7 2024年12月15日
txf123  LV6 2024年12月10日
sjwc88  LV3 2024年12月4日
zolscy  LV24 2024年11月28日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友