Servlet中文图片下载--学习笔记
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet中文图片下载--学习笔记相关的知识,希望对你有一定的参考价值。
java源码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Filedown extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext context = this.getServletContext();
String path = context.getRealPath("/images/神奇的图片.JPG");
File file = new File(path);
response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(file.getName(),"UTF-8"));
//输入流
InputStream is = new FileInputStream(file);
//输出流
OutputStream os = response.getOutputStream();
//字节流
byte[] buf = new byte[1024];
int len = 0;
while((len = is.read(buf)) > 0)
{
os.write(buf,0,len);
}
is.close();
os.close();
}
}
html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>download.html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<a href="/day04/Filedown">下载中文文件</a>
</body>
</html>
结果
以上是关于Servlet中文图片下载--学习笔记的主要内容,如果未能解决你的问题,请参考以下文章