package com.glxp.api.upload; 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; @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 { if (name.endsWith("pdf") || name.endsWith("doc")) { OutputStream os = null; try { FileInputStream input = new FileInputStream(new File(filePath + "/register/file/" + type + "/" + name)); 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) { } finally { if (os != null) { os.flush(); os.close(); } } } else { OutputStream os = null; try { // 读取图片 BufferedImage image = ImageIO.read( new FileInputStream(new File(filePath + "/register/file/" + type + "/" + name))); response.setContentType("image/png"); os = response.getOutputStream(); if (image != null) { ImageIO.write(image, "png", os); } } catch (IOException e) { e.printStackTrace(); } 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) { } finally { if (os != null) { os.flush(); os.close(); } } } }