@RestController public class ReadAppController { //文件的名字 @Value("${app.name}") private String appName; @GetMapping("get/app") private void getAPP(HttpServletResponse response) throws IOException { File file = new File("src/main/resources/static/" + appName); InputStream br = new FileInputStream(file); OutputStream out = response.getOutputStream();
//设置返回文件的名字 response.setHeader("Content-Disposition", "inline; filename=" + URLEncoder.encode(appName, "UTF-8"));
//设置返回值的类型 response.setHeader("content-type", "application/octet-stream"); byte[] buf = new byte[1024]; int len = 0; while ((len = br.read(buf)) != -1) { out.write(buf, 0, len); } //关闭资源 br.close(); out.close(); } }