0

I have a little problem then upload a file from a servlet. The upload is succesful but i must manually refresh my image directory (in WebContent) for see the uploaded image on the website.

here my form for choose the file :

<form action="SRVLTInsererImage" method="POST" enctype="multipart/form-data">
        <div>
            <div>
                <label>Choisir une catégorie</label>
                <select name="id_categorie">
                    <c:forEach var="item" items="${categories}">
                        <option value="${item.idCategorie}">${item.nomCategorie}</option>
                    </c:forEach>
                </select>
            </div>

            <div>
                <label>Titre</label>
                <input type="text" name="nom_image" />
            </div>

            <div>
                <label>Description</label>
                <textarea name="desc_image"></textarea>
            </div>
        </div>

        <div>

            <input type="file" name="file" size="1024" multiple="multiple"/>
            <input type="submit" name="insertimage" value="Upload Fichier" />
        </div>
    </form>

and here the code for my servlet who make the upload

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {      
    String idcategorie = "null";
    String nomImage = "Pas de titre";
    String descImage = "Pas de description";
    String urlImage = "nullurl";

    isMultipart = ServletFileUpload.isMultipartContent(request);
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter( );
    if( !isMultipart ){
        //redirection vers jsp de "pas d'upload"
    }
    DiskFileItemFactory factory = new DiskFileItemFactory();
    factory.setSizeThreshold(maxMemSize);
    factory.setRepository(new File("c:\\temp"));

    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setSizeMax( maxFileSize );

    try{ 
        List fileItems = upload.parseRequest(request);

        Iterator i = fileItems.iterator();


        while ( i.hasNext () ) 
        {
            FileItem fi = (FileItem)i.next();
            if ( !fi.isFormField () )   
            {
                // Get the uploaded file parameters
                String fieldName = fi.getFieldName();
                String fileName = fi.getName();
                String contentType = fi.getContentType();
                boolean isInMemory = fi.isInMemory();
                long sizeInBytes = fi.getSize();
                // Write the file
                if( fileName.lastIndexOf("\\") >= 0 ){
                    file = new File( filePath + fileName.substring( fileName.lastIndexOf("\\"))) ;
                }else{
                    file = new File( filePath + fileName.substring(fileName.lastIndexOf("\\")+1)) ;
                }
                urlImage = file.getName();
                fi.write( file ) ;
            }

            else 
            {
                String fieldname = fi.getFieldName();
                String fieldvalue = fi.getString();

                if (fieldname.equals("id_categorie"))
                    idcategorie = fieldvalue;
                else if (fieldname.equals("nom_image"))
                    nomImage = fieldvalue;
                else if (fieldname.equals("desc_image"))
                    descImage = fieldvalue;                 
            }               
        }

        //here i adding the URLimage with hibernate in the database

        RequestDispatcher req = request.getRequestDispatcher("/index.jsp");
        req.forward(request, response);

    }catch(Exception ex) {
        System.out.println(ex);
    }
}

So, then i submit the form you see befor, and i will be redirect on index.jsp, if i click the "showAllImage" button i will see the older image and a little icone then a path for the img html tag is not existing. If i manually refresh my directory where my images are, and refresh the website, i will see all the images including the new uploaded image.

Why have it this probleme ? And how can i solve it ?

thanks

Lucsartes
  • 321
  • 2
  • 13

1 Answers1

0

I resolved the probleme => tomcat is clone the directory and this directory will be dynamique, il link to this directory and it works. If someone want more explication only ask me

Lucsartes
  • 321
  • 2
  • 13