ImageIO.write 在本地生成图片成功 在服务器上未生成图片
public static void mergeImage( String path1, String path2, int type, String targetFile) throws IOException {
System.err.println("生成路径=>"+targetFile);
File file1 = new File(path1);
File file2 = new File(path2);
if(!file1.exists()) {
System.err.println("路径1文件不存在=>"+path1);
return;
}
if(!file2.exists()) {
System.err.println("路径2文件不存在=>"+path2);
return;
}
//两张图片的拼接
int len = 2;
BufferedImage[] images = new BufferedImage[len];
images[0] = ImageIO.read(file1);
images[1] = ImageIO.read(file2);
int[][] ImageArrays = new int[len][];
for (int i = 0; i < len; i++) {
int width = images[i].getWidth();
int height = images[i].getHeight();
ImageArrays[i] = new int[width * height];
ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width);
}
int newHeight = 0;
int newWidth = 0;
for (int i = 0; i < images.length; i++) {
// 横向
if (type == 1) {
newHeight = newHeight > images[i].getHeight() ? newHeight : images[i].getHeight();
newWidth += images[i].getWidth();
} else if (type == 2) {// 纵向
newWidth = newWidth > images[i].getWidth() ? newWidth : images[i].getWidth();
newHeight += images[i].getHeight();
}
}
if (type == 1 && newWidth < 1) {
return;
}
if (type == 2 && newHeight < 1) {
return;
}
// 生成新图片
try {
BufferedImage ImageNew = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
int height_i = 0;
int width_i = 0;
for (int i = 0; i < images.length; i++) {
if (type == 1) {
ImageNew.setRGB(width_i, 0, images[i].getWidth(), newHeight, ImageArrays[i], 0,
images[i].getWidth());
width_i += images[i].getWidth();
} else if (type == 2) {
ImageNew.setRGB(0, height_i, newWidth, images[i].getHeight(), ImageArrays[i], 0, newWidth);
height_i += images[i].getHeight();
}
}
//输出想要的图片
ImageIO.write(ImageNew, targetFile.split("\\.")[1], new File(targetFile));
System.err.println("拼接图片成功!");
} catch (Exception e) {
System.err.println("拼接图片失败:{"+ e+"}");
}
}
文件路径=>d:\lysoft\Tomcat7.0\webapps\ROOT\module\dzqm
0--1
生成路径=>d:\lysoft\Tomcat7.0\webapps\ROOT\module\dzqm\81_30.png
拼接图片成功!
0--2
生成路径=>d:\lysoft\Tomcat7.0\webapps\ROOT\module\dzqm\81_30.png
路径1文件不存在=>d:\lysoft\Tomcat7.0\webapps\ROOT\module\dzqm\81_30.png
重新试了一下 生成到d盘根目录下可以 tomcat文件夹中不可以