找到你要的答案

Q:how to get string parameter from request when using multipart/form-data? [duplicate]

Q:如何使用multipart/form-data当请求字符串参数?[重复]

This question already has an answer here:

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

这个问题在这里已经有了答案:

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">

在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
answer1: 回答1:

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();
}

因为当你形成enctype =“multipart/form-data”。你不能通过请求得到其他形式的领域。getParameter(“paramnname”);。它总是给你零。

你必须使用FormItem的isformfield()检查定期现场或文件。

例子:

        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();
}
answer2: 回答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));

加上回答和解决的问题”Sayo Oladeji

若要获取输入字段的值,您可以使用以下:

System.out.println("Got a form field: " + item.getFieldName()  + " " + Streams.asString(stream));
html  google-app-engine  servlets  file-upload  servlet-filters