word转pdf:基于spire free免费版 可以进行word转pdf word转图片 免费版仅限于三页 商业还是老老实实买jar包,不商用可搜索aspose spire.doc等jar包进行学习。
public String wordToPdf(String path) throws IOException {
// 加载word示例文档
Document document = new Document();
document.loadFromFile(path);
// 保存结果文件
document.saveToFile("D:\\test3.pdf");
return "转换成功";
}
PDF转图片:
public void pdfFileToImages(String filePath, String cachePath) {
try {
PDDocument pd = PDDocument.load(new File(filePath));
PDFRenderer pdfRenderer = new PDFRenderer(pd);
for (int page = 0; page < pd.getNumberOfPages(); ++page) {
BufferedImage image = pdfRenderer.renderImageWithDPI(page, 96, ImageType.RGB);
//将图片数据保存为PNG格式文档
File file= new File(cachePath + File.separator + page + ".png");
ImageIO.write(image, "png", file);
System.out.println(file.getAbsolutePath());
}
} catch (Exception e) {
e.printStackTrace();
}
}
bufferedImage转MultipartFile word和pdf转图片经常需要:
public MultipartFile bufferedImageToMultipartFile(BufferedImage bufferedImage, String fileName) throws IOException {
//创建一个ByteArrayOutputStream
ByteArrayOutputStream os = new ByteArrayOutputStream();
//把BufferedImage写入ByteArrayOutputStream
ImageIO.write(bufferedImage, "png", os);
//ByteArrayOutputStream转成InputStream
InputStream input = new ByteArrayInputStream(os.toByteArray());
//InputStream转成MultipartFile
MultipartFile multipartFile = new MockMultipartFile(fileName, fileName + ".png", "text/plain", input);
return multipartFile;
}
outputStream转inputStream
public ByteArrayInputStream parse(OutputStream out) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos = (ByteArrayOutputStream) out;
ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
return swapStream;
}
file转byte[]:
public static byte[] filetobyte(File file) {
byte[] fileBytes = new byte[0];
try {
FileInputStream fis = new FileInputStream(file);
// 强转成int类型大小的数组
fileBytes = new byte[(int) file.length()];
// 将pdf内容放到数组当中
fis.read(fileBytes);
// 关闭文件流
fis.close();
// System.out.println(Arrays.toString(fileBytes));
} catch (
IOException e) {
e.printStackTrace();
}
return fileBytes;
}
MultipartFile转file
public static File MultipartFileToFile(MultipartFile mFile) throws IOException {
File file = new File(mFile.getOriginalFilename());
FileUtils.copyInputStreamToFile(mFile.getInputStream(), file);
//这种方法会在本地产生临时文件,用完后需要删除
//if (file.exists()) {
// file.delete();
//}
return file;
}
MultipartFilez转base64
public static String MultipartFileToBase64(MultipartFile file) throws Exception {
BASE64Encoder base64Encoder = new BASE64Encoder();
String base64EncoderImg = file.getOriginalFilename() + "," + base64Encoder.encode(file.getBytes());
return base64EncoderImg;
}
String转base64
public static String StringToBase64(String token) {
byte[] bytes = token.getBytes();
Base64.Encoder encoder = Base64.getEncoder();
String encodeToken = encoder.encodeToString(bytes);
return encodeToken;
}
file转base64
public static String fileToBase64(File file) {
String base64 = null;
InputStream in = null;
try {
in = new FileInputStream(file);
byte[] bytes = new byte[(int) file.length()];
in.read(bytes);
base64 = Base64.getEncoder().encodeToString(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println(base64);
return base64;
}
inputstream转multipartFile
MultipartFile multipartFile = new MockMultipartFile(fileName, fileName + ".docx", "text/plain", inputStream);
base64转inputstream
String base64FileData = "";
BASE64Decoder decoder = new BASE64Decoder();
// Base64解码
byte[] byteArr = decoder.decodeBuffer(base64FileData);
InputStream inputStream = new ByteArrayInputStream(byteArr);