• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

BufferedWriter for Mulitple file type

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Are you here to take over the surface world? Because this tiny ad will stop you!
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic