2、实现代码如下所示:
public static void main(String[] args) {
String fileUrl = "https://p6-dcd-sign.byteimg.com/tos-cn-p-0015/o4DKIDYbmsfElgA99allnDeqWd7nxqgAAUKhWB~tplv-f042mdwyw7-original:640:0.heif?lk3s=23734399&x-expires=1706950357&x-signature=T2%2FlQj2iMr%2FDkitPbf6kTqrtcuw%3D";
String destinationPath = "C:\\Users\\Administrator\\Desktop\\1.heic";
String jpgFilePath = "C:\\Users\\Administrator\\Desktop\\file.jpg";
try {
// 创建URL对象
URL url = new URL(fileUrl);
// 打开连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 获取输入流
InputStream inputStream = connection.getInputStream();
// 创建输出流
FileOutputStream outputStream = new FileOutputStream(destinationPath);
// 创建缓冲区
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
// 读取数据并写入文件
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
// 关闭流
bufferedInputStream.close();
outputStream.close();
connection.disconnect();
System.out.println("文件下载完成!");
} catch (IOException e) {
e.printStackTrace();
}
try {
// 构建命令行参数
String[] command = {
"C:\\Program Files\\ImageMagick-7.1.1-Q16\\magick.exe",
destinationPath,
jpgFilePath
};
// 创建进程并执行命令
ProcessBuilder pb = new ProcessBuilder(command);
Process process = pb.start();
// 等待进程执行完毕
int exitCode = process.waitFor();
if (exitCode == 0) {
System.out.println("Conversion completed successfully.");
} else {
System.out.println("Conversion failed with error code: " + exitCode);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}