我正在开发一个具有上传PDF文件功能的网站。但我有一个错误。
这是我到目前为止所做的事情:
Multipart resolver:
<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服务器中的文件夹(如果这是一个好习惯)?
答案 0 :(得分: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
期望带有密钥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
不匹配。