2010-05-29 55 views
0

我想開發從服務器上傳和下載文件。JSP上傳文件Java.lang.NullPointer

Upload.html

<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 2010-05-29 13:32:15

+0

雅對不起,我不明白你的代碼。 – user236501 2010-05-30 04:01:21

回答

2

我使用Apache commons fileupload來執行此任務。

+0

我試了Apache公用文件上傳也得到了這個java.lang.ClassNotFoundException:org.apache.commons.io.output.DeferredFileOutputStream 錯誤。 – user236501 2010-05-29 10:37:51

+0

org.apache.commons.io.output.DeferredFileOutputStream包含在http://commons.apache.org/io/commons-io中,您還需要將其包含在您的類路徑中。 (WEB-INF/lib) – stacker 2010-05-29 10:57:09

+0

嗨,感謝它的工作。 – user236501 2010-05-29 11:56:42