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