浏览器下载文件

实现效果

QQ20200319-105254@2x

代码如下

public void getReport(HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
                String fileName = URLEncoder.encode("文件名称" + ".xls", StandardCharsets.UTF_8.toString());
                // 下面设置方法可以解决文件名乱码问题
                response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"; filename*=utf-8''" + fileName);
                InputStream in = new FileInputStream(request.getServletContext().getRealPath("/") + "templates/模版地址.xls");
                OutputStream out = response.getOutputStream();
                in.close();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
}

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×