JSP页面包含以下代码片段,其中包含用于上载文件的upload字段。
<form:form commandName="editMemberInfoModelObj" method="post" enctype="multipart/form-data">
<h1>Edit Member Information</h1>
<table>
//Other Form Input Fields ...
<tr>
<td>File</td>
<td><input type="file" name="file"/></td>
</tr>
<tr>
<td><input type="submit" value="Update Info"/></td>
</tr>
</table>
</form:form>
这个JSP的模型如下所示
public class EditMerchandiserModel(){
private MultipartFile file;
//getters and setters for all the properties
}
控制器中处理文件上载的代码如下所示
if(model.getFile().isEmpty()) -->THROWING NULLPOINTER EXCEPTION HERE
{
MultipartFile file = model.getFile();
String fileName = file.getOriginalFilename();
String filePath = "/usr/local/" + fileName;
FileOutputStream fos = new FileOutputStream(filePath);
try
{
fos.write(file.getBytes());
} catch (IllegalStateException e) {
System.out.println(e);
}
finally{
fos.close();
}
}
我无法命中内部代码,因为它在文件中以空值读取。
为什么不将值绑定到字段?