package com.winsun.controller;

import com.winsun.constant.FilePath;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @Author xuede
 * @Date 2020/3/6 16:44
 * @Version 1.0
 */
@Slf4j
@RestController
public class ImgController {

    /**
     * 服务器访问路径
     */
    private static String pathPre = "/springboot/school_center/manager/";

    //menuname = "图片路径"  只支持jpeg 和 png
    @RequestMapping( value = "/images/**",produces ={MediaType.IMAGE_JPEG_VALUE,MediaType.IMAGE_PNG_VALUE} )
    public byte[] getbackground(HttpServletRequest request) {
        byte[] bytes=new byte[10];
        request.getServletPath();
        String  imgurl= pathPre + FilePath.BACKGROUNDIMG.getValue()+"/"+request.getServletPath();
        imgurl = imgurl.replace("//", "/");
        File file = new File(imgurl);
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
            bytes = new byte[inputStream.available()];
            if (inputStream != null) {
                inputStream.read(bytes, 0, inputStream.available());
            }
        } catch (IOException e) {
            log.error("图片下载失败",e.getMessage());
        }finally {
            if (inputStream != null) {
                try{
                    inputStream.close();
                }catch (Exception e){}
            }
        }
        return bytes;
    }
}
