2010-05-29 2 views
0

서버에서 파일 업로드 및 다운로드를 원합니다. 내가 파일을 제출하면하고 Upload.htmlJSP 업로드 파일 Java.lang.NullPointer

<form action="/UploadFile/UploadFile" method="POST" 
enctype="multipart/form-data">Select a file: <input 
type="submit" name="button" /> <input type="file" name="first"></form> 

UploadFile.servlet

protected void doPost(HttpServletRequest request, 
     HttpServletResponse response) throws ServletException, IOException { 
    String temp = request.getParameter("first"); 
    System.out.println(temp); 
    File origFile = new File(temp); 
    FileOutputStream out = new FileOutputStream(request.getContextPath() 
      + "pdtImages/" + "FirstFile"); 
    InputStream ins = new FileInputStream(origFile); 
    try { 
     System.out.println(request.getContextPath()); 
     byte[] buf = new byte[1024]; 
     int len; 
     while ((len = ins.read(buf)) > 0) { 
      out.write(buf, 0, len); 
     } 
     out.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

나는 널 포인터 오류 메시지를 받았습니다. jsp에 익숙하지 않은 사람이 누구든지 나를 도울 수 있습니까? 파일을 서버 디렉토리에 저장하려고합니다.

+0

이전 질문에서 이미 대답했는데, 어떻게 그 정도면 충분하지 않습니까? http://stackoverflow.com/questions/2930240/jsp-uploading-and-downloading-video – BalusC

+0

죄송합니다. 귀하의 코드를 이해할 수 없습니다. – user236501

답변

2

나는이 작업에 Apache commons fileupload을 사용합니다.

+0

나는 Apache Commons FileUpload도 시험해 보았다. java.lang.ClassNotFoundException : org.apache.commons.io.output.DeferredFileOutputStream 오류가 발생했다. – user236501

+0

org.apache.commons.io.output.DeferredFileOutputStream은 http://commons.apache.org/io/ commons-io에 포함되어 있습니다. 클래스 패스에도 이것을 포함시켜야합니다. (WEB-INF/lib) – stacker

+0

안녕하세요 덕분에 작동합니다. – user236501