简单映射文件名
public String rename(String s) throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] digest = md5.digest(s.getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
for (int i=0; i< digest.length; i++) {
sb.append(Integer.toHexString(0xff & digest[i]));
}
sb.append(".jpg");
return sb.toString();
}
调整图片大小
public void resizeImg(InputStream is, OutputStream os, String formatName) throws IOException {
int width = 55;
int height = 50;
BufferedImage img = ImageIO.read(is);
BufferedImage nImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = nImg.createGraphics();
graphics.drawImage(img, 0, 0, width, height, null);
ImageIO.write(nImg, formatName, os);
is.close();
os.flush();
os.close();
}
main函数
public void static main() throws NoSuchAlgorithmException, IOException {
String spath = "D:\\";
String tpath = "E:\\";
File nPath = new File(spath);
if (nPath.isDirectory()) {
// 读取文件目录下所有文件
File[] files = nPath.listFiles();
for (int i=0; i< files.length; i++) {
String name = files[i].getName();
InputStream is = new FileInputStream(files[i]);
File file = new File(tpath + name);
OutputStream os = new FileOutputStream(file);
resizeImg(is, os, "jpg");
}
}
}