2012-02-27 3 views
0

GWT에서 서블릿으로 작업하려고합니다. 그리고 나는 오류를 발견했다.GWT에서 서블릿이 작동하지 않습니다.

No file found for: /uploadfile/uploadFileServlet 

파일을 찾아 서버 측에서 보내고 싶습니다.

저는 전문가가 제공 한 많은 솔루션을 경험했습니다. 하지만 내 실수를 찾을 수 없습니다.

일부 해결 방법은 도움이됩니다.

클라이언트 측

package uploadfile.client; 
public class Uploadfile implements EntryPoint { 

    @SuppressWarnings("deprecation") 
    public void onModuleLoad() { 
     // TODO Auto-generated method stub 
      final FormPanel uploadForm = new FormPanel(); 

      uploadForm.setAction(GWT.getModuleBaseURL() +"uploadFileServlet"); 

      uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART); 
      uploadForm.setMethod(FormPanel.METHOD_POST); 

      // Create a panel to hold all of the form widgets. 
      VerticalPanel panel = new VerticalPanel(); 
      uploadForm.setWidget(panel); 

      // Create a FileUpload widget. 
      FileUpload upload = new FileUpload(); 
      upload.setName("uploadFormElement"); 
      panel.add(upload); 

      // Add a 'submit' button. 
      Button uploadSubmitButton = new Button("Submit"); 
      panel.add(uploadSubmitButton); 
      uploadSubmitButton.addClickListener(new ClickListener() { 
       public void onClick(Widget sender) { 
       uploadForm.submit(); 
       } 
      }); 
      uploadForm.addFormHandler(new FormHandler() { 
       public void onSubmit(FormSubmitEvent event) { 
       } 
       public void onSubmitComplete(FormSubmitCompleteEvent event) { 
       Window.alert(event.getResults()); 
       } 
      }); 
      RootPanel.get().add(uploadForm); 
    } 

} 

서버 측

package uploadfile.server; 


public class UploadFileServlet extends HttpServlet implements Servlet 
{ 
    private static final long serialVersionUID = 8305367618713715640L; 

    protected void doGet(HttpServletRequest request, 
     HttpServletResponse response) 
         throws ServletException, IOException { 
    doPost(request, response); 
} 

protected void doPost(HttpServletRequest request, 
     HttpServletResponse response) 
         throws ServletException, IOException { 

    response.setContentType("text/plain"); 
    FileItem uploadItem = getFileItem(request); 
    if (uploadItem == null) { 
      response.getWriter().write("NO-SCRIPT-DATA"); 
      return; 
    } 
    byte[] fileContents = uploadItem.get(); 
    //TODO: add code to process file contents here. We will just print 

    System.out.println(new String(fileContents)); 
    response.getWriter().write(new String(fileContents)); 
    } 

    private FileItem getFileItem(HttpServletRequest request) { 

    FileItemFactory factory = new DiskFileItemFactory(); 
    ServletFileUpload upload = new ServletFileUpload(factory); 
    try { 
     List items = upload.parseRequest(request); 
     Iterator it = items.iterator(); 
     while (it.hasNext()) { 
      FileItem item = (FileItem) it.next(); 
      if (!item.isFormField() 
        && "uploadFormElement".equals(item.getFieldName())) { 
       return item; 
      } 
     } 
    } catch (FileUploadException e) { 
     return null; 
    } 
return null; 
    } 
} 

web.xml을

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
<servlet> 
<servlet-name>fileUploaderServlet</servlet-name> 
<servlet-class>uploadfile.server.UploadFileServlet</servlet-class> 
</servlet> 
<!-- Servlets 
Default page to serve --> 

<servlet-mapping> 
<servlet-name>fileUploaderServlet</servlet-name> 
<url-pattern>/uploadFileServlet</url-pattern> 
</servlet-mapping> 
<welcome-file-list> 
<welcome-file>Uploadfile.html</welcome-file> 
</welcome-file-list> 
</web-app> 
+0

"일부 오류가 발생했습니다." 무슨 실수 야? –

+0

문제를 지정할 수 있습니까? 어떤 예외? 무엇이 효과가 있고 그렇지 않은 것입니까?/UploadFile로/uploadFileServlet – Timii

+0

은 찾을); ' – NewCodeLearner

답변

1

가이 다를 수 있으므로, 나는 서블릿 액션 URL이 생각 GWT의 개발자가 아닌 건가요 잘못된. 경로가 /uploadfile/uploadFileServlet 인 서블릿을 시도하고 있지만 서블릿은 실제로 URL /uploadFileServlet에 매핑됩니다.

특별한 이유가없는 한, 을 추가로 구현해야 할 필요가 없습니다. HttpServlet을 확장하면됩니다.

+0

고마워, 나는 setAction 메소드를 수정했다. 지금은 무엇입니까 다른 오류 java.lang.NoClassDefFoundError가 : 조직/아파치/공유지/IO/출력/org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem에서 DeferredFileOutputStream \t (DiskFileItemFactory.java:199) \t (ServletFileUpload.java:126) – NewCodeLearner

+1

apache-commons io jar 파일을 추가하십시오. 귀하의 서버 클래스 패스. –

관련 문제