1.下载文件
class FileServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String path="D:\\JavaKuang\\javaweb-02-servlet\\response\\src\\main\\resources\\123.png";
String name=path.substring(path.lastIndexOf("\\")+1);
resp.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(name,"utf-8"));
FileInputStream in=new FileInputStream(path);
int len=0;
byte[] buffer=new byte[1024];
ServletOutputStream out=resp.getOutputStream();
while ((len=in.read(buffer))>0){
out.write(buffer,0,len);
}
in.close();
out.close();
- 重定向
resp.sendRedirect("地址");
- 聊一聊请求转发和重定向的区别
- 相同点: 都会跳转页面
- 不同点:请求转发 307
-
重定向,地址栏会发生变化 302