0

PDFファイルのアップロード機能を備えたWebを開発しています。しかし、私はエラーが発生しています。

これまでに行ったことは次のとおりです。

マルチパート リゾルバー:

<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="10000000"/>
</bean>

アップロードするフォーム:

<form:form commandName="fileUpload" action="../admin/uploadPDF.do" method="post"  enctype="multipart/form-data">
     <form:label path="fileData">Upload a File</form:label> <br />
     <form:input type="file"  path="fileData" />
     <input type="submit" value="upload" >
</form:form>

ユーザーが最初にアップロードページに来たときにリクエストをキャッチするコントローラー、AdminController.java

@RequestMapping( value = "/admin/module", method = RequestMethod.GET )
    public String student( @RequestParam( defaultValue = "" )
    String message, @RequestParam( defaultValue = "" )
    String messageType, HttpServletRequest request, ModelMap model )
    {
        model.addAttribute( "message", message );
        model.addAttribute( "messageType", messageType );
        model.addAttribute( new UploadItemBean() );
        HttpSession session = request.getSession();
        String returnVal = Credentials.checkSession( session );

        if( returnVal != null )
        {
            return returnVal;
        }

        return "als-student/module";
    }

アップロードファイルが送信されたときにリクエストをキャッチするコントローラー、UploadController.java

@RequestMapping( value = "*/uploadPDF", method = RequestMethod.POST )
public String getPDF( @RequestParam( defaultValue = "" )
String message, @RequestParam( defaultValue = "" )
String messageType, @RequestParam( "name" )
String name, @RequestParam( "file" )
MultipartFile file, HttpServletRequest request, ModelMap model )
{
    ...
  if( !file.isEmpty() )
    {
        try
        {
            byte[] bytes = file.getBytes();
            System.out.println( bytes + ", " + name );
        }
        catch( IOException e )
        {

            e.printStackTrace();
        }
    }
    return "als-student/module";
} 

スタックトレース:

Neither BindingResult nor plain target object for bean name 'fileUpload' available as request attribute
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
    at org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:130)
    at org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:120)
    at org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:90)
...
...

また、Bean をフォームに送信する方法も知りたいですfileUpload。これは、エラーの原因となっているようです。また、ファイルをアップロードした後、Apache サーバーのフォルダーに保存するにはどうすればよいですか (これが適切な方法である場合)。

4

1 に答える 1

1

このスニペット

<form:form commandName="fileUpload" action="../admin/uploadPDF.do" method="post"  enctype="multipart/form-data">
     <form:label path="fileData">Upload a File</form:label> <br />
     <form:input type="file"  path="fileData" />
     <input type="submit" value="upload" >
</form:form>

commandNameは、キーを持つモデル (リクエスト) 属性を期待しているためですfileUploadModelハンドラーにそのような属性を入れていないようです

@RequestMapping( value = "/admin/module", method = RequestMethod.GET )
public String student( @RequestParam( defaultValue = "" )
String message, @RequestParam( defaultValue = "" )
String messageType, HttpServletRequest request, ModelMap model )
{
    model.addAttribute( "message", message );
    model.addAttribute( "messageType", messageType );
    model.addAttribute( new UploadItemBean() );
    HttpSession session = request.getSession();
    String returnVal = Credentials.checkSession( session );

    if( returnVal != null )
    {
        return returnVal;
    }

    return "als-student/module";
}

が必要だと仮定しますUploadItemBean。これを行うには、コードを変更するだけです

model.addAttribute("fileUpload", new UploadItemBean() );

デフォルトでは、属性のキーを指定しない場合、Spring はオブジェクトのクラス名に基づいてキーを生成しますが、これは期待されるfileUpload.

于 2013-10-26T04:30:42.427 に答える