java 文件下载,中文表名,中文内容

来源:互联网 发布:网站自己屏蔽广告js 编辑:程序博客网 时间:2024/05/22 17:02


@RequestMapping("userDownloadTemplet")private void userDownloadTemplet(HttpServletRequest request,HttpServletResponse response, String filePath){         try {                    filePath = request.getSession().getServletContext().getRealPath("/WEB-INF/download_templet/用户信息模板.csv");            File file = new File(filePath);            String filename = file.getName(); // 取得文件名。//            String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase(); // 得文件的后缀名。            InputStream fis = new BufferedInputStream(new FileInputStream(filePath)); // 以流的形式下载文件。            byte[] buffer = new byte[fis.available()];            fis.read(buffer);            fis.close();            response.reset(); // 清空response            filename = new String(filename.getBytes("GBK"), "ISO-8859-1");;response.setHeader("Content-Disposition","attachment;filename=" + filename);response.setContentType("application/vnd.ms-excel;");  //设置文件类型              response.setCharacterEncoding("utf-8");              response.addHeader("Content-Disposition", "attachment;filename=" + filename);// 设置response的Header            response.addHeader("Content-Length", "" + file.length());            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());            response.setContentType("application/octet-stream");            toClient.write(buffer);            toClient.flush();            toClient.close();        } catch (IOException ex) {            ex.printStackTrace();        }    }