本地测试代码:
public static void main(String[] args) throws IOException, AWTException { //test jpg to webp String filePath = "e:\\1.png"; String outPath = "e:\\1.jpg.webp"; // FilenameUtils.getFullPath(outPath); // if (!new File(outPath).exists()) { // new File(outPath).mkdirs(); // } // init cwebp path,and set privilege of 755. // you can replace cwebpath in your case. in this case, we used a macos-based cwebp String cwebpPath = "E:\\libwebp-0.4.1-rc1-windows-x86\\bin\\cwebp"; try { String chmodCommand = "" + cwebpPath; Runtime.getRuntime().exec(chmodCommand).waitFor(); StringBuilder command = new StringBuilder(cwebpPath); command.append(" -q " + 75); command.append(" " + filePath); command.append(" -o " + outPath); Runtime.getRuntime().exec(command.toString()); } catch (Exception e) { e.printStackTrace(); // log.error("An error happend when convert to webp. Img is: " + inputFile, e); } }