commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Omar Adobati" <o.adob...@tin.it>
Subject Re: [FileUpload] How to specify the destination of the upload
Date Tue, 28 Sep 2004 18:15:12 GMT
I have problems again, here is the full code I'm trying to use.
As a result I always have a java.lang.Nullpointer Exception when the last 
line is executed.
Why?

thanks 4 your time

boolean isMultipart = FileUpload.isMultipartContent(request);
  if(isMultipart){
    DiskFileUpload upload = new DiskFileUpload();

    List item = upload.parseRequest(request);

    Iterator iter  = item.iterator();
    while(iter.hasNext()){
      FileItem fitem = (FileItem)iter.next();
      if(fitem.isFormField()){
        [...]
      }
      if(!fitem.isFormField()){
        //just the filname without local path
        String fileonly = "";
        if(null != fitem.getName() ){
          int idx = fitem.getName().lastIndexOf("\\");
          if(idx != -1){
            fileonly = fitem.getName().substring(idx+1, 
fitem.getName().length());
          }
        }
        //make the file to upload saing that the dest folder is /img/
        // i also yet try to img/ instead of /img/
        File uploadFile = new File("/img/" + fileonly);
        //try to write the file... executing this i have a 
java.lang.NullpointerException
        //I tried to comment this line and all before works
        fitem.write(uploadFile);
      }
    }
  }
----- Original Message ----- 
From: "Martin Cooper" <mfncooper@gmail.com>
To: "Omar Adobati" <o.adobati@tin.it>
Cc: "Jakarta Commons Users List" <commons-user@jakarta.apache.org>
Sent: Tuesday, September 28, 2004 6:50 PM
Subject: Re: [FileUpload] How to specify the destination of the upload


> On Tue, 28 Sep 2004 17:46:35 +0200, Omar Adobati <o.adobati@tin.it> wrote:
>> Thankyou very much for your answer.
>> I'll try to explain better what is my trouble.
>>
>> Suppose I have a file called imagine.gif on a c:\mydir, so the path to 
>> the
>> latter file is c:\mydir\imagine.gif.
>> Well,  now I need to upload imagine.gif into a server directory called 
>> /img
>> (note: I'm using Tomcat 5.0) that is into the
>> %TOMCAT_HOME%/webapps/myProject. I think that at this point is all easy
>> clear (isn't so?).
>> Now how I have to write the code to do that?
>>
>> <!-- extract from previous code -->
>>      if(!fitem.isFormField()){
>>        //this give me c:\mydir\imagine.gif - so, with the file full path
>>        System.out.println(fitem.getName());
>>        //here i build the file to upload
>>        File uploadFile = new File(fitem.getName());
>>        //here I overwrite the file I intend to upload
>>        //I don't know how to write code to put it into the server folder
>>        fitem.write(uploadFile);
>>      }
>>
>> Hoping this help me and u to better anderstand the problem,
>>   Tanks in advaice
>
> First, you need to decide how you want to name the uploaded file in
> your /img directory.
>
> If you want to give it the same name as the original file that was
> uploaded, then you will need to extract that name from the FileItem,
> since that may or may not contain the complete path. This is just
> regular use of the File class; nothing specific to FileUpload. One
> problem with this approach is that if there are multiple uploads of
> files with the same name, then you will be overwriting the file each
> time it is uploaded. So you might not want to use this approach, and
> instead make up your own naming convention for uploaded files.
>
> Once you know what you want to name the file, you just need to
> construct the appropriate File instance with the path and file name,
> and pass that to write(). That's it.
>
> --
> Martin Cooper
>
>
>>
>> Omar
>>
>>
>>
>>
>> ----- Original Message -----
>> From: "Martin Cooper" <mfncooper@gmail.com>
>> To: "Jakarta Commons Users List" <commons-user@jakarta.apache.org>
>> Sent: Tuesday, September 28, 2004 5:20 AM
>> Subject: Re: [FileUpload] How to specify the destination of the upload
>>
>> > I'm not clear on whether you're asking about the path to the file the
>> > user selected for upload, or about the location of the file after it
>> > has been uploaded.
>> >
>> > In the first case, whether or not you get the full path to the file
>> > the user selected is up to the browser. Most browsers provide only the
>> > base file name, and not the full path. This is primarily for security
>> > reasons - it's not a good idea to expose the user's local file system
>> > to the server. Some browsers do, however, provide the full path.
>> >
>> > In the second case, you simply need to decide for yourself where you
>> > want the file to be written, and then pass that to the
>> > FileItem.write() method. You don't want to do what you have in the
>> > code you provide, since that just takes whatever the browser provided
>> > for the file name. If that provides a path, then it could try to write
>> > anywhere on the server and you are not checking where that might be.
>> > If it doesn't provide a path, then it's going to be written in
>> > whatever the web container deems to be the current working directory,
>> > which is also probably not what you want.
>> >
>> > --
>> > Martin Cooper
>> >
>> >
>> > On Mon, 27 Sep 2004 12:11:57 +0200, Omar Adobati <o.adobati@tin.it> 
>> > wrote:
>> >> This is the code I've write to perform a fileupload.
>> >> It's seems to work nice but there is a definitive problem... I don't
>> >> know, and can't understand, how i can specify the destination path to 
>> >> the
>> >> file I need to upload.
>> >> I'm using commons fileUpload package...
>> >>
>> >> Does anybody can help me?
>> >>
>> >> Tanks in advice,
>> >>  Omar (from Italy)
>> >>
>> >> ---
>> >>
>> >> boolean isMultipart = FileUpload.isMultipartContent(request);
>> >>  if(isMultipart){
>> >>    DiskFileUpload upload = new DiskFileUpload();
>> >>
>> >>    List item = upload.parseRequest(request);
>> >>    Iterator iter  = item.iterator();
>> >>    while(iter.hasNext()){
>> >>      DefaultFileItem fitem = (DefaultFileItem)iter.next();
>> >>      if(fitem.isFormField()){
>> >>        out.println(fitem.getFieldName());
>> >>        out.println(fitem.getName());
>> >>        out.println(fitem.getSize());
>> >>        out.println(fitem.getString());
>> >>      }
>> >>      if(!fitem.isFormField()){
>> >>        out.println(fitem.getFieldName() + "<br>");
>> >>        out.println(fitem.getName() + "<br>");
>> >>        out.println(fitem.getSize() + "<br>");
>> >>        out.println(fitem.getContentType() + "<br>");
>> >>        out.println(fitem.isInMemory() + "<br>");
>> >>        File uploadFile = new File(fitem.getName());
>> >>        fitem.write(uploadFile);
>> >>      }
>> >>    }
>> >>  }
>> >>
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>> > For additional commands, e-mail: commons-user-help@jakarta.apache.org
>> >
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 


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


Mime
View raw message