自启动servlet也叫自动实例化servlet
通过在web.xml中的
<servlet>
<servlet-name>autostart</servlet-name>
<servlet-class>www.servlet.AutoStart</servlet-class>
<!-- 值的大小决定创建的先后顺序-->
<load-on-startup>1</load-on-startup>
</servlet>
1)在servlet标签中添加要获取的文件的路径,并设置该Servlet为自启动
<servlet>
<servlet-name>autostart</servlet-name>
<servlet-class>www.servlet.AutoStart</servlet-class>
<!--添加图片的路径-->
<init-param>
<param-name>path</param-name>
<param-value>image</param-value>
</init-param>
<!-- 值的大小决定创建的先后顺序,该标签必须放到servlet标签的最下面-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>autostart</servlet-name>
<url-pattern>/autostart.do</url-pattern>
</servlet-mapping>
2)在自启动Servlet的init()方法中通过ServletConfig对象获取init-param的name值,并放到全局容器ServletContext中,
这样当Tomcat启动时init方法就会被执行,图片的路径被存放到全局容器中,所有Servlet都能拿到
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class AutoStart extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
public void init() throws ServletException {
//ServletConfig只对当前servlet有效
ServletConfig servletConfig=this.getServletConfig();
String path=servletConfig.getInitParameter("path");
//获取全局容器对象
ServletContext servletContext=this.getServletContext();
//将获取的path放到全局容器ServletContext中,所有servlet都能获取
servletContext.setAttribute("path",path);
System.out.println("AutoStartServlet");
}
}
3)文件下载的Servlet
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@WebServlet("/filedown")
public class FileDown extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取ServletContext对象,继承自GenericServlet类
ServletContext servletContext=this.getServletContext();
//获取存放图片的目录名称
String path=(String) servletContext.getAttribute("path");
//将相对路径转换为绝对路径
String realpath=servletContext.getRealPath(path+"/樱花.jpg");
System.out.println(realpath);
//读取要下载的文件
// File file=new File("D:\\樱花.jpg");
File file=new File(realpath);
//用字节输入流存储文件
InputStream is= new FileInputStream(file);
//创建字节数组
byte[] buff=new byte[is.available()];
is.read(buff);
//在响应添加附加信息
// resp.addHeader("Content-Disposition", "attachment; filename="+file.getName());
//将原来的文件名按照gbk的方式变成字节,再按iso-8859-1编码方式变成新的字符串
resp.addHeader("Content-Disposition","attachment;filename="+
new String(file.getName().getBytes("gbk"),"iso-8859-1"));
//产生响应
//创建字节输出流
OutputStream os=resp.getOutputStream();
//响应到客户端
os.write(buff);
//刷新流
os.flush();
//关闭流
os.close();
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章