我要在表中插入数据,该表还包含图像文件。我从AJAX发送的json数据是:
通过在javascript中将文件类型更改为base64来发送数据。
我的java类的一些实体是:
Letter.java
public class Letter {
private BigDecimal letterNo;
private BigDecimal letterId;
private String inout;
private String inoutNo;
private String inoutDate;
private String letterIssuedSubBy;
private String letterFile;
private String representativeName;
private String representativeNameEng;
private BigDecimal selectionId;
private BigDecimal assessmentNo;
private String entryBy;
private String rStatus;
private byte[] imageFile;
private String imageTitle;
//i ommited getters and setters
}
ProcessAssessmenttTwo.java是我的Dto类
public class ProcessAnexTwo {
private String entryBy;
private String rStatus;
private BigDecimal selectionId;
private String inout;
private String letterReceivedBy;
private String letterIssuedSubBy;
private String representativeNameEng;
private String inoutDate;
private BigDecimal letterId;
private BigDecimal assessmentNo;
private String imageFiles;
private byte[] imageFile;
private String imageTitle;
}
数据已成功到达api,但是在插入数据库时却显示错误:
@PostMapping("saveProcessAnexTwo")
public ResponseEntity saveAnnexTwo(@RequestBody ProcessAnexTwo processAnexTwo,Model model) {
System.out.println("entered in saveProcessAnex Two func");
String imgByte=processAnexTwo.getImageFiles();
System.out.println(imgByte);
byte[] nnn=imgByte.getBytes();
byte[] imageFile= Base64.getEncoder().encode(nnn);
System.out.println(processAnexTwo.getImageTitle());
Letter letter = new Letter(processAnexTwo.getLetterId(), processAnexTwo.getInout(),
processAnexTwo.getInoutDate(),processAnexTwo.getLetterIssuedSubBy(),null,processAnexTwo.getRepresentativeNameEng(),
null,processAnexTwo.getSelectionId(),processAnexTwo.getAssessmentNo(), "PCS",
null,imageFile,null);
BigDecimal letterNo = letterService.insertLetterAnnexTwo(letter);
return null;
}
要插入的LetterDaoImpl类为:
@Override
public BigDecimal saveLetterAnnexTwo(Letter letter) {
try {
System.out.println("hitted123 here");
SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate).withProcedureName("PCPR_ADD_LETTER");
Map<String, Object> inParamMap = new HashMap<String, Object>();
System.out.println(letter.getLetterId());
inParamMap.put("P_LETTER_NO",null);
inParamMap.put("P_LETTER_ID",letter.getLetterId());
inParamMap.put("P_INOUT",letter.getInout());
inParamMap.put("P_INOUT_NO",null);
inParamMap.put("P_INOUT_DATE",letter.getInoutDate());
inParamMap.put("P_LETTER_ISSUED_SUB_BY",letter.getLetterIssuedSubBy());
inParamMap.put("P_LETTER_FILE",letter.getLetterFile());
inParamMap.put("P_REPRESENTATIVE_NAME",letter.getRepresentativeName());
inParamMap.put("P_REPRESENTATIVE_NAME_ENG",letter.getRepresentativeNameEng());
inParamMap.put("P_SELECTION_ID",letter.getSelectionId());
inParamMap.put("P_ASSESSMENT_NO",0);
inParamMap.put("P_ENTRY_BY", letter.getEntryBy());
inParamMap.put("P_R_STATUS",null);
inParamMap.put("P_IMAGE_FILE",letter.getImageFile());
inParamMap.put("P_IMAGE_TITLE",letter.getImageTitle());
SqlParameterSource in = new MapSqlParameterSource(inParamMap);
BigDecimal letterNO = (BigDecimal) simpleJdbcCall.execute(in).get("P_LETTER_NO");
return letterNO;
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
我遇到的错误是:
严重:路径为[]的上下文中的servlet [dispatcher]的Servlet.service()抛出异常[请求处理失败;嵌套异常是java.lang.NullPointerException],其根本原因是 java.lang.NullPointerException 在java.util.Base64 $ Decoder.decode(Base64.java:525) 在com.pcs.spring.controller.api.FormApiController.saveAnnexTwo(FormApiController.java:222) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:498) 在org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) 在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) 在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116) 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) 在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) 在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) 在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) 在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) 在org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:647) 在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) 在org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:603) 在org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:312) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624) 在java.lang.Thread.run(Thread.java:748)