This is my Service method
public void CapchaString() {
String capchaString = UUID.randomUUID().toString();
// ByteArrayInputStream bais = new ByteArrayInputStream(capchaString.getBytes());
try {
BufferedImage bi=new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.drawString(capchaString, 10, 10);
/* File outputfile = new File("src/main/resources/output.png");*/
ImageOutputStream out = new FileImageOutputStream(new File("C:/Users/Saurabh/Documents/Agile maple code/JavaJ2eeTraining/src/main/resources/output.png"));
ImageIO.write(bi, "png", out);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
This is my controller method
@ResponseBody
@RequestMapping(value="/photo2", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
public byte[] testphoto() throws IOException {
capchaService.CapchaString();
Resource resource = loader.getResource("output.png");
ImageIO.createImageInputStream(resource.getFile());
InputStream is = new BufferedInputStream(new FileInputStream(resource.getFile()));
return IOUtils.toByteArray(is);
}
and when i use my server i am getting HTTP Status 500 - class path resource [output.png] cannot be resolved to URL because it does not exist
org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:177) org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:48) com.agilemaple.common.controller.HelloController.testphoto(HelloController.java:174) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:606) org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)