How to convert an <img... in html to byte [] in Java - java

I have opened a webpage in HtmlUnit headless browser. Now that webpage contains a image html tag as follows:
<img src="..." />
So I want that image only. But the problem is that the same src URL of the image shows diff. image each time. Means, if we refresh the img src URL, then it shows diff. image each time.
So how to get the image that is displayed on the html page.

When you get the HTMLPage, you have to get the image through one of its method. You can then get an HtmlImage, which can be saved as a file. You'll just have to analyse this file later.

This is the function to store your image with fully qualified I
protected String saveImage(String imageUrl) throws Exception {
InputStream inputStream;
OutputStream os;
ByteArrayOutputStream byteArrayOutputStream;
String destinationFile = "File path where you want ot store the image";
URL url = new URL(imageUrl);
inputStream = url.openStream();
byteArrayOutputStream = new ByteArrayOutputStream();
os = new FileOutputStream(destinationFile);
int read;
String barcode = null;
while ((read = inputStream.read()) != -1) {
os.write(read);
byteArrayOutputStream.write(read);
barcode = byteArrayOutputStream.toString();
}
inputStream.close();
os.close();
byteArrayOutputStream.close();
return barcode;
}

Related

Can't open image after decoding and writing to disk

I am uploading a jpg to a spring controller endpoint. The image is uploaded as Base64 image/jpg which comes in as a MultipartFile. I am decoding the inputstream using Base64Decoder which seems to decode it ok but when I turn it into an InputStream to write it out to disk I can see it's been modified (according to what I can see in the debugger). When I save the file and open it it says it's an unsupported file type.
I took the multipart inputstream and wrote it directly to disk and I see the base64 encoding in notepad.
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/4QBgRXhpZgAASUkqAAgAAAACADEBAgAHAAAAJgAAAGmHBAABAAAALgAAAAAAAABHb29nbGUAAAMAAJAHAAQAAAAwMjIwAqAEAAEAAAAACAAAA6AEAAEAAAAABgAAAAAAAP/bAIQAAwICCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCggKCgoKCgoICAgKCgoICAgICgoICAgKCgoICA0NCggNCAgKCAEDBAQGBQYIBgYICA0ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgI/8AAEQgGAAgAAwEiAAIRAQMRAf/EAB0AAAIDAQEBAQEAAAAAAAAAAAMEAgUGAQAHCAn/xABJEAABAwIDBgQFBAIBAwMCAA8BAAIRAyEEMUEFElFhcfAGgZGhEyKxwdEHMuHxFEJSFSNiCDNyFoKSFyRDU6LCCTRjRHOy0uL/xAAbAQEBAQEBAQEBAAAAAAAAAAAAAQIDBAUGB//EACoRAQEBAQACAwADAAEEAwEBAQABEQIDcRIhMQQTsVEUIjJBBWGBwUIj/9oADAMBAAIRAxEAPwCgrFsRw4ZfWEs2FXUN4C7p4c10702ML4/M+p6jhzfqep/i4p0uS9Vtw8vddwRkX7t+ck1TdElWusVNVsRY3K1ngx2YFsz5rMYytJHX+7rSeDWGf6XHqf8A0938eZ02zK8LlWoMzwP9LwCV2jxC81j7kswTAYsE2Xto1OAzVPslvzHT8K8LV
Here's my controller and my code:
#PostMapping(value = "/saveBlueprintOrder")
public ResponseEntity<?> saveBlueprintOrder(#RequestParam MultipartFile blueprint,
#RequestParam(required = false) MultipartFile coversheet,
#RequestParam(required = false) MultipartFile logo,
#ModelAttribute BlueprintOrder blueprintOrder) {
if(coversheet != null) {
BASE64Decoder decoder1 = new BASE64Decoder();
byte[] imageBytes = decoder1.decodeBuffer(coversheet.getInputStream());
InputStream bis = new ByteArrayInputStream(imageBytes);
BufferedImage image = ImageIO.read(bis);
ImageIO.write(image, "jpg", new File("C:\\Users\\i58287\\Downloads\\coversheet.jpg"));
OutputStream stream = new FileOutputStream("C:\\Users\\i58287\\Downloads\\coversheet-test.jpg");
stream.write(imageBytes);
stream.close();
I just need to be able to translate this image to an inputstream so I can check the image locally in addition I need to send it to another api as such. What am I missing that's causing this image to be un-openable? Thanks for any help!
PS: I've done a lot of combinations so this is showing a couple options I have tried, BufferedImage image = ImageIO.read(bis) keeps returning a null image.
Ok so I don't know WHY this is what I had to do but I ended up saving my byte[] as a String and cut off the pre-pended
data:image/jpeg;base64
Then I decoded it into an InputStream. Anyone know why I had to do this?
Here's the code:
String imageBytes = new String(coversheet.getBytes(), StandardCharsets.UTF_8);
String imageDataBytes = imageBytes.substring(imageBytes.indexOf(",") + 1);
InputStream stream = new ByteArrayInputStream(Base64.getDecoder().decode(imageDataBytes.getBytes()));
BufferedImage image = ImageIO.read(stream);
ImageIO.write(image, "jpg", new File("C:\\Users\\i58287\\Downloads\\coversheet-test.jpg"));

Liferay DLFileEntry image conversion is not fully converting the image

I am converting an image as a DLFileEntry from JPG to PNG format using the following code.
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();
}
Even though it updates the image content type and extension in "Documents and Media", when we try downloading the image, it is still in JPG format.
The image looks like above in Documents and Media. You can see that the content type has become image/png.
Above shows the screenshot while I tried to Download this image and save it. It is still in the original format of JPG when I try downloading. What should I do in addition to the code above, inorder to completely convert the image to PNG?
You still store the old file name: dlFileEntry.getName()
I would guess that jpeg or jpg is the extension of the old file name and the browser determines his file filter from the extension.
So better exchange the extension as well:
DLAppServiceUtil.updateFileEntry(
dlFileEntry.getFileEntryId(),
dlFileEntry.getName().replaceAll("\\..*?$",".png"),
...
This will change the extension that is stored for that file

How to print HTML to PDF

I Want to print html as a pdf. Here is my code. It was not throwing any error but when i open pdf it is showing error
Here is my java code
URL u = new URL("http://localhost/printPdf.Html");
URLConnection uc = u.openConnection();
BufferedInputStream is = new BufferedInputStream(uc.getInputStream());
File outs = new File("D:/HtmlToPdf.pdf")
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(outs));
byte[] b = new byte[8 * 1024];
int read = 0;
while ((read = is.read(b)) > -1) {
bout.write(b, 0, read);
}
bout.flush();
bout.close();
is.close();
Here is my html file
<html>
<head></head>
<body><div>Hi !!! Example PDF </div></body>
</html>
When i open pdf i am getting this error
Adobe Reader could not open because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).
You cannot simply write one file format to another. You need to write the HTML in a way according to the PDF specification.
Here is a PDF library you can use: http://pdfbox.apache.org/

