I am working on a Spring web application and need to implement a simple FileUpload for one of my pages.
The page for the JSP contains the following snippet of code which included an upload field for uploading the file.
<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>
The model for this JSP looks like the following
public class EditMerchandiserModel(){
private MultipartFile file;
//getters and setters for all the properties
}
The code in the controller that handles the file upload looks like the following
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();
}
}
I am unable to hit the inside code because it is reading in the file as a null value. Why is it not binding the value to the field?
看起来您的文件输入框的名称为“file”,而它应该绑定的属性名称为“photo”(至少您试图使用“getPhoto()”来检索它.Spring很聪明,但它不是那么聪明。:)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.