package com.glxp.api.upload; import cn.hutool.core.exceptions.ExceptionUtil; import cn.hutool.core.thread.ThreadUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.exception.ExceptionUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletResponse; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.charset.StandardCharsets; @Slf4j @RestController public class DownloadController { @Value("${file_path}") private String filePath; @GetMapping(value = "/udiwms/image/register/file/getImage") public void getImage(HttpServletResponse response , @RequestParam String type , @RequestParam String name) throws IOException { // String filePath = new String(name.getBytes("UTF-8"), "UTF-8"); if (name.endsWith("pdf") || name.endsWith("doc")) { OutputStream os = null; try { File file = new File(filePath + "/register/file/" + type + "/" + name); FileInputStream input = new FileInputStream(file); OutputStream out = response.getOutputStream(); byte[] b = new byte[2048]; int len; while ((len = input.read(b)) != -1) { out.write(b, 0, len); } input.close(); } catch (IOException e) { e.printStackTrace(); log.error(e.getMessage()); log.error(new String(ExceptionUtil.getMessage(e).getBytes(StandardCharsets.UTF_8))); log.error("pdf:" + ExceptionUtils.getStackTrace(e)); } finally { if (os != null) { os.flush(); os.close(); } } } else { OutputStream os = null; try { // 读取图片 File file = new File(filePath + "/register/file/" + type + "/" + name); BufferedImage image = ImageIO.read( new FileInputStream(file)); response.setContentType("image/png"); os = response.getOutputStream(); if (image != null) { ImageIO.write(image, "png", os); } } catch (IOException e) { e.printStackTrace(); log.error(e.getMessage()); log.error(new String(ExceptionUtil.getMessage(e).getBytes(StandardCharsets.UTF_8))); log.error("图片:" + ExceptionUtils.getStackTrace(e)); } finally { if (os != null) { os.flush(); os.close(); } } } } @GetMapping(value = "/udiwms/donwload/file") public void download(HttpServletResponse response, @RequestParam String fileName) throws IOException { OutputStream os = null; try { FileInputStream input = new FileInputStream(new File(fileName)); OutputStream out = response.getOutputStream(); byte[] b = new byte[2048]; int len; while ((len = input.read(b)) != -1) { out.write(b, 0, len); } input.close(); } catch (IOException e) { log.error(e.getMessage()); log.error(ExceptionUtil.getMessage(e)); log.error("图片:" + ExceptionUtils.getStackTrace(e)); } finally { if (os != null) { os.flush(); os.close(); } } } @GetMapping(value = "/udiwms/donwload/printFile") public void printFile(HttpServletResponse response, @RequestParam String fileName) throws IOException { String url = "D:\\udi\\udiwms\\udiwmsfile\\pdf\\template\\" + fileName; OutputStream os = null; try { FileInputStream input = new FileInputStream(new File(url)); OutputStream out = response.getOutputStream(); byte[] b = new byte[2048]; int len; while ((len = input.read(b)) != -1) { out.write(b, 0, len); } input.close(); } catch (IOException e) { log.error(e.getMessage()); log.error(ExceptionUtil.getMessage(e)); log.error("图片:" + ExceptionUtils.getStackTrace(e)); } finally { if (os != null) { os.flush(); os.close(); } } } }