文件下载servlet端代码模版

private void downFile(File file,HttpServletResponse response) throws IOException {
		// 写数据到客户端
        response.setContentType("application/octet-stream");
        response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Pragma", "public");
        //设置超时时间
        response.setDateHeader("Expires",(System.currentTimeMillis() + 5000));
        response.addHeader("Content-Disposition","attachment; filename=\""+ file.getName() + "\"");
		FileInputStream in = new FileInputStream(file);
		OutputStream out = response.getOutputStream();
		int len = 0;
		byte[] buffer = new byte[1024];
		while((len=in.read(buffer))!=-1){
			out.write(buffer, 0, len);
			out.flush();
		}
		out.close();
		in.close();
	}

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页