如何在使用multipart/form-data时从请求中获取字符串参数?

Han*_*naa 2 html google-app-engine servlets file-upload servlet-filters

我制作html页面上传图像与文本框,其中有描述形式.我使用multipart/form-data; 在servlet的dopost中,我使用ServletFileUpload upload = new ServletFileUpload();

获取字符串参数我使用了request.getparameter(); 但它总是给我NULL ??? 我怎么才能得到它 ??

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">
Run Code Online (Sandbox Code Playgroud)

在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
Run Code Online (Sandbox Code Playgroud)

Har*_*hra 5

因为当你有形式时enctype="multipart/form-data".您无法使用其他表单字段request.getParameter("paramnName");.它总是会给你NULL.

你必须使用FormItem's 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();
}
Run Code Online (Sandbox Code Playgroud)