文件写入response

String agent = (String)request.getHeader("USER-AGENT");   
					 
					 if (agent != null && agent.indexOf("MSIE") == -1) {// FF      
						 String enableFileName = "=?UTF-8?B?" + (new String(Base64.encodeBase64(filename.getBytes("UTF-8")))) + "?=";
						 response.setHeader("Content-Disposition", "inline; filename=" + enableFileName);
					 } else { // IE      
						 String enableFileName = new String(filename.getBytes("GBK"), "ISO-8859-1");
						 response.setHeader("Content-Disposition", "inline; filename=" + enableFileName);
					 }
					 
					 response.setContentType("application/pdf");

// 读出文件到response
					// 这里是先需要把要把文件内容先读到缓冲区
					// 再把缓冲区的内容写到response的输出流供用户下载
					FileInputStream fileInputStream = new FileInputStream(f);
					BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
					byte[] b = new byte[bufferedInputStream.available()];
					bufferedInputStream.read(b);
					OutputStream outputStream = response.getOutputStream();
					outputStream.write(b);
					// 人走带门
					bufferedInputStream.close();
					outputStream.flush();
					outputStream.close();

 

你可能感兴趣的:(java)