Hi,

Oreilly has a way to rename files automatically so that if a file
exists, a new file with a ".1", ".2" etc, is created (or something
 similar).

If that doesn't work for you, you don't really need two passes,
Just keep a vector of java.io.File and use them later when you figured
out what you want to do.

      Enumeration files = multi.getFileNames();
      while (files.hasMoreElements()) {
        String name = (String)files.nextElement();
        String filename = multi.getFilesystemName(name);
        String type = multi.getContentType(name);
        File f = multi.getFile(name);

                        /*
                         *In here preserve the fields in a hash
                         * something like hashFile.put(filename, f)
                         */

        out.println("<p>name: " + name);
        out.println("filename: " + filename);
        out.println("type: " + type);
        if (f != null) {
          out.println("f.toString(): " + f.toString());
          out.println("f.getName(): " + f.getName());
          out.println("f.exists(): " + f.exists());
          out.println("f.length(): " + f.length());
          out.println();
        }
      }

Haven't really tried this, but it should work.

Dror

On Tue, Oct 15, 2002 at 02:08:24PM +0800, hugo wrote:
> Hi
>
> I am trying to do something in a jsp file with the oreilly
> MultipartParser functions and I am getting immensely frustrated as it
> does not work. What I would like to do is:
>
> 1. Get information from a set of form fields with the MultipartParser,
> including upload fields.
>
> 2. Load the filenames from the upload fields into a Vector and compare
> the filenames from that Vector to filenames already in the directory
> where the files will be downloaded. This is in order to avoid files with
> the same filenames overriding existing files.
>
> 3. If the filenames do not exist in the directory, download the files
> into the directory.
>
> What I am getting stuck on is the fact that I have to use
> MultipartParser to read the files from the form and store the filenames
> in a Vector. I then compare with existing filenames, then  would like to
> use MultipartParser AGAIN to write the filenames to the specified
> directory. But the second time around, the MultipartParser does not want
> to write anything to the directory as the method readNextPart()
> apparently only works once.
>
> Here is the code:
>
> 1. Get information from form fields and write filenames into a Vector:
> (Vector is defined and initialised earlier on).
>
> MultipartParser parser = new MultipartParser(request, 10*1024*1024);
>
> Part part = null;
>
> while ((part = parser.readNextPart()) != null) {
> if (part.isFile()) {
> // It's a file part
>  FilePart filePart = (FilePart) part;
>     String fileName = filePart.getFileName();
>     if (fileName != null) {
>         existingfiles.add(fileName);
>     out.println("filename: " + fileName);
>         }
>   }
> }
>
> 2. Compare filenames in Vector with files already in the directory. Note
> that the String "proj_id" is a string which I obtain from a database - I
> didn't put that code in here.
>
> try {
>
>     Process p = Runtime.getRuntime().exec("ls
> /var/www/projects_data/"+proj_id);
>
>     BufferedReader stdInput = new BufferedReader(new
> InputStreamReader(p.getInputStream()));
>             String s = "";
>             BufferedReader stdError = new BufferedReader(new
> InputStreamReader(p.getErrorStream()));
>             if ((s = stdInput.readLine())== null) {
>             samefile = "no";
>             }
>             while ((s = stdInput.readLine())!= null) {
>             StringTokenizer tr = new StringTokenizer(s," ", true );
>             String t = tr.nextToken();
>             for (int i = 0; i < existingfiles.size(); i++) {
>                 out.println ("<br>Vector value is: " +
> existingfiles.get(i).toString());
>                if (t.equals(existingfiles.get(i).toString()))  {
>                 samefile = "yes";
>                 samefilenames.add(existingfiles.get(i).toString());
>                } else {
>                 samefile = "no";
>                 }
>             }
>           }
>         }   catch (IOException e) {
>             out.println("exception:  ");
>             e.printStackTrace();
>             System.exit(-1);
>          }
>
> 3. Use the parser again, this time to write the files to the directory
> with the line
>
> long size = fileP.writeTo(dir);
>
> (Note that file "dir" has been set tot the directory earlier)
>
> This happens only if the string samefile equals "no" of course. If
> samefile equals "yes" a message is displayed on the screen telling the
> user that the file already exists.
>
> Note that I am even initialising a new FileParser called fileP - I
> thought this may help. But whatever I try, after the line:
>
> while ((part = parser.readNextPart()) != null) {
>
> nothing happens.
>
> It seems to me that the parser cannot be used twice. Help!!
>
> if (samefile.equals("yes")) {
>         out.println ("<br><br><br><br><p><center><font size=+1
> color=#9966CC><b>You have uploaded the following file(s) with the same
> name(s) as existing file(s):");
>         out.println ("<p></p>");
>         for (int j = 0; j < existingfiles.size(); j++) {
>         out.println ("<p>" + existingfiles.get(j).toString());
>         }
>       } else if (samefile.equals("no")) {
>        while ((part = parser.readNextPart()) != null) {
>        String name = part.getName();
>        out.println("name: " + name);
>         if (part.isFile()) {
>           FilePart fileP = (FilePart) part;
>           String fileN = fileP.getFileName();
>            out.println("filename: " + fileN);
>             if (fileN != null) {
>                 long size = fileP.writeTo(dir);
>            }
>         }
>        }
>       }
>
>
> Any help, particularly a code example on how to use MultipartParser
> twice to upload and save files, or even on how to do all of this
> differently will be greatly appreciated.
>
> Thanks
>
> Hugo
> --
> Dr Hugo Bouckaert
> Systems and Programming Engineer
>
> GeoInformatics Exploration Australia P/L
> 57 Havelock St
> West Perth, WA 6005
> PO Box 1675, West Perth 6872
>
> Ph:       61 08 9420 7400
> Fax:      61 08 9226 1299
>
> www.geoinformex.com
>
> ------------------------------------------------------------------------
> This email and any attachments may be confidential or legally
> privileged. If you received this message in error or are not the
> intended recipient, you should destroy the e-mail message and any
> attachments or copies, and you are prohibited from retaining,
> distributing, disclosing or using any information contained herein.
> Please inform us of the erroneous delivery by return e-mail. Thank you
> for your cooperation.
>
> ===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> JSP-INTEREST".
> For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
> DIGEST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
> http://archives.java.sun.com/jsp-interest.html
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.jsp
> http://www.jguru.com/faq/index.jsp
> http://www.jspinsider.com

--
Dror Matalon
Zapatec Inc
1700 MLK Way
Berkeley, CA 94709
http://www.zapatec.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to