0
votes

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)

1

1 Answers

0
votes

You really need to understand how a webapp works:

  1. You setup a development environment and create sources. src/main/resources is part of this development environment.
  2. you compile the sources and assemble them into a war file or exploded war directory
  3. You deploy the war file to a server, which loads the classes and resources from the war file. Not from your development environment.

In production, all you have is the server, where the war file is deployed. There is no development environment on the server. The production server could be a linux server where paths like C:/Users/Saurabh/ don't even make sense. Even if it's a windows machine, this directory doesn't exist on it. And even if it exists, the webapp doesn't load resources from there, but from the deployed war file, which is read-only.

So the above code doesn't make any sense. If you generate an image in the service, then return the bytes of the image from the method instead of trying to write it to a file and then reload the bytes from the file.