2016-10-13 3 views
0

인터넷에서이 오류를 찾으려고했지만 문제점을 해결할 수 없습니다. 나는 내 프로젝트의 로컬 폴더에 이미지를 저장하기 위해 노력하고있어 나는 그렇게 다음 자습서를 사용 : 나는 코드를 컴파일 할 때이 오류를 수신하지,하지만 난 NullPointerException이받은 서블릿의 POST 메소드가 JSP에서 값을 가져 오지 않았습니다.

http://docs.oracle.com/javaee/6/tutorial/doc/glraq.html

. 이것은 내 JSP입니다 :

<form action="uploadImagem.do" method="post" enctype="multipart/form-data" 
     name="productForm" id="productForm"><br><br> 
<form method="POST" action="upload" enctype="multipart/form-data" > 
     File: 
     <input type="file" name="file" id="file" /> <br/> 
     Destination: 
     <input type="text" value="/tcc_SI_M_12 - 12-10-2016_v1/images" name="destination" disabled/> 
     </br> 
     <input type="submit" value="Upload" name="upload" id="upload" /> 
    </form> 

그리고 이것은 내 서블릿입니다 :

@WebServlet("/uploadImagem.do") 
public class uploadImagem extends HttpServlet { 
private static final long serialVersionUID = 1L; 
private final static Logger LOGGER = 
     Logger.getLogger(FileUpload.class.getCanonicalName()); 


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // Create path components to save the file 
    final String path = request.getParameter("destination"); 
    System.out.println("Parametros: " + path); 
    final Part filePart = request.getPart("file"); 
    final String fileName = getFileName(filePart); 


    OutputStream out = null; 
    InputStream filecontent = null; 
    final PrintWriter writer = response.getWriter(); 

    try { 
     out = new FileOutputStream(new File(path + File.separator 
       + fileName)); 
     filecontent = filePart.getInputStream(); 

     int read = 0; 
     final byte[] bytes = new byte[1024]; 

     while ((read = filecontent.read(bytes)) != -1) { 
      out.write(bytes, 0, read); 
     } 
     writer.println("New file " + fileName + " created at " + path); 
     LOGGER.log(Level.INFO, "File{0}being uploaded to {1}", 
       new Object[]{fileName, path}); 
    } catch (FileNotFoundException fne) { 
     writer.println("You either did not specify a file to upload or are " 
       + "trying to upload a file to a protected or nonexistent " 
       + "location."); 
     writer.println("<br/> ERROR: " + fne.getMessage()); 

     LOGGER.log(Level.SEVERE, "Problems during file upload. Error: {0}", 
       new Object[]{fne.getMessage()}); 
    } finally { 
     if (out != null) { 
      out.close(); 
     } 
     if (filecontent != null) { 
      filecontent.close(); 
     } 
     if (writer != null) { 
      writer.close(); 
     } 
    } 
} 

private String getFileName(final Part part) { 
    final String partHeader = part.getHeader("content-disposition"); 
    LOGGER.log(Level.INFO, "Part Header = {0}", partHeader); 
    for (String content : part.getHeader("content-disposition").split(";")) { 
     if (content.trim().startsWith("filename")) { 
      return content.substring(
        content.indexOf('=') + 1).trim().replace("\"", ""); 
     } 
    } 
    return null; 

} 

하십시오, 나는 위의 튜토리얼의 일부 변경 한 마음. 또한, 나는 System.out을 포함하여 변수에 valor를 표시하고 null을 받았다.

도와주세요.

감사합니다.

+0

왜 서블릿에 @MultipartConfig를 포함시키지 않았습니까? – rickz

답변

0

왜 두 가지 양식을 사용하고 있습니까? 아래 코드를 제거하십시오 :

<form method="POST" action="upload" enctype="multipart/form-data" > 
관련 문제