第一种:

就是直接给出下载的地址,这种方式很不好,因为会暴露你的地址,带来很多不安全的因素,可以说是千万不要用这种

第二种:

下载页面

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

<html>

<head> 

<title>download</title>

</head> 

<body>

<a href="xia.jsp?filename=新建文档.txt">新建文档.txt</a>

</body>

</html>

然后编写如下页面

<%@ page contentType="text/html;charset=gbk"%>

<%@ page language="java" import="java.io.*,java.net.*" pageEncoding="gbk"%>

<html>

<head> 

<title>test</title>

</head> 

<body>

<% 

response.setContentType("text/html");

javax.servlet.ServletOutputStream ou = response.getOutputStream();

String filepath="uploadfile/";

String filename=new String(request.getParameter("filename").getBytes("ISO8859_1"),"GB2312").toString();

System.out.println("DownloadFile filepath:" + filepath);

System.out.println("DownloadFile filename:" + filename);

java.io.File file = new java.io.File(filepath + filename);

if (!file.exists()) {

System.out.println(file.getAbsolutePath() + " 文件不存在!");

return;

}

// 读取文件流

java.io.FileInputStream fileInputStream = new java.io.FileInputStream(file);

// 下载文件

// 设置响应头和下载保存的文件名

if (filename != null && filename.length() > 0) {

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

response.setHeader("Content-Disposition", "attachment; filename=" + new String(filename.getBytes("gb2312"),"iso8859-1") + "");

if (fileInputStream != null) {

int filelen = fileInputStream.available();

//文件太大时内存不能一次读出,要循环

byte a[] = new byte[filelen];

fileInputStream.read(a);

ou.write(a);

}

fileInputStream.close();

ou.close();

}

%> 

</body>

</html>

第三种方法

使用servlet 首先配置web.xml

<servlet>

<servlet-name>DownloadFile</servlet-name>

<servlet-class>libin123.com.cn.servlet.DownloadFile</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>DownloadFile</servlet-name>

<url-pattern>/downloadfile</url-pattern>

</servlet-mapping>

编写DownloadFile.java类

package libin123.com.cn.servlet;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.sun.image.codec.jpeg.ImageFormatException;

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGImageDecoder;

public class DownloadFile extends HttpServlet {

private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html");

javax.servlet.ServletOutputStream out = response.getOutputStream();

String filepath=request.getRealPath("/") + "uploadfile/";

String filename=new String(request.getParameter("filename").getBytes("ISO8859_1"),"GB2312").toString();

System.out.println("DownloadFile filepath:" + filepath);

System.out.println("DownloadFile filename:" + filename);

java.io.File file = new java.io.File(filepath + filename);

if (!file.exists()) {

System.out.println(file.getAbsolutePath() + " 文件不存在!");

return;

}

// 读取文件流

java.io.FileInputStream fileInputStream = new java.io.FileInputStream(file);

// 下载文件

// 设置响应头和下载保存的文件名

if (filename != null && filename.length() > 0) {

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

response.setHeader("Content-Disposition", "attachment; filename=" + new String(filename.getBytes("gb2312"),"iso8859-1") + "");

if (fileInputStream != null) {

int filelen = fileInputStream.available();

//文件太大时内存不能一次读出,要循环

byte a[] = new byte[filelen];

fileInputStream.read(a);

out.write(a);

}

fileInputStream.close();

out.close();

}

}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN>");

out.println("<HTML>");

out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");

out.println(" <BODY>");

out.print(" This is ");

out.print(this.getClass().getName());

out.println(", using the POST method");

out.println(" </BODY>");

out.println("</HTML>");

out.flush();

out.close();

}

下载页面

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

<html>

<head>

</head> 

<body>

<a href="downloadfile?filename=新建文档.txt">新建文档.txt</a>

</body>

</html>