关于java图片上传的! 实现一个功能 上传一张只是JPG 格式的图片,上传后保存到一个文件夹里面。直接存储成3种大小格式 例如:30x30 50x50 80x80 这种! 求大神解答! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 算了直接发吧! public static boolean compressPic(byte[] arr, String path, String name, File dst, int width, int height, boolean isScale) { // 图片流 InputStream bufin = new ByteArrayInputStream(arr); FileOutputStream out = null; boolean b = false; try { // 图片流 BufferedImage img = ImageIO.read(bufin); int srcWidth = img.getWidth(null); int srcHeight = img.getHeight(null); if ((srcHeight > height) || (srcWidth > width)) { // 判断是否是等比缩放 if (isScale) { double rate = Math.max((double) srcWidth / width, (double) srcHeight / height); width = (int) (srcWidth / rate); height = (int) (srcHeight / rate); } BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Image image = img.getScaledInstance(width, height, Image.SCALE_SMOOTH); tag.getGraphics().drawImage(image, 0, 0, null); out = new FileOutputStream(dst); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); out.flush(); } else { saveImage(dst, arr, path, name); } b = true; } catch (IOException e) { e.printStackTrace(); } return b; } ByteArrayOutputStream bos = new ByteArrayOutputStream(); FileInputStream fis = (FileInputStream) inputStream; int len = 0; byte[] b = new byte[BUFFER_SIZE]; while ((len = fis.read(b)) != -1) { bos.write(b, 0, len); } byte[] byteArr = bos.toByteArray(); saveImage(realImageFile, byteArr, path, realRelative); // 原图保存 compressPic(byteArr, path, relative, imageFile, 400, 300);// 大图,放大保存 compressPic(byteArr, path, smallRelative, smllImageFile, 180, 120);// 小图,缩小保存 在Eclipse中怎么运行程序? 【求助】【各种求助】 J2SE版的俄罗斯方块 求一DAO例题 大侠帮忙啊! ArrayList 二维怎么用 求高手点拨一个Java的Socket通信程序的故障原因... 有关tree中的节点问题(再线等待) 数据库连接池调用的错误 java如何能定时(比如说0.1秒)执行一个固定的方法? 有没有类似Pro*Java的东西 怎样创建同名外部类的对象? 关于JAVA反编译中,ActionListener的问题
boolean isScale) {
// 图片流
InputStream bufin = new ByteArrayInputStream(arr);
FileOutputStream out = null;
boolean b = false;
try {
// 图片流
BufferedImage img = ImageIO.read(bufin);
int srcWidth = img.getWidth(null);
int srcHeight = img.getHeight(null);
if ((srcHeight > height) || (srcWidth > width)) {
// 判断是否是等比缩放
if (isScale) {
double rate = Math.max((double) srcWidth / width, (double) srcHeight / height);
width = (int) (srcWidth / rate);
height = (int) (srcHeight / rate);
}
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Image image = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
tag.getGraphics().drawImage(image, 0, 0, null);
out = new FileOutputStream(dst);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
out.flush();
} else {
saveImage(dst, arr, path, name);
}
b = true;
} catch (IOException e) {
e.printStackTrace();
}
return b;
}
FileInputStream fis = (FileInputStream) inputStream;
int len = 0;
byte[] b = new byte[BUFFER_SIZE];
while ((len = fis.read(b)) != -1) {
bos.write(b, 0, len);
}
byte[] byteArr = bos.toByteArray();
saveImage(realImageFile, byteArr, path, realRelative); // 原图保存
compressPic(byteArr, path, relative, imageFile, 400, 300);// 大图,放大保存
compressPic(byteArr, path, smallRelative, smllImageFile, 180, 120);// 小图,缩小保存