一.
HTTP Status 405 - HTTP method POST is not supported by this URL type Status report message HTTP method POST is not supported by this URL description The specified HTTP method is not allowed for the requested resource (HTTP method POST is not supported by this URL). Apache Tomcat/6.0.32
实现图片下载
public class DownLoadImage extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
----------
//调用doPost方法时,super忘记去掉
------------------------
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 1. 拿到图片路径
String path = req.getServletContext().getRealPath("/1.jpg");
// 2.设置响应头
// 2.1设置响应类型
String type = req.getServletContext().getMimeType(".jpg");
resp.setContentType(type);
// 2.2设置响应正文长度
FileInputStream in = new FileInputStream(path);
resp.setContentLength(in.available());
// 2.3设置待下载文件名
resp.addHeader("Content-Disposition", "attachment;filename=downLoad");
// 输出响应文件
OutputStream out = resp.getOutputStream();
byte[] b = new byte[1024];
int length = 0;
while ((length = in.read(b)) != -1) {
out.write(b, 0, length);
}
in.close();
}
}
二.
警告: Failed to retrieve JNDI naming context for container [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/downloadServle]] so no cleanup was performed for that container
javax.naming.NamingException: No naming context bound to this class loader
at org.apache.naming.ContextBindings.getClassLoader(ContextBindings.java:350)
at org.apache.catalina.deploy.NamingResources.cleanUp(NamingResources.java:985)
at org.apache.catalina.deploy.NamingResources.stopInternal(NamingResources.java:968)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:221)
at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5846)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:221)
at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:259)
at org.apache.catalina.core.ContainerBase.removeChild(ContainerBase.java:1030)
at org.apache.catalina.core.ContainerBase.destroyInternal(ContainerBase.java:1230)
at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:292)
at org.apache.catalina.core.ContainerBase.removeChild(ContainerBase.java:1030)
at org.apache.catalina.core.ContainerBase.destroyInternal(ContainerBase.java:1230)
at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:292)
at org.apache.catalina.core.StandardService.destroyInternal(StandardService.java:594)
at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:292)
at org.apache.catalina.core.StandardServer.destroyInternal(StandardServer.java:846)
at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:292)
at org.apache.catalina.startup.Catalina.start(Catalina.java:697)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:294)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:428)
只要是出现 [StandardEngine[Catalina],说明配置文件.xml有问题