这个问题已经在这里有一个答案:
- HTTP状态405 - HTTP GET方法不受此URL支持 2个回答
下面的代码是从一本书,所以它会不会是incorrect.But我不知道如何解决这个下面error.When删除方法的doGet(),同样的错误!
“HTTP状态405 - HTTP GET方法不受此URL支持”
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class PDFServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException{
this.doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException{
response.setContentType("application/pdf");
ServletOutputStream out=response.getOutputStream();
File pdf=null;
BufferedInputStream buf=null;
try{
pdf=new File("C:\\Users\\lk\\Desktop\\Desktop\\ example.pdf");
response.setContentLength((int)pdf.length());
FileInputStream input=new FileInputStream(pdf);
buf=new BufferedInputStream(input);
int readBytes=0;
while((readBytes=buf.read())!=-1) out.write(readBytes);
}catch(IOException e){
System.out.println("file not found!");
}finally{
if(out!=null) out.close();
if(buf!=null) buf.close();
}
}
}
web.xml中:
<?xml version="1.0" encoding="UTF-8"?>
-<web-app xsi:.........." version="2.5">
-<servlet>
<description>This is the description of my Java EE component</description>
<display-name>This is the display name of my Java EE component</display-name>
<servlet-name>PDFServlet</servlet-name>
<servlet-class>PDFServlet</servlet-class>
</servlet>
-<servlet-mapping>
<servlet-name>PDFServlet</servlet-name>
<url-pattern>/PDFServlet</url-pattern>
</servlet-mapping>
-<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
-<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>