2012-06-19 53 views
4

我想在服務器上傳pdf文件。我使用以下代碼塊進入控制器:異常java.lang.NoSuchMethodError

@RequestMapping(value = /submit, method = RequestMethod.POST) 
public String upload(UploadItem uploadItem, BindingResult result, HttpServletRequest request, HttpSession session) { 

    //some code here 

    String name = request.getServletContext().getRealPath("/pdf/" + filename); 
    File dest = new File(name); 
    try { 
     file.transferTo(dest); 
    }catch(Exception e){ 
     System.err.println(e); 
    } 

    return "redirect:/details"; 

我這樣做是爲了將pdf存儲到pdf文件中。在我的本地主機工作正常,但是當我執行該服務器上的我正在以下異常:

exception 

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getServletContext()Ljavax/servlet/ServletContext; 
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:839) 
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719) 
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644) 
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:637) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) 
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) 

root cause 

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getServletContext()Ljavax/servlet/ServletContext; 
frontend.controller.EsteemRatingsController.handleFormUpload(EsteemRatingsController.java:113) 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
java.lang.reflect.Method.invoke(Method.java:601) 
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176) 
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436) 
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424) 
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790) 
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719) 
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644) 
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:637) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) 
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) 

如果我刪除,在控制器類以上提供線工作(ofcource,而不上傳PDF文件)。誰能幫我這個?

+0

什麼Web容器您使用的?看起來你正在跑老東西。 –

+0

Apache tomcat 6 –

回答

21

該方法request.getServletContext()是在servlet 3.0中引入的。確保你的容器/庫支持該版本。

編輯:tomcat的6只的servlet 2.5,看到http://tomcat.apache.org/whichversion.html

可以自動裝配:ServletContext and Spring MVC

public class Xxxx{ 
    @Autowired 
    ServletContext context; 

    @RequestMapping(value = "/submit", method = RequestMethod.POST) 
    public String upload(UploadItem uploadItem, BindingResult result, HttpServletRequest request, HttpSession session) { 

     //some code here 

     String name = context.getRealPath("/pdf/" + filename); 
... 
+0

因此,而不是這種方法,我可以使用哪種方法,Apache 6.0.24可以支持它? –

+0

可以爲你注入'ServletContext'嗎?還沒有嘗試過,但我想它可以。 –

+0

非常感謝您的答覆和您的時間。我在互聯網上看到,它可以,但我在這一切都很新,我不明白。你能幫我嗎? –

相關問題