我正在开发一个 Spring Web 应用程序,需要为我的一个页面实现一个简单的 FileUpload。
JSP 页面包含以下代码片段,其中包括用于上传文件的上传字段。
<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
}
Controller 中处理文件上传的代码如下所示
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();
}
}
我无法点击内部代码,因为它在文件中读取为空值。 为什么它不将值绑定(bind)到字段?
最佳答案
看起来你的文件输入框的名称为“file”,而它应该绑定(bind)的属性的名称为“photo”(至少你正在尝试使用“getPhoto()”检索它。Spring 很聪明,但它没那么聪明。:)
关于java - FileUpload 问题 - 多部分文件为空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3713831/