.html代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body> <h1>
使用<a>标签的方式实现文件下载
</h1>
<a href="/WEB14/download/a.txt">a.txt</a>
<br>
<a href="/WEB14/download/a.jpg">a.jpg</a> <h1>使用服务器端编码的方式实现文件下载</h1>
<a href="/WEB14/downloadServlet?filename=a.txt">a.txt</a>
<br>
<a href="/WEB14/downloadServlet?filename=a.jpg">a.jpg</a>
<br>
<a href="/WEB14/downloadServlet2?filename=你的名字.txt">你的名字</a> </body>
</html>
1.DownloadServlet()代码
package com.hdh.content; import java.io.FileInputStream;
import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class DownloadServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { //1.获取下载文件的名称
String filename=request.getParameter("filename");
//*告诉客户端这个文件不是解析 而是以附件的形式下载
response.setHeader("Content-Disposition", "attachment;filename="+filename);
//2.获取文件的绝对路径
String path=this.getServletContext().getRealPath("download/"+filename);
//3.获得该文件的输入流
FileInputStream in=new FileInputStream(path);
//获得输出流---通过response获得的输出流 用于向客户端写内容
ServletOutputStream out=response.getOutputStream();
//拷贝文件
int len=0;
byte [] buffer=new byte[1024]; while((len=in.read(buffer))>0) {
out.write(buffer, 0, len);
}
in.close();
out.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }
2.在服务器上运行后出现图片无法下载而是直接解析:
需要不进行解析而是以附件的形式下载:告诉客户端这个文件不是解析 而是以附件的形式下载
加上:response.setHeader("Content-Disposition", "attachment;filename="+filename);
package com.hdh.content; import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream; import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class DownloadServlet2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* 解决下载 你的名字.txt时乱码的问题
*/
//1.获取下载文件的名称
String filename=request.getParameter("filename"); filename=new String(filename.getBytes("ISO8859-1"),"UTF-8");
//*告诉客户端这个文件不是解析 而是以附件的形式下载
response.setHeader("Content-Disposition", "attachment;filename="+filename);
//2.获取文件的绝对路径
String path=this.getServletContext().getRealPath("download/"+filename);
//3.获得该文件的输入流
InputStream in=new FileInputStream(path);
//获得输出流---通过response获得的输出流 用于向客户端写内容
ServletOutputStream out=response.getOutputStream();
//拷贝文件
int len=0;
byte [] buffer=new byte[1024]; while((len=in.read(buffer))>0) {
out.write(buffer, 0, len);
}
in.close();
out.close();
} protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }
3.如果在获取中文文件名时,文件名时乱码问题
filename=new String(filename.getBytes("ISO8859-1"),"UTF-8");