org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Current request is not a multipart request


在springMVC中寫文件上傳的時候,前端頁面是jsp,具體代碼是

<form action="/user/up/file" method="post">
<input type="file" name="upfile" >
<button>提交</button>
</form>


在controller文件中的代碼如下
@PostMapping("/up/file")
@ResponseBody
public String fileup(@RequestParam("upfile")MultipartFile mf){
logger.info("name:"+mf.getName());
logger.info("size:"+mf.getSize());
Path path=Path.of("D://kfm"+mf.getOriginalFilename());
try {
mf.transferTo(file);
InputStream in= mf.getInputStream();
Files.copy(in,path,StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
System.out.println("上傳文件錯誤");
e.printStackTrace();
}
return "success";
}
會出現如標題的錯誤,原因是:在前端傳參數時,只傳入文件名,沒有上傳文件,因此,controller中接收時會出現傳入參數錯誤。

解決方法:
<form action="/user/up/file" method="post" enctype="multipart/form-data">
<input type="file" name="upfile" >
<button>提交</button>
</form>

multipart/form-data是將文件以二進制的形式上傳,這樣可以實現多種類型的文件上傳。



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 org.springframework.web.multipart.MultipartException: The current request is not a multipart request 上傳文件Request processing failed;nested exception is org.springframework.web.multipart.MultipartException:Failed to parse multipart servlet request;multipart/form-data request failed.(**沒有權限**) error:org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException 成功解決 org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException:報錯 org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.jboss.resteasy.plug org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location [/tmp/tomcat.1428942566812653608 org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: 臨時上傳路徑[D:\tomcat\work\Catalina\localhost\ROOT]無效 org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: Unable to process parts as no multi-part configura Spring MVC報異常:org.springframework.web.util.NestedServletException: Request processing failed
 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM