在spring mvc中多次下载,在此只下载第一个文件并跳过其他文件

时间:2017-08-16 09:05:45

标签: 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>

我已经做了很多事情,但是一直下载一个文件,即第一个文件。

1 个答案:

答案 0 :(得分:0)

HTTP协议不支持使用多个文件进行响应。无法执行此操作。如果需要迭代,视图将不得不多次请求,并且您的控制器servlet可能必须是有状态的,以保持迭代索引。