jsp实现文件下载的代码

OutputStream out=response.getOutputStream();
			  byte by[]=new byte[500];
			  File fileLoad=new File(path);
			  response.reset();
			  response.setContentType("application/vnd.ms-excel");
			  response.setHeader("content-disposition","attachment; filename=text.xls");
			  long fileLength=fileLoad.length();
			  String length1=String.valueOf(fileLength);
			  response.setHeader("Content_Length",length1);
			  FileInputStream in=new FileInputStream(fileLoad);
			  int n;
			  while((n=in.read(by))!=-1){
			   out.write(by,0,n);
			  }
			  
			  in.close();
			  out.flush();

以上代码以下载excel为例,各种文件的下载大体相同只需改变相应的contentType即可。
其中response.setContentType()的String参数及对应类型为

  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 

你可能感兴趣的:(Java)