2017-03-01 85 views
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); 
    } 
} 

回答

0

如果我理解正確你的問題,你可以做這樣的事情:

public ResponseEntity<InputStreamResource> getFile(@PathVariable("idForm") String idForm) 
{ 
    try 
    { 
     Student item = registerService.get(id); 
     HttpHeaders respHeaders = new HttpHeaders(); 
     //Change it with your real content type 
     MediaType mediaType = new MediaType("img","jpg"); 
     respHeaders.setContentType(mediaType); 
     respHeaders.setContentLength(file.length()); 
     //I suppose you have a method "getFileName" 
       //By using attachment you download the file; by using inline you should see the image in the browser 
     respHeaders.setContentDispositionFormData("attachment", item.getFileName()); 
     InputStreamResource isr = new InputStreamResource(new ByteArrayOutputStream(item.getData())); 
     return new ResponseEntity<InputStreamResource>(isr, respHeaders, HttpStatus.OK); 
    } 
    catch (Exception e) 
    { 
     String message = "Error; "+e.getMessage(); 
     logger.error(message, e); 
     return new ResponseEntity<InputStreamResource>(HttpStatus.INTERNAL_SERVER_ERROR); 
    } 
} 

安傑洛

+0

看到我已經更新了我的問題後,嘗試了一些更多的東西 –

+0

如果使用不能返回一個新的ModelAndView響應輸出流;你返回null你使用我發佈的代碼 –