我正在开发一个具有上传 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 服务器中的文件夹(如果这是好的做法)?