報錯更改 Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] wit


 

 

嚴重: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException

 

使用input上傳文件時候出錯,因為在form里提交了圖文件,需要進行二進制編碼,而我卻少了這部分,即enctype="multipart/form-data"。更改后無錯。

 

表單中enctype="multipart/form-data"的意思,是設置表單的MIME編碼。

默認情況,這個編碼格式是application/x-www-form-urlencoded,不能用於文件上傳;

只有使用了multipart/form-data,才能完整的傳遞文件數據,進行下面的操作。

enctype="multipart/form-data"是上傳二進制數據,form里面的input的值以2進制的方式傳過去。

 

核心代碼如下:

 

jsp:

<form action="ManagerUpdate" onsubmit="return checkPwd()" enctype="multipart/form-data" method="post" >
                    <div>
                        <input type="hidden" value="${manager.id}" name="id">
                    </div>
                    <div class="sign-u" style="margin-bottom: 20px">
                        <span>
                            <img src="<%=path %>/upload/${manager.pic}" onerror="this.src='<%=path %>/upload/manager.png'" style="width: 20%;height: 20%;margin-left: 15%;border: 2px solid black">
                        </span>
                        <label><input type="file" name="file" id="file"  style="display: none" autocomplete="off"  /><strong style="color: #01AAED;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;上傳頭像</strong></label>
                        <div class="clearfix"> </div>
                    </div>
 </form>

 

Controller:

 @RequestMapping(value = "ManagerUpdate")
    public String ManagerUpdate(Manager manager,MultipartFile file,HttpServletRequest request) throws IOException {
        //處理流業務
        String path = request.getServletContext().getRealPath("/upload");//虛擬路徑
        File dir = new File(path);//把虛擬路徑存儲到流中,
        dir.mkdirs();//查詢是否存在此文件夾,如果存在無操作,如果不存在這里自動創建;
        String srcName = file.getOriginalFilename();//獲取流中的文件名稱
        File dest = new File(dir, srcName);//spring自動封裝的流上傳函數
        file.transferTo(dest);//文件上傳
        manager.setPic(srcName);


        boolean b = managerservice.updateManager(manager);
        request.setAttribute("b",b);
        return "redirect:ManagerShow";
    }

 

學習鏈接:https://www.iteye.com/blog/zkliqiang-779285

 


免責聲明!

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



猜您在找 Servlet.service() for servlet [dispatcherServlet] in context with path [/heima_ssm_web] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed;springboot無法注入,注入后無效 錯誤:嚴重: Servlet.service() for servlet [appServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is Request processing failed; nested exception is java.lang.NullPointerException Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception 異常 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 Servlet.service() for servlet [jsp] in context with path [/Healthy_manager] threw exception [Unable to compile class for JSP] with root cause java.lang.IllegalArgumentException: Page directive: inval ?--Porg.springframework.beans.MethodInvocationException: Property 'username' threw exception; nested exception is java.lang.NullPointerException 報錯:嚴重: Servlet.service() for servlet [jsp] in context with path [/20161116-Struts2-6] threw exception [/index.jsp (line: 13, column: 20) No tag "textfiled" defined in tag library imported with prefix
 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM