如何使用ajax在jsp中上传文件和其他一些文本字段

时间:2018-04-19 05:44:03

标签: jsp file-upload ajaxform

我试图上传一个文件和jsp中的其他一些字段文件上传,但是request.getParameter(" fieldName")无法正常工作

我的HTML表单

<form action="SurveyServlet" id="upload-form" method="post" enctype="multipart/form-data">
  Upload Image
  <input type="file" id="merge_notification" name="merge_notification"  required="required"/>
  <small>Please upload merge notification (Allowed file types are png,jpg)</small>
  Notification Date
  <input type="date" id="merge_notification_date" name="merge_notification_date" required="required"/>
  Remarks
  <textarea id="notification_remarks" name="notification_remarks"></textarea>
  <input type="hidden" id="merging_district" name="merging_district" value="sometest">
  <input type="submit" name="submit_btn" value="Merge School" class="admin-button merge-school-btn">

我的Ajaxform代码

<script type="text/javascript">
    $(function () {
         $('#upload-form').ajaxForm({
            success: function(msg) {
            alert(msg);
        },
        error: function(msg) {
            $("#upload-error").text("Couldn't upload file");
        }
    });
    });

文件上传的Servlet代码

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
    /*Upload form*/
    if (ServletFileUpload.isMultipartContent(request)) {
        String UPLOAD_DIRECTORY = "c:/mergeSchoolNotifications";

        //Creates the directory if it does not exist in the specified location
        String dest_path = "/" + request.getParameter("merging_district");//create another directory inside
        String savePath = UPLOAD_DIRECTORY + dest_path;
        File uploadDir = new File(UPLOAD_DIRECTORY);
        if (!uploadDir.exists()) {
            uploadDir.mkdir();
        }
        File upload_sub_dir = new File(UPLOAD_DIRECTORY + dest_path);
        if (!upload_sub_dir.exists()) {
            upload_sub_dir.mkdir();
        }
        try {
            List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
            for (FileItem item : multiparts) {
                if (!item.isFormField()) {
                    String format = "none";
                    String name = new File(item.getName()).getName();
                    int index = name.lastIndexOf(".");
                    if (index > 0) {
                        format = name.substring(index + 1);
                        format = format.toLowerCase();
                    }
                    item.write(new File(savePath + File.separator + "MyfileName." + format));
                }
            }
            request.setAttribute("message", "uploaded");
        } catch (Exception e) {
            request.setAttribute("message", "File(s) upload failed due to " + e.getMessage() + "!");
        }
    }
    /*Upload form ends here*/
}

上传文件的路径如下:C:\ mergeSchoolNotifications \ null \ MyfileName.jpg

我的问题是如何动态创建子目录,然后保存上传文件的路径以及sql db中的其他一些字段

感谢。

0 个答案:

没有答案