Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Open Source Projects
Search Coderanch
Advance search
Google search
Register / Login
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
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Other Open Source Projects
How to display all headers of multipart request?
senol ozer
Greenhorn
Posts: 2
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi all!
I would like process multipart request from my
servlet
. For this, I use the API Apache Common FileUpload.
Here is an example of my request:
--GWqdHAxkdeIb8vSZKRRMInQ23WH77j Content-Disposition: form-data; name="identifiant" Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 8bit Content-ID: contentId1 Value1 --GWqdHAxkdeIb8vSZKRRMInQ23WH77j Content-Disposition: form-data; name="sessionID" Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 8bit Content-ID: contentId2 value2
Here is my code:
boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { // Create a factory for disk-based file items FileItemFactory factory = new DiskFileItemFactory(); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Parse the request try { @SuppressWarnings("unchecked") List<FileItem> items = upload.parseRequest(request); // Process the uploaded items Iterator<FileItem> iter = items.iterator(); int i = 0; while (iter.hasNext()) { System.out.println("*************************"); System.out.println("part : " + ++i); System.out.println("*************************"); FileItem item = iter.next(); System.out.println("getFieldName : " + item.getFieldName()); System.out.println("getName : " + item.getName()); System.out.println("getString : " + item.getString()); System.out.println("toString : " + item.toString()); if (item.isFormField()) { // processFormField(item); } else { // processUploadedFile(item); } } } catch (FileUploadException e) { e.printStackTrace(); } }
I can't display my "content-id" field.
Can you help me please?
Clivant Yeo
Ranch Hand
Posts: 124
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
Think you can achieve what you want by doing the following:
Use the getItemIterator method instead of the parseRequest method.
Use the FileItemIterator returned to iterate through the FileItemStream instances.
Use the openStream method of the FileItemStream instances to read the contents.
I didn't try it yet, but that's what I can gather by reading the API.
Hope it helps!
Clivant Yeo
My Personal Website
senol ozer
Greenhorn
Posts: 2
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks for your reply!
I tried your method but it display only the value of the part. For my request example, it display only
11:35:23,770 INFO [STDOUT] ************************* 11:35:23,770 INFO [STDOUT] part : 1 11:35:23,770 INFO [STDOUT] Value1 11:35:23,770 INFO [STDOUT] ************************* 11:35:23,770 INFO [STDOUT] part : 2 11:35:23,770 INFO [STDOUT] Value2
Tere is my code:
boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { // Create a factory for disk-based file items FileItemFactory factory = new DiskFileItemFactory(); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); try { @SuppressWarnings("unchecked") FileItemIterator fileItemIterator = upload .getItemIterator(request); int i = 0; while (fileItemIterator.hasNext()) { FileItemStream fileItemStream = fileItemIterator.next(); System.out.println("*************************"); System.out.println("part : " + ++i); InputStream input = fileItemStream.openStream(); BufferedReader buffer = new BufferedReader( new InputStreamReader(input)); String ligne = null; while ((ligne = buffer.readLine()) != null) { System.out.println(ligne); } } } catch (FileUploadException e) { e.printStackTrace(); } }
Clivant Yeo
Ranch Hand
Posts: 124
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hey Senol,
I took a look at the api again. It seems that FileItemStream extends FileItemHeadersSupport, which means that there is a getHeaders() method available from the fileItemStream instances.
API for FileItemStream
API for FileItemHeadersSupport
I have a strong feeling that the getHeaders() method is what you are looking for.
I don't have a
Java
web container installed on my machine, but let me know if this works.
Clivant Yeo
My Personal Website
With a little knowledge, a
cast iron skillet
is non-stick and lasts a lifetime.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
using ServletFileUpload , List size is always becoming zero
Apache File Upload - which JAR is required?
Submit multipart and form data
upload.parseRequest(request); giving null.
getting nullPointerException on line upload.parseRequest(request);
More...