Problem troubleshooting
1.java.lang.IllegalStateException
java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.commons.CommonsMultipartFile'
Picture Description:
The reason is that there is no way to add a request when submitting a form. You can only write a post request. By default, the form is submitted as a get request.
1. Multi part (enctype = "multipart / form data") form * * 2. The request must be a post request, because post can transfer binary and text, because the get request file only supports about 2k-4k and can only transfer text, not binary, and all cannot use the get method**
<form action="${pageContext.request.contextPath}/upload" enctype="multipart/form-data" method="post"> <input type="file" name="file"> <input type="submit" value="upload"> </form>
enctype is set to "multipart / form data", which can upload the required resources in binary form
2. Detailed explanation of enctype*
The attribute enctype manages the MIME (Multipurpose Internet Mail Extensions) encoding of the form. There are three optional values: 1. application/x-www-form-urlencoded --- encode all characters before sending (default) (spaces are encoded as' + 'and special characters are encoded as ASCII hexadecimal characters). It is used to set the encoding of form transmission, which is mostly used for text upload.
Eg: xmlhttp.setrequestheader ("content type", "application / x-www-form - urlencoded") in Ajax will report an error if it is not written. enctype=application/x-www-form-urlencoded can not be written in the form form form, because the default HTML form is this transmission encoding type. 2. Multipart / form data --- no character encoding. This value must be used when using a form that contains a file upload control. Specify the special type of data to be transmitted. This value should also be used for uploaded non text content, such as pictures or mp3. However, the value of the corresponding form cannot be obtained directly by using request in the form form. 3. text/plain --- space is converted to "+" plus sign, but special characters are not encoded.
3. Common errors of commonsmultipartfile and MultipartFile
1. MultipartFile and CommonsMultipartFile are used to receive uploaded file streams!
2. MultipartFile is an interface. CommonsMultipartFile is the implementation class of MultipartFile interface!
3. When you use MultipartFile as a formal parameter to receive an uploaded file, you can use it directly. When CommonsMultipartFile receives the uploaded file as a formal parameter, the @ RequestParam annotation must be added (otherwise an error will be reported:
exception is java.lang.NoSuchMethodException: org.springframework.web.multipart.commons.CommonsMultipartFile)
Correct example
@RequestMapping("/upload1") public String fileUpload1(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {return "";}
@RequestMapping("/upload1") public String fileUpload1(MultipartFile file, HttpServletRequest request) throws IOException {return "";}
4. Registration of org.springframework.web.multipart.commons.CommonsMultipartResolver in the spring container in the Commons IO package
Error condition:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
Correct example
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8" /> <property name="maxUploadSize" value="102400000" /> <property name="maxInMemorySize" value="4096" /> </bean>
Explanation: because id="multipartResolver" matches other classes, an error java.lang.NullPointerException is reported after renaming