Vorrei realizzare una servlet che crei una X su un’immagine. Se uso questo codice:
codice:
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
@WebServlet(name = "ImageServlet", urlPatterns="/image")
public class ImageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {BufferedImage bi = new BufferedImage(300,300, BufferedImage.TYPE_INT_ARGB);Graphics2D g2 = bi.createGraphics();
        g2.setPaint(Color.BLUE);g2.drawLine(10,10,bi.getWidth()-10,bi.getHeight()-10);
        g2.drawLine(10,bi.getHeight()-10,bi.getWidth()-10,10);g2.dispose();
        response.setContentType("image/png");
        ImageIO.write(bi,"PNG",response.getOutputStream());
    }
}
ottengo questo errore:
HTTP Status 500 – Internal Server Error


Type Exception Report
Message Can't create cache file!
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
javax.imageio.IIOException: Can't create cache file!
java.desktop/javax.imageio.ImageIO.createImageOutputStream(Imag eIO.java:423)
java.desktop/javax.imageio.ImageIO.write(ImageIO.java:1589)
ImageServlet.doGet(ImageServlet.java:30)
javax.servlet.http.HttpServlet.service(HttpServlet .java:634)
javax.servlet.http.HttpServlet.service(HttpServlet .java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilt er(WsFilter.java:53)
Root Cause
java.nio.file.AccessDeniedException: C:\Program Files\Java\apache-tomcat-9.0.10\temp\imageio12188076398689945206.tmp
java.base/sun.nio.fs.WindowsException.translateToIOException (WindowsException.java:89)
java.base/sun.nio.fs.WindowsException.rethrowAsIOException(W indowsException.java:103)
java.base/sun.nio.fs.WindowsException.rethrowAsIOException(W indowsException.java:108)
java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChanne l(WindowsFileSystemProvider.java:231)
java.base/java.nio.file.Files.newByteChannel(Files.java:369)
java.base/java.nio.file.Files.createFile(Files.java:640)
java.base/java.nio.file.TempFileHelper.create(TempFileHelper .java:137)
java.base/java.nio.file.TempFileHelper.createTempFile(TempFi leHelper.java:160)
java.base/java.nio.file.Files.createTempFile(Files.java:905)
java.desktop/javax.imageio.stream.FileCacheImageOutputStream.<i nit>(FileCacheImageOutputStream.java:88)
java.desktop/com.sun.imageio.spi.OutputStreamImageOutputStreamS pi.createOutputStreamInstance(OutputStreamImageOut putStreamSpi.java:68)
java.desktop/javax.imageio.ImageIO.createImageOutputStream(Imag eIO.java:419)
java.desktop/javax.imageio.ImageIO.write(ImageIO.java:1589)
ImageServlet.doGet(ImageServlet.java:30)
javax.servlet.http.HttpServlet.service(HttpServlet .java:634)
javax.servlet.http.HttpServlet.service(HttpServlet .java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilt er(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.

Apache Tomcat/9.0.10
Lo script dovrebbe essere corretto, non capisco perché su IntelliJ ottengo questo errore.