0

I'm pretty new to Java servlets and I'm working on one used to bring uploaded files into a third part program. Starting from an HTML page I'm able to perform all tasks correctly, file upload with folder structure, a progress bar related to file upload to server and ingestion to third part program and i do it in doPost method. What I need is to retrieve an int or a String value to push to HTML in order to show the user the elaboration progress after file upload. Unfortunately simply write to response isn't useful because the values are sent to HTML only at process end. I'll try to be clear here with an example and the variable i'd like to show while running is whatToShow, hoping to see it as a thread safe value:

public void vai (HttpServletRequest request, HttpServletResponse response) {
  Map <String,String> subFolders = new HashMap <String,String>();
  Map <String,String> subFoldersP = new HashMap <String,String>(); 
  Map <String,Path> subFoldersPath = new HashMap <String,Path>();
  String cosa = "";
  String nome = "";
  String percorso = "";
  int corrente,massimo = 0;
  int whatToShow = 0 ;
  Path nom =  Paths.get(UPLOAD_DIRECTORY) ;

  System.out.println("QueryString : " + request.getQueryString() + "   ContentType : " + request.getContentType());

  response.setContentType ("text/event-stream;charset=UTF-8"); //("text/html");  
  response.setContentType("Cache-Control: no-cache");
  PrintWriter out = response.getWriter();

  //process only if its multipart content

  if(ServletFileUpload.isMultipartContent(request)){
    try {
      String Host = "third-party"; 
      String Username = "someuser"; 
      String Password = "somePWD"; 

      String handle = "";
      List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
      massimo = multiparts.size();

      corrente=0;
      for(FileItem item : multiparts){
        corrente ++;
        whatToShow = ((corrente*100)/massimo);

        if(!item.isFormField()){
          handle = insertIntoThirdPart(item,nom,subFolders,subFoldersP,subFoldersPath,"myDest");
        }
        else {
          nome = item.getString();
          System.out.println("Ricevuto dal client " + nome + " e le mappe hanno dimensione " + subFolders.size() + " " + subFoldersP.size() + " " +subFoldersPath.size() );
          nom = Paths.get(nome);
          percorso = nom.toString(); 
          percorso = percorso.substring(0, (percorso.length() - nom.getFileName().toString().length()));
          System.out.println("Passato alla routine  " + percorso);
          nom = Paths.get(percorso);
          System.out.println("come   " + nom.toString());
          cosa = cosa + item.getString()+"<BR>";
        }
      } 
      out.write("Files processing terminated...");
      out.flush();
      subFolders.clear();
      subFoldersP.clear();
      subFoldersPath.clear();
      corrente = 0;

      //File uploaded successfully

    }
    catch (Exception ex) {
      out.write("File Upload Failed due to " + ex); //mannaggia alla pupazza !!
    }    
  }
  else{
    out.write("Sorry, this function in restricted for internal purposes");  // eh no ciccio !
  }
}    

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  vai(request,response);
}
Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52
Gigi F.
  • 1
  • 1
  • your description is unclear, please rewrite you question and the process you wish to perform. – Jadeye Feb 05 '17 at 09:59
  • Hi Jadeye, thank you for your kind reply. I'll try to be clear . – Gigi F. Feb 05 '17 at 10:21
  • Hi Jadeye, thank you for your kind reply. I'll try to be clear . My goal is, even if it's possible, to fetch a response, containing the whatToShow value to the html page that was invoking the servlet, while its execution is still in progress. The for (FileItem item: multiparts) cycle is a time consuming task responsible for accepting the files uploaded from HTML and storing them into the third part application server and until it was not completed no response will be fetched. I'd like to show on the HTML a progress bar or simply a percent value to inform the user meanwhile. – Gigi F. Feb 05 '17 at 10:34
  • Welcome to Stack Overflow! Please review our [SO Question Checklist](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) to help you to ask a good question, and thus get a good answer. – Joe C Feb 05 '17 at 10:52
  • Thank you very much. Sorry if i'm not clear enough but also my english is a little bit rusty.. I don't know if what i'm asking is possible but a little example, even not based on my code, should be illuminating for me. What i need is to know if a servlet should return execution progression infos to a web page while executing , not only when completed. I'm completely opened to every solution. Thank you again. – Gigi F. Feb 05 '17 at 11:03
  • Thank you very much BalusC, i missed that in my searches. I owe you a favor ! – Gigi F. Feb 10 '17 at 08:11

0 Answers0