2017-08-16 81 views
0

控制器:多個下載Spring MVC中,在此只下載第一個文件,並跳過其他文件

@RequestMapping(value = { "/download-answer1-{userId}" }, method = RequestMethod.POST) 

public String downloadAnswers1(@PathVariable int userId, HttpServletRequest request, HttpServletResponse response) throws IOException { 
    try { 
     String []docId1 = request.getParameterValues("doc-id"); 

     for(String docId : docId1) { 
      UserDocument document = userDocumentService.findById(Integer.valueOf(docId)); 
      response.setContentType(document.getType()); 
      response.setContentLength(document.getContent().length); 
      response.setHeader("Content-Disposition","attachment; filename=\"" + document.getName() +"\""); 

      FileCopyUtils.copy(document.getContent(), response.getOutputStream()); 
      System.out.println("call me"); 
      response.flushBuffer();*/ 


     } 
    }catch(Exception e) {} 
    return "redirect:/download-answer-"+userId; 
} 

景(downloadAnswer.jsp):

<form action="${pageContext.request.contextPath}/download-answer1-${userId}" method="post"> 
     <div class="panel-heading"><span class="lead">List of Documents </span><%-- <a href="<c:url value='/download-answer1-${user.id}-${doc.id}' />" class="btn btn-success custom-width" id="all"> --%><button onclick="return confirm('Are you sure')">download</button><!-- </a> --></div> 

      <table class="table table-hover"> 
       <thead> 
        <tr> 
         <th scope="col" class="slctDlt" ><input type="checkbox" id="selectAll" ></th> 
         <th>No.</th> 
         <th>File Name</th> 
         <th>Type</th> 
         <th>Description</th> 
         <th width="100"></th> 
         <th width="100"></th> 
        </tr> 
       </thead> 
       <tbody> 
       <c:forEach items="${documents}" var="doc" varStatus="counter"> 
        <tr id=''> 
         <td><input type="checkbox" class="dltCheck" name="doc-id" value="${doc.id}"></td> 
         <td>${counter.index + 1}</td> 
         <td>${doc.name}</td> 
         <td>${doc.type}</td> 
         <td>${doc.description}</td> 
         <td><a href="<c:url value='/download-answer1-${user.id}-${doc.id}' />" class="btn btn-success custom-width" >download</a></td> 
        </tr> 
       </c:forEach> 
       </tbody> 
      </table> 
      </form> 

我已經做了很多的事情但所有的時間,它是在一次下載一個文件,即第一個文件。

回答

0

HTTP協議不支持多個文件的響應。這是不可能的。如果您需要迭代,視圖將不得不多次請求,並且您的控制器servlet可能必須處於有狀態才能保持迭代索引。