Some precautions and solutions for jsp form form attribute enctype

In the form tag, enctype is used to mark the MIME encoding format, multipart/form-data is the encoding format for file uploading, and the default is application/x-www-form-urlencoded, which cannot be used for file uploading.

Therefore, a form with enctype="multipart/form-data" format will report a null pointer exception when request.getParameter gets the value of the text attribute in the background.

So in an item that has both a textfield and a file upload is my workaround is:

[java]  view plain  copy
  1. private Map<String,String> parameters =new HashMap<String,String>();</span>  
[java]  view plain  copy
  1. Design a map property  
[java]  view plain  copy
  1. Use an upload component of apache:  
[java]  view plain  copy
  1. DiskFileItemFactory factory = new DiskFileItemFactory();  
  2.         ServletFileUpload upload = new ServletFileUpload(factory);  
  3.         try {  
  4.             List items = upload.parseRequest(request);  
  5.             Iterator it = items.iterator();  
  6.             while (it.hasNext()) {  
  7.                 FileItem item = (FileItem) it.next();  
  8.                 if  (item.isFormField()) { //Determine whether it is text field information  
  9.                     parameters.put(item.getFieldName(), item.getString("UTF-8"));  
  10.                       
  11.   
  12.                 }
  13.  The getFieldName in the class is  to get the name attribute value of the input, getString is to get the value value, put these two values ​​into a map and then get it through the key.

  14. The corresponding jar packages are: commons-email - 1.5.jar , commons-fileupload - 1.3.3.jar , commons-io-2.5.jar , mail.jar

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325658571&siteId=291194637