0
您好想嘗試上傳文件,顯示文件,下載文件和使用spring mvc刪除文件等操作,我成功地上傳了文件並刪除了文件所有操作都工作正常,但隨後發生了什麼是當我上傳上傳的文件或圖像顯示或下載兩次,並獲得 java.lang.IllegalStateException:getOutputStream方法()已經被調用,這種響應如何使用彈簧mvc上傳,顯示,下載和刪除文件
<form method="post" action="doUpload" enctype="multipart/form-data">
<table border="0">
<tr>
<td>Pick file #1:</td>
<td><input type="file" name="fileUpload" size="50" /></td>
</tr>
<tr>
<td>Pick file #2:</td>
<td><input type="file" name="fileUpload" size="50" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Upload" /></td>
</tr>
</table>
</form>
<table border="1" bgcolor="black" width="600px">
<tr style="background-color: teal; color: white; text-align: center;"
height="40px">
<td>File Name</td>
<td>Image</td>
<td>Download</td>
<td>Delete</td>
</tr>
<c:forEach items="${employeeList}" var="user">
<tr style="background-color: white; color: black; text-align: center;"
height="30px">
<td><c:out value="${user.fileName}" /></td>
<td><img src="show?id=${user.id}" /></td>
<td><a href="edit?id=${user.id}">Download</a></td>
<td><a href="delete?id=${user.id}">Delete</a></td>
</tr>
</c:forEach>
</table>
@Controller
@RequestMapping("/")
public class RegistrationController {
@Autowired
private IRegistrationService registerService;
@RequestMapping(value = "/saveParentAndStudentFromAdmin", method = RequestMethod.POST)
public ModelAndView saveParentAndStudentByAdmin(
@ModelAttribute Student student,
@RequestParam CommonsMultipartFile[] fileUpload) {
if (fileUpload != null && fileUpload.length > 0) {
for (CommonsMultipartFile aFile : fileUpload) {
System.out.println("Saving file: "
+ aFile.getOriginalFilename());
student.setFileName(aFile.getOriginalFilename());
student.setFileType(aFile.getContentType());
student.setData(aFile.getBytes());
registerService.saveParentAndStudentByAdmin(student);
}
}
java.util.List<Student> uploadedFiles = registerService.findAllFiles();
return new ModelAndView("StudentEnrollmentFromAdmin", "employeeList",
uploadedFiles);
}
@RequestMapping("delete")
public ModelAndView deleteUser(@RequestParam int id) {
registerService.deleteRow(id);
java.util.List<Student> uploadedFiles = registerService.findAllFiles();
return new ModelAndView("StudentEnrollmentFromAdmin", "employeeList",
uploadedFiles);
}
@RequestMapping("show")
public ModelAndView displayImage(@RequestParam int id,
HttpServletResponse response, HttpServletRequest request) {
System.out.println("Id to display image: " + id);
Student item = registerService.get(id);
response.setContentType("image/jpeg, image/jpg, image/png, image/gif");
try {
response.getOutputStream().write(item.getData());
} catch (IOException e) {
e.printStackTrace();
}
try {
response.getOutputStream().close();
} catch (IOException e) {
e.printStackTrace();
}
return new ModelAndView("StudentEnrollmentFromAdmin");
}
@RequestMapping("downalod")
public ModelAndView downloadFile(@RequestParam int id,
HttpServletResponse response, HttpServletRequest request) {
System.out.println("Id to download: " + id);
Student student = registerService.get(id);
response.setContentType(student.getFileType());
response.setContentLength(student.getData().length);
response.setHeader("Content-Disposition", "attachment; filename=\""
+ student.getFileName() + "\"");
try {
FileCopyUtils.copy(student.getData(), response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
java.util.List<Student> uploadedFiles = registerService.findAllFiles();
return new ModelAndView("StudentEnrollmentFromAdmin", "employeeList",
uploadedFiles);
}
}
看到我已經更新了我的問題後,嘗試了一些更多的東西 –
如果使用不能返回一個新的ModelAndView響應輸出流;你返回null你使用我發佈的代碼 –