简体   繁体   English

它不起作用! 处理多部分/表单数据请求失败。 套接字上意外的EOF读取

[英]Its not working!! Processing of multipart/form-data request failed. Unexpected EOF read on the socket

I know this question has been asked previously but none of them helped me. 我知道以前曾问过这个问题,但没有一个人帮助我。 I've tried every solution related to this question but didn't got the solution. 我已经尝试过与该问题相关的所有解决方案,但没有找到解决方案。

If anyone knows about this error It will be very helpful 如果有人知道此错误,这将非常有帮助

My ajax code is : 我的ajax代码是:

    $('#versionForm').submit(function (e) {

    formData = new FormData(this);
    $.each(formAttachements, function (i, file) {
        formData.append('file-' + i, file);
        console.log('data' + formData);
    });

    $.ajax({

        url: 'UploadServlet',
        data: formData,
        type: 'POST',
        cache: false,
        contentType: false,
        processData: false,
        success: function () {
            console.log('Files Uploaded');
            console.log('versionForm submit ajax success');
        },
        error: function () {

            console.log('versionForm submit ajax error');
        }
    });
});

My Servlet code is where I wrote the code for fileUpload using apache-commons file-upload library: 我的Servlet代码是我使用apache-commons file-upload库编写fileUpload的代码的地方:

public class UploadServlet extends HttpServlet {

String version = "";
String countryCode = "";

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    System.out.println("In Upload Servlet");

    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    System.out.println("Is form multipart:" + isMultipart);

    // check if form is multipart
    if (isMultipart) {
        // Create a factory for disk-based file items
        DiskFileItemFactory factory = new DiskFileItemFactory();

        // Configure a repository (to ensure a secure temp location is used)
        ServletContext context = this.getServletConfig().getServletContext();
        File repository = (File) context.getAttribute("javax.servlet.context.tempdir");
        factory.setRepository(repository);

        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload(factory);


        try {

            List<FileItem> items;

            // Parse the request
            items = upload.parseRequest(request);

            for(FileItem item : items) {
                // Process a regular form field
                if (item.isFormField()) {
                    System.out.println("Regular form field!");
                    processFormField(item);

                    // Process a file upload
                } else {
                    System.out.println("File Upload Field!");
                     InputStream fileContent = item.getInputStream();
                    processUploadFile(fileContent,item);
                }
            }




        } catch (FileUploadException ex) {
            Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

// for regular form field
public void processFormField(FileItem item) {
    String fieldName = item.getFieldName();
    String value = item.getString();

    if (fieldName.equals("version")) {
        version = value;
    }
    if (fieldName.equals("countryCode")) {
        countryCode = value;
    }
    System.out.println("fieldName " + fieldName);
    System.out.println("value " + value);
}

// for upload file
public void processUploadFile(InputStream fileContent,FileItem item) {

    // fields of uploaded file
    String fieldName = item.getFieldName();
    String fileName = item.getName();
    String contentType = item.getContentType();
    boolean isInMemory = item.isInMemory();
    long sizeInBytes = item.getSize();
    System.out.println("FieldName:" + fieldName + ",FileName:" + fileName + ",ContentType:" + contentType
            + ",IsInmemory:" + isInMemory + ",SizeInByte:" + sizeInBytes);


    String saveDirPath = "C:\\Users\\Gest1\\Desktop\\karan\\server\\" + countryCode + "\\" + version;
    File file = new File(saveDirPath);
    // if directory does not exists make directory
    if (!file.exists()) {
        file.mkdirs();
        System.out.println("Dir created!");
    }

    // Path for file
    String filePath = saveDirPath + File.separator + fileName;

    try {

        while (fileContent.available() > 0) {
            System.out.println("Inside While Loop");
            // write the file
            File storeFile = new File(filePath);
            item.write(storeFile);
            fileContent.close();

        }

    } catch (EOFException ex) {
        Logger.getLogger(UploadServlet.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Please help its getting complex 请帮助它变得复杂

Actually as I was using AJAX so what is happening is when I request servlet to process the upload thing it wasn't uploading whole file and then It is giving response back Immediately So the servlet needs to wait untill the file Upload finishes. 实际上,当我使用AJAX时,发生的是当我请求Servlet处理上传的内容时,它没有上传整个文件,然后立即返回响应,因此Servlet需要等待文件上传完成。

So I just put a async : false In ajax call and then It just worked! 因此,我只是放置了一个async:false在ajax调用中,然后它就起作用了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM