package com.gyee.runeconomy.util;

import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * 文件处理工具类
 *
 * @author gyee
 * @date: 2018年9月22日 下午10:33:31
 */
public class FileUtils {

    // 设置目标文件的映射路径
    @Value("classpath:static/syz")
    private Resource syz;

    private FileUtils() {
    }

    /**
     * 输出指定文件的byte数组
     *
     * @return
     */
    public static void writeBytes(String filePath, OutputStream os) throws IOException {
        FileInputStream fis = null;
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                throw new FileNotFoundException(filePath);
            }
            fis = new FileInputStream(file);
            byte[] b = new byte[1024];
            int length;
            while ((length = fis.read(b)) > 0) {
                os.write(b, 0, length);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    /**
     * 删除文件
     *
     * @param filePath 文件
     * @return
     */
    public static boolean deleteFile(String filePath) {
        boolean flag = false;
        File file = new File(filePath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }

    /**
     * 读取图片地址 输出到页面
     *
     * @param request
     * @param response
     * @param fileurl
     * @throws IOException
     */
    public static void readIMGTohtml(HttpServletRequest request, HttpServletResponse response, String fileurl) throws IOException {
        //设置发送到客户端的响应内容类型
        response.setContentType("image/*");
        //读取本地图片输入流
        try (FileInputStream inputStream = new FileInputStream(fileurl); OutputStream out = response.getOutputStream()) {
            int i = inputStream.available();
            //byte数组用于存放图片字节数据
            byte[] buff = new byte[i];
            inputStream.read(buff);
            out.write(buff);
        }
    }


}
