gpt4 book ai didi

javax.imageio.IIOException : Can't read input file in Servlet

转载 作者:行者123 更新时间:2023-12-02 11:52:56 25 4
gpt4 key购买 nike

我正在比较存储在项目内 pic 文件夹中的两个图像

在编译期间我收到IOException我尝试了以前的所有解决方案,但仍然不起作用请帮我解决这个错误在此项目中,我使用 servlet 页面

这是我的 Servlet 方法

 BufferedImage imgA = null;
BufferedImage imgB = null;
String Filepath = new String();

File fileA = new File("pic/image2.jpg");
File fileB = new File("pic/image2.jpg");

for(int i=0 ;i<list.size();i++)
{

Filepath = list.get(i).getImagepath();
try
{

imgA = ImageIO.read(fileA);
imgB = ImageIO.read(fileB);
}
catch (IOException e)
{
System.out.println(e);
}


int width1 = imgA.getWidth();
int width2 = imgB.getWidth();
int height1 = imgA.getHeight();
int height2 = imgB.getHeight();

if ((width1 != width2) || (height1 != height2))
System.out.println("Error: Images dimensions"+
" mismatch");
else
{
long difference = 0;
for (int y = 0; y < height1; y++)
{
for (int x = 0; x < width1; x++)
{
int rgbA = imgA.getRGB(x, y);
int rgbB = imgB.getRGB(x, y);
int redA = (rgbA >> 16) & 0xff;
int greenA = (rgbA >> 8) & 0xff;
int blueA = (rgbA) & 0xff;
int redB = (rgbB >> 16) & 0xff;
int greenB = (rgbB >> 8) & 0xff;
int blueB = (rgbB) & 0xff;
difference += Math.abs(redA - redB);
difference += Math.abs(greenA - greenB);
difference += Math.abs(blueA - blueB);
}
}

double total_pixels = width1 * height1 * 3;

double avg_different_pixels = difference / total_pixels;

double percentage = (avg_different_pixels / 255) * 100;

System.out.println("Difference Percentage-->" +
percentage);

}
}

错误控制台给出

javax.imageio.IIOException: Can't read input file!
Dec 12, 2017 12:26:30 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [services.CompareImage] in context with path [/JavaCvServlet] threw exception

最佳答案

您应该尝试获取如下文件

File fileA = new File(getServletContext().getRealPath("pic/image2.jpg"));
File fileB = new File(getServletContext().getRealPath("pic/image2.jpg"));

关于javax.imageio.IIOException : Can't read input file in Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47767149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com