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刪除。



猜您在找 上傳文件Request processing failed;nested exception is org.springframework.web.multipart.MultipartException:Failed to parse multipart servlet request;multipart/form-data request failed.(**沒有權限**) org.springframework.web.util.NestedServletException:Handlerdispatch failed;nested exception is java.lang.NoSuchMethodErroe; maven項目拋出org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.IncompatibleClassChangeError: Implementing class org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: 報錯異常原因及解決 spring boot 整合jwt之后報錯org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/DatatypeConverter Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Mapper method 'xxxxxxxxx' has an unsupported return type: interface java.util.List Request processing failed; nested exception is java.lang.NullPointerException HTTP Status 500 - Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) Exception occurred during processing request: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM