Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35122

Re: How to upload a file with jsp?

From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: How to upload a file with jsp?
Date 2016-07-23 23:11 -0400
Organization Aioe.org NNTP Server
Message-ID <nn1bli$1m5t$1@gioia.aioe.org> (permalink)
References <nn18vh$1jll$1@gioia.aioe.org>

Show all headers | View raw


On 7/23/2016 10:25 PM, parmenides wrote:
> I have a requirment for my website with Java Server Page to upload a
> client file whose path is specified in a form. After uploaded, the file
> should be named after a person's job id and his name it the sever. For
> example, suppose that in the client, the name of the file to be uploaded
> is 'sth.zip', the id and the name of the person who upload the file are
> '12345' and 'tom', then in the server the uploaded file's name should be
> '12345.tom.zip'.
>
> I have designed a form in a page like this:
>
> <form action="" method="post" enctype="multipart/form-data">
>     id:   <input type="text" id="id" name="id" >
>     name: <input type="text" id="name" name="name" >
>     file: <input type="file" id="file" name="file" >
>     <input type="submit" name ="submit" >
> </form>
>
> And then, I need write some jsp code to deal with the request from the
> above form. Unfortunately, I encountered two situations confict with
> each other. On the on hand, owing to 'multipart/form-data' is specified
> in the attribute 'enctype' of the form, the file can be tranferred
> correctly. But, in jsp code, the parameter 'id' and 'name'can not be
> obtained with 'request.getParameter()'. Therefore, the file name can not
> be named after 'id' and 'name'. I think this case results from the
> encoding type of the form (namely 'multipart/form-data').
>
> On the oter hand, if I remove the "multipart/form-data" from the form
> like this:
>
> <form action="" method="post" >
>     ...
> </form>
>
> 'id' and 'name' can be obtained by 'request.getParameter()', while the
> file can not be uploaded correctly (the file size is zero).

The easy solution is to use this:

https://commons.apache.org/proper/commons-fileupload/

Arne

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

How to upload a file with jsp? parmenides <xparmenides@yahoo.com> - 2016-07-24 10:25 +0800
  Re: How to upload a file with jsp? Arne Vajhøj <arne@vajhoej.dk> - 2016-07-23 23:11 -0400

csiph-web