Path: csiph.com!aioe.org!.POSTED!not-for-mail From: parmenides Newsgroups: comp.lang.java.programmer Subject: How to upload a file with jsp? Date: Sun, 24 Jul 2016 10:25:54 +0800 Organization: Aioe.org NNTP Server Lines: 47 Message-ID: NNTP-Posting-Host: KKephej3o10WsolWfXEGyA.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=gbk; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 X-Notice: Filtered by postfilter v. 0.8.2 X-Mozilla-News-Host: news://nntp.aioe.org:119 Xref: csiph.com comp.lang.java.programmer:35120 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:
id: name: file:
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:
...
'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.