!图片下载错误请求不到是路径错了还是](https://img-ask.csdn.net/upload/201507/27/1437976537_395357.jpg)
package cn.kakabuli.android.response;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 中文名文件下载
*/
public class Demo07 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//服务器要求浏览器以下载方式打开资源
String filename = "广州地铁线路图.jpg";
//中文文件名要进行编码
filename = URLEncoder.encode(filename, "UTF-8");
response.setHeader("content-disposition", "attachment;filename="+filename);
//以下代码就是用流的方式,将服务器的文件输出到浏览器中
InputStream is = this.getClass().getClassLoader().getResourceAsStream("../../images/广州地铁规划图.jpg");
OutputStream os = response.getOutputStream();
byte[] buf = new byte[2048];
int len = 0;
while((len=is.read(buf))>0){
os.write(buf, 0, len);
}
os.close();
is.close();
}
}
download.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>download.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
a:link{
text-decoration:none
}
a:hover{
text-decoration:underline;
color:red
}
</style>
</head>
<body>
广州地铁图.jpg
<!--
/表示webapps目录
-->
<a href="/shulan-day07_request_response/Demo07">下载</a>
</body>
</html>