moonorongo
2/23/2015 - 7:45 PM

Forzar descarga de algo, en lo que querramos

Forzar descarga de algo, en lo que querramos

PrintWriter out = response.getWriter();
		String fileName = "log.txt\\";
		
		File f= new File(fileName);
	  String lastModified = new SimpleDateFormat("dd-MM-yyyy").format(new Date(f.lastModified()));
		
		response.setContentType("application/force-download");
		response.setContentLength((int)f.length());
		response.setHeader("Content-Transfer-Encoding", "binary");
		response.setHeader("Content-Disposition","attachment; filename=\"reporte_"+ lastModified +".txt\"");

	    BufferedReader br = null;
	    br = new BufferedReader(new FileReader(fileName));
	    String line;
	    
	    while ((line=br.readLine())!=null) {
	    	out.print(line + "\r\n");
	    }

	    br.close();
	    out.close();
	    
	    br = null;
	    out = null;