我正在使用以下代码将一张JPG格式的图片转换为DLFileEntry的PNG格式。
尽管在“文档和媒体”中更新了图像内容类型和扩展名,但我们尝试下载该图像时,仍然是JPG格式。 在文档和媒体中,图片看起来像上面的样子。您可以看到内容类型已变为
try {
DLFileEntry dlFileEntry = DLFileEntryServiceUtil.getFileEntry(dlFileEntryId);
InputStream inputStream = dlFileEntry.getContentStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1024];
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
byte[] byteArray = buffer.toByteArray();
ImageBag imageBag = ImageToolUtil.read(byteArray);
RenderedImage renderedImage = imageBag.getRenderedImage();
if (renderedImage == null) {
throw new IOException("Unable to decode image");
}
renderedImage = ImageToolUtil.scale(renderedImage, 2000);
buffer = new ByteArrayOutputStream();
ImageIO.write(renderedImage, "png", buffer);
InputStream fis = new ByteArrayInputStream(buffer.toByteArray());
DLAppServiceUtil.updateFileEntry(
dlFileEntry.getFileEntryId(),
dlFileEntry.getName(),
MediaType.IMAGE_PNG_VALUE,
dlFileEntry.getTitle(),
dlFileEntry.getDescription(),
"",
true,
fis,
buffer.size(),
serviceContext);
} catch (Exception e) {
e.printStackTrace();
}
尽管在“文档和媒体”中更新了图像内容类型和扩展名,但我们尝试下载该图像时,仍然是JPG格式。 在文档和媒体中,图片看起来像上面的样子。您可以看到内容类型已变为
image/png
。
上面显示了我尝试下载并保存这张图片的截图。当我尝试下载时,它仍然是JPG的原始格式。除了上面的代码,我还需要做什么才能完全将图片转换为PNG格式?