view flv video by servletvideo java

When I try to view a video in the jwplayer from a file in the webcontent directory it shows up and I can play it, but when I read the same file from a database and respond with an flv via servlet it doesn't show up. Can any one help me?
In Html file :
<script type='text/javascript' src='/ThoughRecord18-8/jwplayer.js'></script>
<script type='text/javascript'>
jwplayer('mediaspace').setup({
'flashplayer': '/ThoughRecord18-8/player.swf',
'file': '/ThoughRecord18-8/videoss?videoId=1',
'controlbar': 'bottom',
'width': '470',
'height': '320'
});
</script>
and the servlet is
String videoId = request.getParameter("videoId");
if (videoId != null || !videoId.equals("")) {
VideoDao dao = new VideoDao();
Video video = dao.getVideo(videoId);
Blob blob = video.getVideoBlob();
byte[] buf = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream in = null;
int len;
try {
len = (int) blob.length();
byte[] rb = new byte[len];
InputStream readImg = blob.getBinaryStream();
int index = readImg.read(rb, 0, len);
...
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType("video/x-flv");
response.setContentLength(rb.length);
response.setHeader("Content-Disposition", "inline; filename=file.flv");
byte[] content = new byte[DEFAULT_BUFFER_SIZE];
BufferedInputStream is = new BufferedInputStream(
new ByteArrayInputStream(rb));
OutputStream os = response.getOutputStream();
while (is.read(content) != -1) {
os.write(content);
}
is.close();
os.close();**
This isn't a java issue, JW Player only supports HTTP Psuedo Streaming and RTMP Streaming. They're both their own protocols - you can't just stream the pure content at it. Take a look at this page: http://www.longtailvideo.com/support/jw-player/jw-player-for-flash-v5/12534/video-delivery-http-pseudo-streaming, and this page: http://www.longtailvideo.com/support/jw-player/jw-player-for-flash-v5/12535/video-delivery-rtmp-streaming for info on how JW Player does streaming.
If you don't want the user to have to wait to get all of the content, you'll need to go with one of those streaming mechanisms. If that's not an issue, you could consider changing your servlet to write the file somewhere in your webcontent directory and then do a redirect to the file or something, but I don't think writing to the response stream like that is going to do the trick.

Downloadable pdf in JasperReports

I am working with JasperReprots. This is part of my code:
ServletContext context = this.getServletConfig().getServletContext();
File reportF = new File(context.getRealPath(rF));
byte[] bytes = null;
ServletOutputStream servletOutputStream = resp.getOutputStream();
InputStream reportStream = new FileInputStream(reportF.getPath());
reportF.delete();
bytes = JasperRunManager.runReportToPdf(reportStream, new HashMap(),new JREmptyDataSource());
resp.setContentType("application/pdf");
resp.setContentLength(bytes.length);
servletOutputStream.write(bytes, 0, bytes.length);
servletOutputStream.flush();
servletOutputStream.close();
After this I can see pdf in my browser, but when I try to save it, the file has no extension pdf. How to add this extension without saving report on my server?
This should probably do the trick:
resp.setHeader("Content-Disposition", "attachment;filename=report.pdf");

Categories