public static void dwgToPdf (String uuid, String fileName, String fileExtension) throws IOException
{
// 根据uuid得到文件在服务器的存储路径
String zipFilePath = StringUtils.format(“{}/{}”, getUploadPath(), uuid);
// 如果文件不存在抛出异常
File file = new File(zipFilePath);
if (!file.exists())
{
throw new FileNotFoundException("文件在服务器上不存在!");
}
try(ZipFile zipFile = new ZipFile(zipFilePath))
{
List<FileHeader> headers = zipFile.getFileHeaders();
if(headers.stream().noneMatch(x -> x.getFileName().toLowerCase().endsWith(".dwg")))
{
throw new FileNotFoundException("文件异常!");
}
if(headers.stream().anyMatch(x -> x.getFileName().toLowerCase().endsWith(".pdf")))
{
zipFile.removeFile(StringUtils.format("{}{}",fileName,".pdf"));
}
try(
net.lingala.zip4j.io.inputstream.ZipInputStream zis = zipFile.getInputStream(zipFile.getFileHeader(StringUtils.format("{}{}",fileName,fileExtension)));
BufferedInputStream bis = new BufferedInputStream(zis);
ByteArrayOutputStream os = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
)
{
LoadOptions opts = new LoadOptions();
opts.setSpecifiedEncoding(CodePages.Utf8);
Image image = Image.load(bis, opts);
CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
cadRasterizationOptions.setBackgroundColor(Color.getWhite());
cadRasterizationOptions.setScaleMethod(ScaleType.GrowToFit);
// cadRasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
image.save(bos, pdfOptions);
bos.flush();
ZipParameters parameters = new ZipParameters();
parameters.setFileNameInZip(StringUtils.format("{}{}",fileName,".pdf"));
try(
InputStream is = new ByteArrayInputStream(os.toByteArray());
BufferedInputStream bis2 = new BufferedInputStream(is)
)
{
zipFile.addStream(bis2, parameters);
}
catch (Exception e)
{
throw e;
}
}
catch (Exception e)
{
throw e;
}
}
catch (Exception e)
{
throw e;
}
}
问题文件.zip (2.8 MB)