그때, BYTEARRAY에 이미지를 변환 다음 코드로의 HttpServlet 클래스에서의 BufferedOutputStream를 사용하여 내 JSP 페이지에 인쇄하려고 :색상을 유지 BYTEARRAY에 이미지를 변환
public byte[] extractBytes(String imagePath) {
byte[] imageInByte = new byte[0];
try {
ByteArrayOutputStream baos = null;
BufferedImage originalImage = ImageIO.read(new File(imagePath));
baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "jpg", baos);
baos.flush();
imageInByte = baos.toByteArray();
} catch (Exception ex) {
ex.printStackTrace();
}
return imageInByte;
}
그것을 인쇄하려면 :
imageInByte = extractBytes(requestedUrl);
response.setContentType(
"image/jpeg");
response.setContentLength(imageInByte.length);
response.setHeader(
"Content-Disposition", "inline; filename=\"" + name
+ "\"");
BufferedInputStream input = null;
BufferedOutputStream output = null;
time = System.currentTimeMillis();
input = new BufferedInputStream(new ByteArrayInputStream(imageInByte));
output = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[8192];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
그러나 결과 이미지의 색상이 손실됩니다.
문제는 BYTEARRAY 변환 부분에있는 것 같아요. 이 문제를 어떻게 해결할 수 있습니까?
이미지를 인코딩하기 전에 이미지를 디코딩해야합니까 (예 : 이미지에서 이미지 처리를 수행 할 계획입니까, 아니면 JPEG로 변환해야합니까?)? 그렇지 않다면 파일에서 서블릿 인'OutputStream'으로 바이트를 복사하는 것만으로 쉽게 피할 수 있습니다. 보너스로 훨씬 빨라졌습니다. – haraldK