data in multi data type form

I have html page which contains 3 file input and 3 text inputs. If I use enctype = multipart/form-data in jsp page I am not able to get the test input form fields. These values always show null. If I remove enctype from post form in jsp, I can get the text field inputs but in this case I cannot upload files. So my question is is it possible to have multiple input fields with file input and if yes how to get the text input field names??

Any help on this is appreciated..

Below is the html code

<html>
<body>
<form method="post" action="upload.jsp" enctype="multipart/form-data">
  Office Name: <input type="text" name="officeName" />  <br>
  Doc. Description : <input type="text" name="docDesc" />   <br>
  Document 1 :  <input type="file" name="doc1" />   <br>
  Document 2 :  <input type="file" name="doc2" />   <br>
  Document 3 :  <input type="file" name="doc3" />   <br>
  Remarks : <input type="text" name="remarks" />    <br>

  <br>
    <input type="submit" value="submit" />
</form>

And i am retrieving text and file inputs as

strOffficeName=Request.getParameter("officeName");
strDocDescription=Request.getParameter("docDesc");
strDoc1Path=Request.getParameter("doc1");
strDoc2Path=Request.getParameter("doc2");
strDoc3Path=Request.getParameter("doc3");
strRemarks=Request.getParameter("remarks");

I would take a look at Apache Commons FileUpload. It has a User Guide that explains how to get file uploads from your request.

The section "Processing the uploaded items" shows an example how to process both file uploads and text inputs.


Create a class called fileUploader which returns ServletFileUpload object

private FileUploader()
{

}

public static synchronized ServletFileUpload getservletFileUploader(String tempDir, int maxSizeInMB) 
{
    if(uploader == null)
    {
        DiskFileItemFactory factory = new DiskFileItemFactory();

        factory.setSizeThreshold(1024 * 1024);
        factory.setRepository(new File(tempDir));

        uploader = new ServletFileUpload(factory);

        uploader.setFileSizeMax(maxSizeInMB * 1024 * 1024);
    }

    return uploader;
}

Now you can process a request and read all the data. It handles the files uploaded as well as other form data.

protected MultiPartFormData handleMultiPartRequest(HttpServletRequest request)
throws FileSizeLimitExceededException
{
    if(!isMultipartRequest(request))
        return null;

    ServletFileUpload upload = FileUploader.getservletFileUploader(tempDir, 50);
    MultiPartFormData data = new MultiPartFormData();
    try
    {
        List<FileItem> items = upload.parseRequest(request);

        for (FileItem item : items) 
        {
            if(item.isFormField())
            {
                data.getParameters().put(item.getFieldName(), item.getString());
            }
            else
            {
                String filename = item.getName();

                //Internet explorer and firefox will send the file name differently
                //Internet explorer will send the entire path to the file name including 
                //the backslash characters etc ... we should strip it down
                //THIS IS HACKY
                if(filename.indexOf("") != -1)
                {
                    int index = filename.lastIndexOf("");
                    filename = filename.substring(index + 1);
                }


                if(filename == null || filename.equals(""))
                {
                    //do nothing 
                }
                else
                {
                    File uploadFile = new File(uploadDir + File.separator + randomFileName);
                    item.write(uploadFile);

                                            data.addFile(item.getFieldname(), item.getString());
                }
            }
        }
    }
    catch(FileSizeLimitExceededException e)
    {
        throw e;
    }
    catch(Exception e)
    {
        e.printStackTrace();

    }


    return data;
}

After parsing the request I am storing it in some object called MultipartFormData which can be used to get request parameters

public class MultiPartFormData {

private Hashtable<String, String> parameters;
    private Hashtable<String, String> uploadedFiles;

public MultiPartFormData()
{
    this.parameters = new Hashtable<String, String>();
    this.uploadedFiles = new Hashtable<String, String>();
}

public Hashtable<String, String> getParameters() {
    return parameters;
}
public void setParameters(Hashtable<String, String> parameters) {
    this.parameters = parameters;
}
    public void getParameter(String paramName) {
          if(this.parameters.contains(paramName))
                 return tyhis.parameters.get(paramName);
          return null;
    }
    public void addFile(String key, String filename) {
        uploadedFile.put(key, filename);
    }
    public void getFilename(String key) {
        uploadedFile.get(key);
    }
}
链接地址: http://www.djcxy.com/p/64188.html

上一篇: ascii使用enctype multipart / form时形成数据

下一篇: 多数据类型表格中的数据