JSP文件上传但大小为零

时间:2017-08-25 07:03:05

标签: jsp spring-mvc spring-boot file-upload nhibernate

这是我的JSP页面

<%@include file="../../util/header.jsp" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <c:set var="path" value="${pageContext.request.contextPath}"/>
    <div class=" col-md-10 col-md-offset-4">
        <form class="ui tiny form" id="myform" style="width: 50%;padding-top: 
 50px;" action='${pageContext.request.contextPath}/student/action/save'
              method="POST" enctype="multipart/form-data" accept-
 charset="UTF-8">
            <input type="text" name="first_name">
            <div class="ui divider"></div>
            <input type="text" name="last_name">
            <div class="ui divider"></div>
            <div class="required field">
                <label>Student Image</label>
                <input type="file" name="img_student"/>
            </div>
            <div class="ui divider"></div>
            <div class="required field">
                <label>Gender</label>
                <select name="gender" class="ui fluid search dropdown 
  gender">
                    <option selected></option>
                    <option value="M">Male</option>
                    <option value="F">Female</option>
                    <option value="OTHER">Other</option>
                </select>
            </div>
            <button class="tiny ui green button" type="submit">Save</button>
        </form>
    </div>
    <script type="text/javascript">
        $('.gender').dropdown();
    </script>
    </body>
    </html>

这是Spring Controller

  @RequestMapping(value = REQUEST_ACTION_SAVE_STUDENT_URL, method = 
  RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
        public void saveStudent(HttpServletResponse response, 
  HttpServletRequest request) throws IOException {
            Student student = new Student();
            if (ServletFileUpload.isMultipartContent(request)) {
                try {
                    List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
                    for (FileItem item : items) {
                        if (!item.isFormField()) {
                            if (item.getSize() > 0) {
                                if (item != null) {
                                    if 
 (item.getFieldName().equals("img_student") == true) {

  student.setURI(fileUploadService.uploadFile(item));
                                    } else {

                                    }
                                }
                            }
                        } else {
                            getFormValue(item, student);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            response.sendRedirect(REQUEST_VIEW_STUDENT_URL);
        }

在jsp表单中我输入type =“file”但是当我点击提交然后       它将调用函数saveStudent。但是当走到行

List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);

总是返回items.size = 0.那么为什么items.size总是返回0? 事件我检查所有值或文件我可以从jsp表单获取所有 但是当它遇到parseRequest(request)时它总是返回0。        谢谢!

0 个答案:

没有答案