0

我正在开发一个具有上传 PDF 文件功能的网络。但我有一个错误。

这是我到目前为止所做的:

多部分解析器:

<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期望带有 key 的模型(请求)属性fileUpload。你似乎没有Model在你的处理程序中放置这样一个属性

@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 回答