2017-10-05 2 views
0

Ich weiß, dass diese Frage schon einmal gestellt wurde, aber keiner von ihnen hat mir geholfen. Ich habe jede Lösung im Zusammenhang mit dieser Frage versucht, aber keine Lösung gefunden.Es funktioniert nicht !! Verarbeitung der mehrteiligen/Formulardatenanforderung fehlgeschlagen. Unerwarteter EOF am Sockel gelesen

Wenn jemand über diesen Fehler kennt, wird es sehr hilfreich sein

Meine Ajax-Code ist:

$('#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'); 
     } 
    }); 
}); 

Mein Servlet-Code ist, wo ich den Code für Fileupload mit Apache-Commons schrieb File- Upload-Bibliothek:

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(); 
    } 
} 

Bitte helfen seine immer komplexer

Antwort

0

Eigentlich, wie ich AJAX war, was passiert ist, wenn ich Servlet anfordern, um die Upload-Sache zu verarbeiten, war es nicht ganze Datei hochladen und dann gibt es Antwort zurück Sofort So muss das Servlet bis zum Hochladen der Datei warten endet.

Also habe ich einfach eine async: false In Ajax Anruf und dann hat es einfach funktioniert!

Verwandte Themen