commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Cooper <mfncoo...@gmail.com>
Subject Re: Rename File While Upload
Date Tue, 02 Nov 2004 16:06:45 GMT
On Tue, 2 Nov 2004 09:25:19 -0500, Vito Campanelli
<vito.campanelli@mcgill.ca> wrote:
> Thanks Martin, I understand that from the user manual.

No, I don't think you do. In the code below, you're trying to use the
field *name* as the file name. If that's what you wanted, you could
have hardcoded it in the first place. I believe you want the field
*value* instead. How to get that was explained in my last message.

Also, it is very, very dangerous to take an arbitrary string from a
user and write to a file in that location. What if the user specified
the path to a crucial system file? Your code would blindly overwrite
it, potentially causing a fatal system crash.

And please stop removing the mailing list from your messages. Other
people could be answering your questions, and other people might be
interested in the answers.

--
Martin Cooper


> I am trying to rename the file the client uploads to the string they
> enter in text foo (<input type="text" name="foo">). I don't know how to
> do that.
> I thought I could do the following:
> 
> String foo = item.getFieldName();
> System.out.println(foo);
> 
> File fNew= new File(application.getRealPath("/"), foo);
> 
> But I don't get the results I want.
> Does this make sense?
> 
> Thanks
> Viito
> 
> 
> 
> 
> -----Original Message-----
> From: Martin Cooper [mailto:mfncooper@gmail.com]
> Sent: Monday, November 01, 2004 8:42 PM
> To: Vito Campanelli
> Cc: Jakarta Commons Users List
> Subject: Re: Rename File While Upload
> 
> On Mon, 1 Nov 2004 18:57:15 -0500, Vito Campanelli
> <vito.campanelli@mcgill.ca> wrote:
> > Hi Martin,
> >
> > Yes I do understand that, and yes I did read the User Guide.
> > I just don't understand how you can assign a string to a specific form
> field.
> > How doo a specify one field from another field.
> 
> OK, now _you_ have lost _me_. ;-}
> 
> What do you mean by "assign a string to a specific form field"?
> 
> <input type="text" name="foo"> and someone types in "foovalue" results
> in a FileItem where:
> 
> item.isFormField() --> true
> item.getFieldName() --> "foo"
> item.getString() --> "foovalue"
> 
> <input type="file" name="bar"> and someone chooses a file named
> "barfile" results in a FileItem where:
> 
> item.isFormField() --> false
> item.getFieldName() --> "bar"
> item.getName() --> "barfile"
> 
> Is that any clearer?
> 
> --
> Martin Cooper
> 
> >
> >
> >
> >
> > -----Original Message-----
> > From:   Martin Cooper [mailto:mfncooper@gmail.com]
> > Sent:   Mon 11/1/2004 4:51 PM
> > To:     Vito Campanelli
> > Cc:     Jakarta Commons Users List
> > Subject:        Re: Rename File While Upload
> > On Mon, 1 Nov 2004 16:37:02 -0500, Vito Campanelli
> > <vito.campanelli@mcgill.ca> wrote:
> > > Hi Martin,
> > >
> > > You totally lost me.
> >
> > Each input field in your form will be provided to you as a FileItem
> > instance in the list returned by parseRequest(). In your example
> > below, that list will contain two entries:
> >
> > 1) The 'file1' field, for which isFormField() will return false.
> > 2) The 'regno' field, for which isFormField() will return true.
> >
> > I had hoped that this was clear in the User Guide, which I assume
> you've read:
> >
> > http://jakarta.apache.org/commons/fileupload/using.html
> >
> > See the section on "Processing the uploaded items". If that's not
> > clear, it would be good to know how I could better explain this.
> >
> > --
> > Martin Cooper
> >
> > > If my File Upload Page looks like this:
> > >
> > > <FORM name="filesForm" action="ProcessFileUpload.jsp"
> > >    method="post" enctype="multipart/form-data">
> > >        File 1:<input type="file" name="file1"/><br/>
> > >        Regno: <input type="text" name = "regno"/><br/>
> > >        <input type="submit" name="Submit" value="Upload Files"/>
> > >    </FORM>
> > >
> > > Can you help me with what my ProcessUploadFile.jsp will need?
> > >
> > >
> > >
> > > Thanks
> > > Vito
> > >
> > > -----Original Message-----
> > > From: Martin Cooper [mailto:mfncooper@gmail.com]
> > > Sent: Monday, November 01, 2004 4:18 PM
> > > To: Vito Campanelli
> > > Cc: Jakarta Commons Users List
> > > Subject: Re: Rename File While Upload
> > >
> > > On Mon, 1 Nov 2004 15:44:16 -0500, Vito Campanelli
> > > <vito.campanelli@mcgill.ca> wrote:
> > > > Hi Martin,
> > > >
> > > > One last question...
> > > >
> > > > I added a text field in my UploadFiles.html file called "code"
> > > > In my ProcessFileUpload.jsp file I try to reference this field
> with
> > > the
> > > > request.getParameter("code"), but I kept getting a null value. Is
> this
> > > > something to do with the commons package?
> > >
> > > It's not so much related to FileUpload as it is to the nature of
> > > multipart requests in general. The container does not parse these
> > > requests, which is why you need FileUpload in the first place, and
> why
> > > request.getParameter() returns null.
> > >
> > > Your "code" field will be one of the FileItem entries in the list
> > > returned by parseUpload(), and the FileItem.isFormField() method
> will
> > > return true for this item.
> > >
> > > By the way, I strongly recommend moving your FileUpload code into a
> > > servlet, rather than having binary upload handling in JSP pages,
> which
> > > are designed for text-based content.
> > >
> > > --
> > > Martin Cooper
> > >
> > > On Mon, 1 Nov 2004 15:44:16 -0500, Vito Campanelli
> > > <vito.campanelli@mcgill.ca> wrote:
> > > > Hi Martin,
> > > >
> > > > One last question...
> > > >
> > > > I added a text field in my UploadFiles.html file called "code"
> > > > In my ProcessFileUpload.jsp file I try to reference this field
> with
> > > the
> > > > request.getParameter("code"), but I kept getting a null value. Is
> this
> > > > something to do with the commons package?
> > > >
> > > > ===ProcessFileUpload.jsp
> > > > <%@ page contentType="text/html;charset=windows-1252"%>
> > > > <%@ page import="org.apache.commons.fileupload.DiskFileUpload"%>
> > > > <%@ page import="org.apache.commons.fileupload.FileItem"%>
> > > > <%@ page import="java.util.List"%>
> > > > <%@ page import="java.util.Iterator"%>
> > > > <%@ page import="java.io.File"%>
> > > > <html>
> > > > <head>
> > > > <meta http-equiv="Content-Type" content="text/html;
> > > > charset=windows-1252">
> > > > <title>Process File Upload</title>
> > > > </head>
> > > > <%
> > > >        System.out.println("Content Type
> ="+request.getContentType());
> > > >        out.println(request.getParameterValues("code"));
> > > >
> > > >        DiskFileUpload fu = new DiskFileUpload();
> > > >        // If file size exceeds, a FileUploadException will be
> thrown
> > > >        fu.setSizeMax(1000000);
> > > >
> > > >        List fileItems = fu.parseRequest(request);
> > > >        Iterator itr = fileItems.iterator();
> > > >
> > > >        while(itr.hasNext()) {
> > > >          FileItem fi = (FileItem)itr.next();
> > > >
> > > >          //Check if not form field so as to only handle the file
> > > inputs
> > > >          //else condition handles the submit button input
> > > >          if(!fi.isFormField()) {
> > > >            System.out.println("\nNAME: "+fi.getName());
> > > >            System.out.println("SIZE: "+fi.getSize());
> > > >            //System.out.println(fi.getOutputStream().toString());
> > > >            File fNew= new
> File(application.getRealPath("/document"),
> > > > fi.getName());
> > > >            //File fNew= new
> File(application.getRealPath("/document"),
> > > > "test"+fi.getContentType());
> > > >
> > > >            System.out.println(fNew.getAbsolutePath());
> > > >            fi.write(fNew);
> > > >          }
> > > >          else {
> > > >            System.out.println("Field ="+fi.getFieldName());
> > > >          }
> > > >        }
> > > > %>
> > > > <body>
> > > > Upload Successful!!
> > > > </body>
> > > > </html>
> > > > ======
> > > >
> > > >
> > > >
> > > >
> > > > Thanks
> > > > Vito
> > > >
> > > > -----Original Message-----
> > > > From: Martin Cooper [mailto:mfncooper@gmail.com]
> > > > Sent: Monday, November 01, 2004 3:33 PM
> > > > To: Vito Campanelli
> > > > Cc: Jakarta Commons Users List
> > > > Subject: Re: Rename File While Upload
> > > >
> > > > On Mon, 1 Nov 2004 15:28:52 -0500, Vito Campanelli
> > > > <vito.campanelli@mcgill.ca> wrote:
> > > > > Thanks Martin, but how can I get the extension? .doc, .pdf,
> .xls?
> > > >
> > > > FileItem.getName() gives you the filename as it was supplied by
> the
> > > > browser. Assuming this has the extension on it, you can extract it
> > > > from that.
> > > >
> > > > --
> > > > Martin Cooper
> > > >
> > > > > Thanks
> > > > >
> > > > >
> > > > >
> > > > > -----Original Message-----
> > > > > From: Martin Cooper [mailto:mfncooper@gmail.com]
> > > > > Sent: Monday, November 01, 2004 3:06 PM
> > > > > To: Jakarta Commons Users List
> > > > > Subject: Re: Rename File While Upload
> > > > >
> > > > > On Mon, 1 Nov 2004 14:43:08 -0500, Vito Campanelli
> > > > > <vito.campanelli@mcgill.ca> wrote:
> > > > > > Hi Martin,
> > > > > >
> > > > > > Thanks for the reply.
> > > > > >
> > > > > > My user uploads a file filename.doc, once the file gets
> uploaded
> > > or
> > > > > > before, I would like to give it a more meaningful name
> > > > > > (firstname_lastname.doc).
> > > > > >
> > > > > > Is this possible? Right now I am using the following:
> > > > > >
> > > > > > Runtime r = Runtime.getRuntime();
> > > > > >            r.exec("cmd /c rename " ......)
> > > > > >
> > > > > > I just wonder is there something else I could use.
> > > > >
> > > > > You have complete control over what the actual file name is,
> since
> > > you
> > > > > can pass whatever name you want to the write() method.
> > > > >
> > > > > --
> > > > > Martin Cooper
> > > > >
> > > > > > Thanks
> > > > > > Vito
> > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > > -----Original Message-----
> > > > > > From: Martin Cooper [mailto:mfncooper@gmail.com]
> > > > > > Sent: Monday, November 01, 2004 2:08 PM
> > > > > > To: Jakarta Commons Users List
> > > > > > Subject: Re: Rename File While Upload
> > > > > >
> > > > > > On Mon, 1 Nov 2004 12:27:52 -0500, Vito Campanelli
> > > > > > <vito.campanelli@mcgill.ca> wrote:
> > > > > > > Hi,
> > > > > > >
> > > > > > > I am using the FileUpload 1.0 with success.
> > > > > > >
> > > > > > > My question is, is there anyway to rename the file before
it
> > > gets
> > > > > > > uploaded?
> > > > > >
> > > > > > I'm not entirely sure I understand what you mean by this. It's
> not
> > > > > > possible to rename the file on the local disk before
> uploading. On
> > > > the
> > > > > > other hand, it is entirely your choice as to whether or not
to
> use
> > > > the
> > > > > > file name supplied by the browser when the file is uploaded.
> > > > > >
> > > > > > Perhaps you could clarify what it is that you are trying to
> do?
> > > > > >
> > > > > > --
> > > > > > Martin Cooper
> > > > > >
> > > > > > > Thanks in advance.
> > > > > > >
> > > > > > > Vito
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > >
> ---------------------------------------------------------------------
> > > > > > 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
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> > ---------------------------------------------------------------------
> >
> >
> > 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