【问题标题】:Its not working!! Processing of multipart/form-data request failed. Unexpected EOF read on the socket它不工作!处理多部分/表单数据请求失败。在套接字上读取意外的 EOF
【发布时间】:2018-03-16 20:58:44
【问题描述】:

我知道以前有人问过这个问题,但他们都没有帮助我。 我已经尝试了与此问题相关的所有解决方案,但没有得到解决方案。

如果有人知道这个错误,那将非常有帮助

我的 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');
        }
    });
});

我的 Servlet 代码是我使用 apache-commons 文件上传库为 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();
    }
}

请帮助它变得复杂

【问题讨论】:

    标签: java file-upload apache-commons-fileupload


    【解决方案1】:

    实际上,当我使用 AJAX 时,发生的情况是当我请求 servlet 处理上传的事情时,它并没有上传整个文件,然后它立即给出响应,所以 servlet 需要等到文件上传完成。

    所以我只是在 ajax 调用中放了一个 async : false 然后它就起作用了!

    【讨论】:

      猜你喜欢
      • 2012-09-10
      • 2013-04-27
      • 1970-01-01
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      • 2014-09-21
      • 2014-06-15
      • 2018-10-17
      相关资源
      最近更新 更多