You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Brian Cook <bc...@printtime.com> on 2004/01/09 03:00:01 UTC

[commons-file upload] Will not save just the file name

Hi,

I am having a problem with the commons FileUpload package.  It works 
fine on my development work station(Win2k).  I run into a problem with 
Internet Explorer(5.5, 6.0) after I transfer the servlet file to my 
production box(Linux).  Mozilla, Opera, and Safari are all fine, but 
when uploading a file with Internet Explorer it returns the entire 
client side path with the file name.

EXAMPLE :

When uploading the file example.txt the item.getName(); object returns :
C:\dev\store\rpm\5\example.txt as the file name.

I tried the fix posted on :
http://www.onjava.com/pub/a/onjava/2003/06/25/commons.html?page=3
with out success.


I have included the code and Vital stats below.  Any ideas on what I am 
doing wrong would be great.



<Vital Stats>
Java            : J2SE SDK 1.4.2
Server OS        : Red Hat 9.0     2.4.28
Server Container    : Tomcat 4.1.29
IDE            : Netbeans  3.5.1
Browsers that fail    : Internet Explorer 5.5, 6
Browsers that work    : Mozilla 1.4 Opera 6
Package            : commons-fileupload-1.0.zip
</Vital Stats>


<Code>

DiskFileUpload incomingData = new DiskFileUpload();

     boolean isMultipart = FileUpload.isMultipartContent(req);

     if(isMultipart==false)         {return;}

     java.util.List items = incomingData.parseRequest(req);

     // Parse the list of everything but the field names
     java.util.Iterator list = items.iterator();

     // Now that we have a list of ALL of the fields that were POSTed
     // Start a loop to read though the list one at a time.
     while(list.hasNext()) {

         // If it is actually a field then call another method to
         // copy the field value to the aprpreate variable.
         FileItem item = (FileItem) list.next();
         if(item.isFormField()) {
             writeToSessionObject(item.getFieldName(), item.getString(),
                   session);
         }                        // Close the test for form field

     	// If the item is not a field then it is assumed to be a file,
         // and it is written to the disk.
         else {                   // opens else for field type test

         // This should be the file name, but IE returns
         // The entire path
         String itemName = item.getName();

          // Test for and empty file field
             if(itemName.length()<2) {/*orderInfo.FileUploaded=false;*/ }

         else {         // Opens else in readIncommingData

                     // Define the File object that will be used to 
              		    // extract the file.
                     File fullFile = new File(itemName);

                     // This second File object will actually write the 
                     		    // file to the back up dir.  It will put the 
file                  		    // name from the fullFile object
                     File savedFile = new File(getServletContext()
                       .getRealPath(File.separator)+"uploadedFiles"
                       +File.separator,fullFile.getName());

                     // Write the file to disk
                     item.write(savedFile);

                     // Save the name of the file that was uploaded
             	    // in the session object.
                     session.putValue("fileName",fullFile.getName());

                     // Record in the system log that a file was
              	    // uploaded and where it was saved
                     System.out.println("Ok, "+fullFile.getName()
                     +" was writen to:\n"+savedFile.toString());
                 }                           // Closes nested else
              }                              //
           }                                 //

</Code>

-- 
Brian Cook
Variable Data \
Web Services Consultant
Print Time Inc.
bcook@printtime.com



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [commons-file upload] Will not save just the file name

Posted by Martin Cooper <ma...@apache.org>.
On Thu, 8 Jan 2004, Brian Cook wrote:

> Hi,
>
> I am having a problem with the commons FileUpload package.

Actually you're not. ;-) You're having a problem with the browsers. The
FileUpload component returns to you exactly what the browser provided to
it. It doesn't do anything with it at all, and neither should it.
Unfortunately, different browsers behave differently in this respect,
which is why you are seeing file names in some cases and full paths in
others.

--
Martin Cooper


> It works
> fine on my development work station(Win2k).  I run into a problem with
> Internet Explorer(5.5, 6.0) after I transfer the servlet file to my
> production box(Linux).  Mozilla, Opera, and Safari are all fine, but
> when uploading a file with Internet Explorer it returns the entire
> client side path with the file name.
>
> EXAMPLE :
>
> When uploading the file example.txt the item.getName(); object returns :
> C:\dev\store\rpm\5\example.txt as the file name.
>
> I tried the fix posted on :
> http://www.onjava.com/pub/a/onjava/2003/06/25/commons.html?page=3
> with out success.
>
>
> I have included the code and Vital stats below.  Any ideas on what I am
> doing wrong would be great.
>
>
>
> <Vital Stats>
> Java            : J2SE SDK 1.4.2
> Server OS        : Red Hat 9.0     2.4.28
> Server Container    : Tomcat 4.1.29
> IDE            : Netbeans  3.5.1
> Browsers that fail    : Internet Explorer 5.5, 6
> Browsers that work    : Mozilla 1.4 Opera 6
> Package            : commons-fileupload-1.0.zip
> </Vital Stats>
>
>
> <Code>
>
> DiskFileUpload incomingData = new DiskFileUpload();
>
>      boolean isMultipart = FileUpload.isMultipartContent(req);
>
>      if(isMultipart==false)         {return;}
>
>      java.util.List items = incomingData.parseRequest(req);
>
>      // Parse the list of everything but the field names
>      java.util.Iterator list = items.iterator();
>
>      // Now that we have a list of ALL of the fields that were POSTed
>      // Start a loop to read though the list one at a time.
>      while(list.hasNext()) {
>
>          // If it is actually a field then call another method to
>          // copy the field value to the aprpreate variable.
>          FileItem item = (FileItem) list.next();
>          if(item.isFormField()) {
>              writeToSessionObject(item.getFieldName(), item.getString(),
>                    session);
>          }                        // Close the test for form field
>
>      	// If the item is not a field then it is assumed to be a file,
>          // and it is written to the disk.
>          else {                   // opens else for field type test
>
>          // This should be the file name, but IE returns
>          // The entire path
>          String itemName = item.getName();
>
>           // Test for and empty file field
>              if(itemName.length()<2) {/*orderInfo.FileUploaded=false;*/ }
>
>          else {         // Opens else in readIncommingData
>
>                      // Define the File object that will be used to
>               		    // extract the file.
>                      File fullFile = new File(itemName);
>
>                      // This second File object will actually write the
>                      		    // file to the back up dir.  It will put the
> file                  		    // name from the fullFile object
>                      File savedFile = new File(getServletContext()
>                        .getRealPath(File.separator)+"uploadedFiles"
>                        +File.separator,fullFile.getName());
>
>                      // Write the file to disk
>                      item.write(savedFile);
>
>                      // Save the name of the file that was uploaded
>              	    // in the session object.
>                      session.putValue("fileName",fullFile.getName());
>
>                      // Record in the system log that a file was
>               	    // uploaded and where it was saved
>                      System.out.println("Ok, "+fullFile.getName()
>                      +" was writen to:\n"+savedFile.toString());
>                  }                           // Closes nested else
>               }                              //
>            }                                 //
>
> </Code>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org