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


Groups > comp.lang.java.programmer > #35120 > unrolled thread

How to upload a file with jsp?

Started byparmenides <xparmenides@yahoo.com>
First post2016-07-24 10:25 +0800
Last post2016-07-23 23:11 -0400
Articles 2 — 2 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#35120 — How to upload a file with jsp?

Fromparmenides <xparmenides@yahoo.com>
Date2016-07-24 10:25 +0800
SubjectHow to upload a file with jsp?
Message-ID<nn18vh$1jll$1@gioia.aioe.org>
Hi,

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).

A natual idea is to take two separate forms, one is for submitting id 
and name, the other for transfering file. But I think this scheme is so 
clumsy. An alternative scheme is to use FTP to upload file, rather than 
HTTP's file transferring. But this scheme requires applet running in 
browser which further require JVM in client. Obviously, this make 
deployment more difficult.

Is there any suggestion? Thanks a lot.

[toc] | [next] | [standalone]


#35122

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-07-23 23:11 -0400
Message-ID<nn1bli$1m5t$1@gioia.aioe.org>
In reply to#35120
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web