2017-10-31 8 views
0

spring mvcを使用してファイルをアップロードしようとすると、以下のエラーが表示されます。 "Etat HTTP 500 - 要求の処理に失敗しました。ネストされた例外はjava.lang.NullPointerExceptionです。" これは初めてです。 私は原因について考えていますが、解決策を見つけるまでには至りません。Spring MVC - ファイルアップロード - Etat HTTP 500エラーメッセージ

お手伝いできる方に感謝します。

FileModel.java

package com.model; 

import org.springframework.web.multipart.MultipartFile; 

public class FileModel { 

    private MultipartFile file; 

    public MultipartFile getFile() { 
     return file; 
    } 

    public void setFile(MultipartFile file) { 
     this.file = file; 
    } 
} 

FileUploadController.java

package com.controller; 

import java.io.File; 
import java.io.IOException; 

import javax.servlet.ServletContext; 

import org.jboss.logging.Logger; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.util.FileCopyUtils; 
import org.springframework.validation.BindingResult; 
import org.springframework.validation.annotation.Validated; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.multipart.MultipartFile; 
import org.springframework.web.servlet.ModelAndView; 

import com.model.FileModel; 

@Controller 
public class FileUploadController { 

    public Logger logger = Logger.getLogger(FileUploadController.class); 

    public FileUploadController(){ 
     System.out.println("FileUploadController"); 
    } 

    @Autowired 
    private ServletContext context; 

    @RequestMapping(value = "/fileUploadPage", method = RequestMethod.GET) 
    public ModelAndView fileUploadPage() { 
     FileModel file = new FileModel(); 
     ModelAndView modelAndView = new ModelAndView("fileUpload", "command", file); 
     return modelAndView; 
    } 

    @RequestMapping(value="/fileUploadPage", method = RequestMethod.POST) 
    public String fileUpload(@Validated FileModel file, BindingResult result, ModelMap model) throws IOException { 
     if (result.hasErrors()) { 
      System.out.println("validation errors"); 
      return "fileUploadPage"; 
     } else {    
      System.out.println("Fetching file"); 

      MultipartFile multipartFile = file.getFile(); 

      System.out.println("Multipart "+multipartFile); 

      String uploadPath = context.getRealPath("") + File.separator + "fichiers" + File.separator; 
      System.out.println(uploadPath);   

      String fileName = multipartFile.getOriginalFilename(); 
      System.out.println(fileName); 

      FileCopyUtils.copy(file.getFile().getBytes(), new File(uploadPath+file.getFile().getOriginalFilename()));   

      model.addAttribute("fileName", fileName); 
      return "success"; 
     } 
    } 

} 

FileUpload.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" isELIgnored="false"%> 
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 

<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
     <title>Upload</title> 
    </head> 
    <body> 
     <form:form action="fileUploadPage" method = "POST" modelAttribute = "fileUpload" enctype = "multipart/form-data"> 
      Please select a file to upload : <input type = "file" name = "file" /> 
      <input type = "submit" value = "upload" /> 
     </form:form> 
    </body> 
</html> 

コンソール

GRAVE: Servlet.service() for servlet [spring] in context with path [/oracle_spring_user_test_project] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause 
java.lang.NullPointerException 
    at com.controller.FileUploadController.fileUpload(FileUploadController.java:56) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) 
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) 
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706) 
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) 
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:650) 
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Thread.java:745) 
+1

fileUploadControllerの56行目は何ですか? – marco

+0

文字列fileName = file.getOriginalFilename(); – FooBayo

答えて

0

あなたのJSPフォームでmodelAttribute = "fileUpload"を使用しています。したがって、モデルから取得する必要がある引数を示すためには、メソッド引数として@ModelAttribute spring注釈を使用する必要があります。これはベストプラクティスですが、これは必要ではありません。

fileUploadモデル属性には、fileUploadPageエンドポイントに送信されたフォームのデータが入力されている必要があります。あなたはこの注釈に関する公式情報を読み取ることができるSpring documentation

@RequestMapping(value="/fileUploadPage", method = RequestMethod.POST) 
    public String fileUpload(@ModelAttribute("fileUpload") FileModel fileUpload, BindingResult result, ModelMap model) throws IOException { 
     if (result.hasErrors()) { 
      System.out.println("validation errors"); 
      return "fileUploadPage"; 
     } else {    
      System.out.println("Fetching file"); 

      MultipartFile file = fileUpload.getFile(); 
     ... 

:だから、コードの以下のスニペットを試してみてください。

+0

ご協力いただきありがとうございます。私は試みましたが、それはまだ動作していません。同じエラーメッセージが表示されます。 – FooBayo

関連する問題