| Author |
BufferedWriter for Mulitple file type
|
Myat Thu
Greenhorn
Joined: Dec 10, 2008
Posts: 7
|
|
Hi All, I want multiple files to upload to Web(or)FTP Server. So, now what i have been done is i carried whichever files and wherever location from client browser to servlet file. Here is my servlet code. My problem is i don't know how to write multiple file to server. If i need to use BufferedWriter to write multiple file, how should i do. I want to write not just single file, i need to write multiple files and mulitple files type. Please give me suggestion. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ try{ boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (!isMultipart) { } else { InputStreamReader input = new InputStreamReader(request.getInputStream()); BufferedReader buffer = new BufferedReader(input); String line =""; do{ line = buffer.readLine(); System.out.println("Multipart data " + line ); }while(line != null); } }catch(Exception e){ e.printStackTrace(); } } Here is my console output. 10:03:29,390 INFO [STDOUT] Multipart data -----------------------------7d85d1d7027c 10:03:29,390 INFO [STDOUT] Multipart data Content-Disposition: form-data; name="txtFile1"; filename="D:\TestForUpload\Phrase1.txt" 10:03:29,390 INFO [STDOUT] Multipart data Content-Type: text/plain 10:03:29,390 INFO [STDOUT] Multipart data 10:03:29,390 INFO [STDOUT] Multipart data Form-based File upload in HTML is defined in RFC 1867. 10:03:29,390 INFO [STDOUT] Multipart data The enctype attribute of the form element in HTML specifies the content 10:03:29,390 INFO [STDOUT] Multipart data type of the data submitted to the server. 10:03:29,390 INFO [STDOUT] Multipart data The default encrypt type is application/x-www-form-urlencoded . 10:03:29,406 INFO [STDOUT] Multipart data This content type is not efficient for forms containing non-ASCII data, 10:03:29,406 INFO [STDOUT] Multipart data binary data or files. In such case, the encrypt type multipart/form-data is used. 10:03:29,406 INFO [STDOUT] Multipart data A typical HTML form for file upload looks like: 10:03:29,406 INFO [STDOUT] Multipart data -----------------------------7d85d1d7027c 10:03:29,406 INFO [STDOUT] Multipart data Content-Disposition: form-data; name="txtFile1"; filename="D:\TestForUpload\Phrase2.txt" 10:03:29,406 INFO [STDOUT] Multipart data Content-Type: text/plain 10:03:29,406 INFO [STDOUT] Multipart data 10:03:29,406 INFO [STDOUT] Multipart data Once the form is submitted, the binary multipart form-data is available in the HttpServletRequest 10:03:29,406 INFO [STDOUT] Multipart data as an InputStream. The regular form data are not available as Parameter and 10:03:29,406 INFO [STDOUT] Multipart data request.getParameter(?hsubmit?h) will always return null. So for any data submitted, 10:03:29,406 INFO [STDOUT] Multipart data you will have to know the content and then retrieve the content by parsing through the stream: 10:03:29,406 INFO [STDOUT] Multipart data -----------------------------7d85d1d7027c 10:03:29,406 INFO [STDOUT] Multipart data Content-Disposition: form-data; name="txtFile1"; filename="D:\TestForUpload\Phrase3.txt" 10:03:29,406 INFO [STDOUT] Multipart data Content-Type: text/plain 10:03:29,406 INFO [STDOUT] Multipart data 10:03:29,406 INFO [STDOUT] Multipart data You can write a parser yourself to analyze each multipart data from the stream and grab it 10:03:29,406 INFO [STDOUT] Multipart data as they apply to your application. There are some good ones already written. 10:03:29,406 INFO [STDOUT] Multipart data Apache has a Commons Fileupload Library, now let?fs see how we can use it to upload files. 10:03:29,406 INFO [STDOUT] Multipart data -----------------------------7d85d1d7027c 10:03:29,406 INFO [STDOUT] Multipart data Content-Disposition: form-data; name="txtFileOther"; filename="" 10:03:29,406 INFO [STDOUT] Multipart data Content-Type: application/octet-stream 10:03:29,406 INFO [STDOUT] Multipart data 10:03:29,406 INFO [STDOUT] Multipart data 10:03:29,406 INFO [STDOUT] Multipart data -----------------------------7d85d1d7027c-- 10:03:29,406 INFO [STDOUT] Multipart data null
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12265
|
|
Important note:
Once the form is submitted, the binary multipart form-data
You need to read and write a binary stream - NOT a character stream. Reader and Writer io always attempts to interpret a stream as characters which is bound to cause trouble. Bill
|
Java Resources at www.wbrogden.com
|
 |
 |
|
|
subject: BufferedWriter for Mulitple file type
|
|
|