web响应不同类型的文件

web响应不同类型的文件_第1张图片


    @GetMapping("/show")
    public void show(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String filename = request.getParameter("filename");
        String mimeType = request.getServletContext().getMimeType(filename);
        File file = new File("E:/" + filename);
        response.setContentType(mimeType);
        FileInputStream fileInputStream = new FileInputStream(file);
        ServletOutputStream outputStream = response.getOutputStream();
        int len = 0;
        byte[] bytes = new byte[8 * 1024];
        while ((len = fileInputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, len);
        }
        fileInputStream.close();
        outputStream.close();
    }

web响应不同类型的文件_第2张图片

 

你可能感兴趣的:(web)