2016-08-29 12 views
1

私は春に新しくなりました。私はファイルをアップロードしてデータベースに書き込むWebアプリケーションを作るためにエクササイズをしています。私は、NetbeansのSpring MVCとMavenで作っています。Spring MVC - アップロードファイルステータス400 - 必要なMultipartFileパラメータ 'file'が存在しません。

私はこのチュートリアルに基づいて

https://saarapakarinen.wordpress.com/2015/01/11/building-a-basic-spring-3-web-project-netbeans-and-maven/

完全に動作する基本的なプロジェクトを作って、自分のアプリケーションのためにそれを拡張しようとしたが、公式の春のチュートリアル

http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-multipart

のベースのファイルアップロードコンポーネントを作りたかったです

が動作していないため、エラーが発生しました:

HTTPステータス400 - 必要なMultipartFileパラメータ「ファイル」

を提示していない私はwyslij.jspでこのプロジェクトを展開してきました(アップロードファイルの形式)

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Wysylanie pliku</title> 
    </head> 

<body> 
    <form method="get" action="http://localhost:8084/Hello/application/wyslij" enctype="multipart/form-data"> 
      <input type="file" name="file"/> 
      <input type="submit"/> 
     </form> 
</body> 


</html> 

そしてUpladController.java

と呼ばれるupladingファイル用のコントローラを追加しました
package helloweb; 

import java.io.IOException; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.multipart.MultipartFile; 

@Controller 
public class UploadController 
{ 

    @RequestMapping(value = "wyslij", method = RequestMethod.GET) 
    public String handleFormUpload(@RequestParam("file") MultipartFile file) throws IOException 
    { 

     if (!file.isEmpty()) { 
      byte[] bytes = file.getBytes(); 
      // store the bytes somewhere 
      return "redirect:tak"; 
     } else 
     { 
      return "redirect:nie"; 
     } 
    } 

} 

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"> 
    <!-- name of the project//--> 
    <display-name>HelloProject</display-name> 
    <servlet> 
     <servlet-name>front-controller</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>front-controller</servlet-name> 
     <url-pattern>/application/*</url-pattern> 
    </servlet-mapping> 

    <!-- max time of the session //--> 

    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <!-- default page //--> 
    <welcome-file-list> 
     <welcome-file>application/wyslij.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

とフロントコントローラ-SE rlvet.xmlファイル:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <!-- configuration to fetch jsp files automatically from WEB-INF/jsp --> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

    <bean id="multipartResolver" 
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 

    <!-- one of the properties available; the maximum file size in bytes --> 
    <property name="maxUploadSize" value="100000"/> 

</bean> 



    <context:component-scan base-package="helloweb" /> 

</beans> 

なぜ誰かがエラーを説明し、説明できますか?

EDIT:(彼らは私のコードよりも多く働いていた)

form.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 

     <title>Form Page</title> 
    </head> 
    <body> 

      <form method="POST" enctype="multipart/form-data" action="http://localhost:8084/Hello/application/form"> 
       <label>file to send: <input type="file" name="file" /></label> 
       <input type="submit" /> 
      </form> 
    </body> 
</html> 

は私がform.jsp使用することを決めたとHelloController.javaはチュートリアルで作られ、アップロードをファイルに変換

とHelloController.java

package helloweb; 

import java.io.IOException; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.multipart.MultipartFile; 


@Controller 
public class HelloController 
{ 
    @RequestMapping(value = "form", method = RequestMethod.POST) 
    public String login(@RequestParam(value = "file", required = true) MultipartFile file) throws IOException 
    { 
        if (!file.isEmpty()) 
        { 
      byte[] bytes = file.getBytes(); 
      // store the bytes somewhere 
      return "redirect:tak"; 
     } 
        else 
     { 
      return "redirect:nie"; 
     } 

    } 

     @RequestMapping("form") 
    public String viewLoginPage(Model model) 
    { 
     return "form"; 
    } 
} 

今、私は、少なくともファイルを持っていますスタートページに正しく表示されますが、ファイルを選択した後、ボタンをクリックし、フォームをアップロードし、私はantoherエラーを取得する:

​​

答えて

0

あなたは、HTTP POSTを必要とするマルチパート/フォームデータのエンコードを使用してファイルを送信しています。あなたのコントローラでそれを以下のように転記してください。

@RequestMapping(value = "wyslij", method = RequestMethod.POST) 

あなたのJSPでも同様です。

<form method="post" action="http://localhost:8084/Hello/application/wyslij" enctype="multipart/form-data"> 
     <input type="file" name="file"/> 
     <input type="submit"/> 
    </form> 
+0

私も変更しようとしていました。メソッドがPOSTに変わったときに別のエラーが発生する:HTTP Status 405 - リクエストメソッド 'GET'がサポートされていない。私はGETではなくPOSTメソッドを使用しているので不思議です。 –

+0

コントローラーとJSPの両方で「投稿」に変更しましたか? soapuiまたはPostmanを使用してサービスをテストしてください。 Posman POSTリクエスト用の – abaghel

+0

は、key = fileおよびvalue = multipart/form-dataのヘッダを追加します。 [本文]タブで[フォームデータとキー=ファイル]を選択し、[ファイル]ボタンをクリックしてファイルをアップロードします。 /こんにちは/アプリケーション/ wyslij タイプステータスレポート メッセージ/こんにちは/アプリケーション/ wyslij 説明要求されたリソースは利用できません - あなたが言ったと404はエラーHTTPステータスを得たとして、その後行われ、要求 – abaghel

0

Chnagedとコントローラは現在、次のとおりです。

@Controller 
public class UploadController 
{ 

    @RequestMapping(value = "wyslij", method = RequestMethod.POST) 
    public String handleFormUpload(@RequestParam("file") MultipartFile file) throws IOException 
    { 

     if (!file.isEmpty()) { 
      byte[] bytes = file.getBytes(); 
      // store the bytes somewhere 
      return "redirect:tak"; 
     } else 
     { 
      return "redirect:nie"; 
     } 
    } 

} 

とのjsp:

HTTP Status 405 - Request method 'GET' not supported 

type Status report 

message Request method 'GET' not supported 

description The specified HTTP method is not allowed for the requested resource. 

結果について:GETテストのための郵便配達 結果に

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Wysylanie pliku</title> 
    </head> 

<body> 
    <form method="post" action="http://localhost:8084/Hello/application/wyslij" enctype="multipart/form-data"> 
      <input type="file" name="file"/> 
      <input type="submit"/> 
     </form> 
</body> 


</html> 

テストサービスPOSTテスト:

HTTP Status 400 - Required MultipartFile parameter 'file' is not present 

type Status report 

message Required MultipartFile parameter 'file' is not present 

description The request sent by the client was syntactically incorrect. 
関連する問題