如何将带有文件的字符串从ajax发送到servlet?

时间:2015-10-24 04:36:59

标签: jquery ajax java-ee servlets jquery-file-upload

在此我发送带有字符串的表单数据。如果我发送没有密钥的数据然后它提交但我尝试用formData发送密钥我无法在服务端获得它。 客户端代码:

var fileData = new FormData();
     fileData.append('file', document.getElementById("file_section").files[0]);
     fileData.append('firstName', firstName);
     fileData.append('familyName', familyName);
     fileData.append('preferName', preferName);
     fileData.append('dob', dob);
     $.ajax({
            url: 'FileUpload',
            type: 'POST',
            data: { 
                fileData : fileData
                key : "fileUpload",
            },
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            dataType: "json",
            success: function (response) {
                alert("In success");

                },
            error: function() {
                console.log("Error in file upload");
            }
          });

服务器端的代码:

boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);

        if(isMultipartContent) {


            File file = null;

            FileItemFactory factory = new DiskFileItemFactory();


            String timestamp = new Date().toString().replace(' ', '_');
            String name = "";

            ServletFileUpload upload = new ServletFileUpload(factory);
            ServletContext ctx=getServletContext();
            try {
                // Parse the request 
                List<FileItem> multiparts = upload.parseRequest(request);

                for (FileItem item : multiparts) {

                    //Get File
                    if (!item.isFormField()) {
                        name = new File(item.getName())+timestamp;
                        file = new File(ctx.getAttribute("dataUpload").toString() + name+".xlsx");
                        item.write(file);
                    }

                    //Get Form data
                    String fieldname = item.getFieldName();
                    String fieldvalue = item.getString();
                    if (fieldname.equals("firstName")) {
                           firstName = fieldvalue;
                           columnCount = columnCount + colCount(firstName);
                    } else if (fieldname.equals("familyName")) {
                           familyName = fieldvalue;
                           columnCount = columnCount + colCount(familyName);
                    } else if (fieldname.equals("preferName")) {
                           preferName = fieldvalue;
                           columnCount = columnCount + colCount(preferName);
                    } else if (fieldname.equals("dob")) {
                           dob = fieldvalue;
                           columnCount = columnCount + colCount(dob);
                    }
                }

            } catch (Exception e) {
                System.out.println("File upload failed " + e.getMessage());
                e.printStackTrace();
            } 
        }

0 个答案:

没有答案