下载txt

  不好意思,最近工作忙,没上来看贴.我在这把代码给大家下:

 Java代码

 <meta?http-equiv="Content-Type"?content="text/html;?charset=gbk">

 <HTML>

 <HEAD>

 </HEAD>

 <BODY>

 <a?href?=?"download1.jsp?filepath=d:\\&filename=1a.txt"?>downloadtest1</a>

 </BODY>

 </HTML>

 <meta http-equiv="Content-Type" content="text/html; charset=gbk">

 <HTML>

 <HEAD>

 </HEAD>

 <BODY>

 <a href = "download1.jsp?filepath=d:\\&filename=1a.txt" >downloadtest1</a>

 </BODY>

 </HTML>

 这是页面上的连接,我传了个路径和文件名

 Java代码

 <%?

 ?String?filename?=?request.getParameter("filename");//"1a.txt";?

 ?String?filepath?=?request.getParameter("filepath");//"d:\\";

 int?i?=?0;

 ?response.setContentType("application/octet-stream");

 ?response.setHeader("Content-Disposition","attachment;filename?=?"+filename);?

 ?java.io.FileInputStream?fileInputStream?=?new?java.io.FileInputStream(filepath+filename);

 ?while((i=?fileInputStream.read())?!=?-1){

 out.write(i);

 ?}

 %>

 <%

  String filename = request.getParameter("filename");//"1a.txt";

  String filepath = request.getParameter("filepath");//"d:\\";

  int i = 0;

  response.setContentType("application/octet-stream");

  response.setHeader("Content-Disposition","attachment;filename = "+filename);

  java.io.FileInputStream fileInputStream = new java.io.FileInputStream(filepath+filename);

  while((i= fileInputStream.read()) != -1){

  out.write(i);

  }

 %>

 这是被调用的download1.jsp,这个jsp就是执行直接下载文件的不管是txt还是word文档都可以直接下载

 这个东东是我在一个专业编程杂志的网站花3块钱买的,嘿嘿.没办法,项目急用,今天上来看到有人需要赶紧发出来,希望3,4楼还能看到.

 out.clear();

 out = pageContext.pushBody();

 <%@page language="java" import=".*" pageEncoding="gb2312"%>

 <%

  response.setContentType("application/x-download");//设置为下载application/x-download

  String filenamedownload = "/shushan.txt";//即将下载的文件的相对路径

  String filenamedisplay = "shushan.txt";//下载文件时显示的文件保存名称

  filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8");

  response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);

 

  try

  {

  RequestDispatcher dispatcher = application.getRequestDispatcher(filenamedownload);

  if(dispatcher != null)

  {

  dispatcher.forward(request,response);

  }

  response.flushBuffer();

  }

  catch(Exception e)

  {

  e.printStackTrace();

  }

  finally

  {

  out.clear();

 out = pageContext.pushBody();

 

  }

 %>

 <%@page language="java" contentType="application/x-msdownload" import="java.io.*,.*" pageEncoding="UTF-8"%>

 <%

  String txt ="实验";

 System.out.println(txt);

  response.setContentType("application/txt");

  response.setHeader("Content-disposition", "attachment;filename=table.txt");

  BufferedOutputStream bos = null;

  try {

  bos = new BufferedOutputStream(response.getOutputStream());

  bos.write(txt.getBytes());

  } catch (IOException e) {

  throw e;

  } finally {

  if (bos != null)

  bos.close();

  out.clear();

  out = pageContext.pushBody();

 

  }

 %>

 <%@page language="java" contentType="application/x-msdownload" import="java.io.*,.*" pageEncoding="gb2312"%><%

  response.reset();//可以加也可以不加

  response.setContentType("application/x-download");

  String filenamedownload = "D:\\shushan.txt";

  String filenamedisplay = "shushan.txt";//系统解决方案.txt

  filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8");

  response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);

  OutputStream output = null;

  FileInputStream fis = null;

  try

  {

  output = response.getOutputStream();

  fis = new FileInputStream(filenamedownload);

  byte[] b = new byte[1024];

  int i = 0;

  while((i = fis.read(b)) > 0)

  {

  output.write(b, 0, i);

  }

  output.flush();

  }

  catch(Exception e)

  {

  System.out.println("Error!");

  e.printStackTrace();

  }

  finally

  {

  if(fis != null)

  {

  fis.close();

  fis = null;

  }

  if(output != null)

  {

  output.close();

  output = null;

  }

  out.clear();

 out = pageContext.pushBody();

 

  }

 %>

推荐访问:机动风暴txt全文下载 下载 txt