如何使用 Spring MVC3 将图像保存在数据库中
控制器:
public String postAdd(@ModelAttribute("employeeAttribute") @Valid Employee employee, BindingResult result,@RequestParam("file") MultipartFile file) throws Exception{
byte[] bFile=null;
System.out.println("File Name......"+file.getName());
if (!file.isEmpty()) {
bFile = new byte[(int) file.getSize()];
FileInputStream fileInputStream = new FileInputStream(file.getOriginalFilename());
fileInputStream.read(bFile);
fileInputStream.close();
}
employee.setImage(bFile);
employeeServiceImpl.add(employee);
}
Jsp页面:
<c:url var="saveEmp" value="/manam/mobee/employee/add"/>
<form:form modelAttribute="employeeAttribute" method="POST" action="${saveEmp}" enctype="multipart/form-data" >
<form:label path="image">Image</form:label>
<input type="file" name="file" id="file"></input>
我在这里发送 C:\Users\Public\Pictures\Sample Pictures\Desert Landscape.jpg 文件,但 FileInputStream 只需要 Landscape.jpg 。请建议如何为 FileInputStream 设置完整文件路径。
在这里我得到 java.io.FileNotFoundException: Forest.jpg (The system cannot find the file specified) 异常。