2

i make html page to upload image with text box which has description form it . i have use multipart/form-data ; in the dopost at servlet i get file using ServletFileUpload upload = new ServletFileUpload();

to get string parameter i used request.getparameter(); but it always give me NULL ??? HOw can i get it ??

html ::

<form name="filesForm" action="/upload" method="post" enctype="multipart/form-data">

File : <input type="file" name="file">  
<textarea name = "description"  
 rows = "4" cols = "30">Enter comments here.</textarea>
<input type="submit" name="Submit" value="Upload File">

at servlet :

ServletFileUpload upload = new ServletFileUpload(); 
upload.setSizeMax(500000000);
 FileItemIterator iterator = null;
    try {
        iterator = upload.getItemIterator(req);
    } catch (FileUploadException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } //  to handle contents or 
    // instances  of request.
    FileItemStream item = null;
    try {
        item = iterator.next();
    } catch (FileUploadException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } // get  access each item on iterator.
    java.io.InputStream in = item.openStream(); // allows to read the items contents. 


    Blob imageBlob = new Blob(IOUtils.toByteArray(in)); // store each item on Blob 


    // object after converting them to Bytes.
    /*……….
    Note: we are concerned on uploading images files. So the type of files is either (.jpg or gif)
    ……….
    */
    PersistenceManager pm = PMF.get().getPersistenceManager();
    String counter="1";
    ServletOutputStream outt = resp.getOutputStream();
     //match incoming request Content type and invoke appropriate method for handel request
Hanaa
  • 87
  • 1
  • 2
  • 10

2 Answers2

5

Its because when you have form enctype="multipart/form-data". You can not get other form fields by using request.getParameter("paramnName");. It will always give you NULL.

You have to use FormItem's isFormField() to check if its regular field or file.

Example:

        try {
            ServletFileUpload upload = new ServletFileUpload();
            response.setContentType("text/plain"); 

            FileItemIterator iterator = upload.getItemIterator(request);

            while (iterator.hasNext()) {
              FileItemStream item = iterator.next();

              InputStream stream = item.openStream();

              if (item.isFormField()) {
                  System.out.println("Got a form field: " + item.getFieldName()  + " " +item);

              } else{
                  System.out.println("Got an uploaded file: " + item.getFieldName() +
                          ", name = " + item.getName());
                int len;
                byte[] buffer = new byte[8192];
                while ((len = stream.read(buffer, 0, buffer.length)) != -1) {

                  response.getOutputStream().write(buffer, 0, len);

                }

            }

        }
} catch (FileUploadException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96
  • HOW ??? can you give me an example ??? – Hanaa Apr 30 '12 at 06:23
  • i try it but now it give me exception – Hanaa Apr 30 '12 at 16:59
  • java.lang.NoClassDefFoundError: java.rmi.server.UID is a restricted class. Please see the Google App Engine developer's guide for more details. at com.google.appengine.tools.development.agent.runtime.Runtime.reject(Runtime.java:51) at org.apache.commons.fileupload.disk.DiskFileItem.(DiskFileItem.java:103) at org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:196) at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:358) – Hanaa Apr 30 '12 at 16:59
  • I guess you can not use `DiskFileItemFactory` with `GAE`. Check this link http://stackoverflow.com/questions/9501527/java-lang-noclassdeffounderror-java-rmi-server-uid-is-a-restricted-class and also edited example – Hardik Mishra May 01 '12 at 03:39
  • 1
    The line `System.out.println("Got a form field: " + item.getFieldName() + " " +item);` does not print the value of a corresponding form field. Please how do I get the value of a form field? – Sayo Oladeji Jan 27 '13 at 17:26
  • @HardikMishra Your absolutely right. Just saved me several hours. – Usman Mutawakil Feb 04 '13 at 03:06
2

Adding onto the answer and addressing the question @Sayo Oladeji

To get the value of an input field you can use the following:

System.out.println("Got a form field: " + item.getFieldName()  + " " + Streams.asString(stream));
CYMA
  • 621
  • 7
  • 15