Java实现 pdf 转 图片:
(1)获取pdf文件输入流
(2)获取文件路径,带文件名:设置新的文件名(后缀名为图片,如 .png)
(3)将pdf文件输入流和文件路径作为参数,调用 服务器上传文件接口
(4)返回图片服务器地址
public static String uploadByPath (String pdfUrl, String path, Long groupId) {
//上传文件到OSS,返回文件访问路径
String url = null;
try {
// 通过URL获得PDF文件输入流内容:当作一个URL来装载文件
URL url1 = new URL(pdfUrl);
URLConnection con = url1.openConnection();
con.setConnectTimeout(3 * 1000);
InputStream inputStream = con.getInputStream();
//获取文件路径,带文件名(生成一个随机字符串)
String uuid = UUID.randomUUID().toString().replace("-", "");
String fileName = path + uuid + "." + "png";
//上传文件到OSS,返回文件访问路径
url = OSSFactory.build(groupId).upload(inputStream, fileName);
} catch (IOException e) {
e.printStackTrace();
}
return url;
}
二、OSS文件上传工具类
public String upload(InputStream inputStream, String path) {
OSSClient client = new OSSClient(config.getAliyunEndPoint(), config.getAliyunAccessKeyId(),
config.getAliyunAccessKeySecret());
try {
client.putObject(config.getAliyunBucketName(), path, inputStream);
client.shutdown();
} catch (Exception e){
throw new RenException(ErrorCode.OSS_UPLOAD_FILE_ERROR, e, "");
}
return "/" + path;
}



