JAVAWEB 文件下载
<body>
<hr/>
<a href="/prictice/servlet?filename=hibernate-release-5.0.7.Final.zip">download--ZIP</a>
<a href="/prictice/servlet?filename=love(0).png">download-PNG</a>
</body>
</html>
@WebServlet("/servlet")
public class ServeltDownLoad extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String filename=req.getParameter("filename");
System.out.println(filename);
resp.setContentType(getServletContext().getMimeType(filename));
resp.setHeader("Content-Disposition", "attachment;filename="+filename);
String fullfilename = getServletContext().getRealPath("/domn/"+filename);
InputStream in = new FileInputStream(fullfilename);
System.out.println("fullname:"+fullfilename);
OutputStream out = resp.getOutputStream();
byte[] str=new byte[2048];
int len;
while((len=in.read(str))!=-1){
out.write(str, 0, len);
}
in.close();
out.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}