Loading a png file created from a php script - java

I am downloading png files to a bimap.
bitmap = BitmapFactory.decodeStream(new URL(url).openConnection().getInputStream());
It works perfectly for "static"ยท images,images stored in a server with his .png file extension
But if the image is a jsp script response nothing is downloaded into the bitmap
The url of the script
http://xxxxxxxxxxxxxx:9000/xxxxxxx/jsp/ios/imageAlumno?X_ALUMNO=144244
The image is displayed properly in a browser and the html code of the response is:
<html><body style="margin: 0px;"><img style="-webkit-user-select: none" src="http://XXXXXXXXXXXXXXX:9000/XXXXXXX/jsp/ios/imageAlumno?X_ALUMNO=144244"></body></html>
Any idea?

First guess is: the JSP code sends wrong image header. Java is probably less permissive than a web browser when it comes to certain rules... I would first check what header is sent back to the user. Use the telnet XXXXXXXXXXXXXXX:9000, and then type in the GET /XXXXXXX/jsp/ios/imageAlumno?X_ALUMNO=144244 to pretend you are a browser, and you will see what the web server sends back.

Related

play! Uploaded images not visible in production mode

Hi guys I'm new to play framework,
Qun 1:
what I wanted in my server is Users can upload images, and see uploaded images in web page,
when I run the server in Dev mode everything is works fine (I can upload and see the images in web page),
but I try do this same thing in production mode, uploading works fine(images saved in public directory) but images are not visible in web page...!
my twirl html template:
<img src="#routes.Assets.versioned(public/file1.png)" width=25% height=auto alt="file1.png">
routes:
GET /versionedAssets/*file controllers.Assets.versioned(path="/public", file: Asset)
browser's Elements page (F12):
<img src="/versionedAssets/file1.png" width="25%" height="auto" alt="file1.png">
I can even show the image using NGINX (passing image location as an url) successfully,
but why i can't see the image in play's production mode?,
I don't even have clue about what part I did wrong.!
please help me if you know anything about it.!
Qun 2:
Is that possible to serve our app without frontend server like NGINX, etc.
because play already running in AkkaHttpServer.
After reading docs
I've found Ok.SendFile() method used to serving files, then I've created a method in controller
def getImageFromPath()= Action{ implicit request: Request[AnyContent] =>
Ok.sendFile(new java.io.File("/path/to/file")) }
then added route in routes file
GET /getimage controllers.ImageController.getImageFromPath()
reverse routing in Twirl template
<img src="#routes.ImageController.getImageFromPath()" width=25% height=auto alt="file1.png">
helped me to solve this issue..!

HTTP header for inline PDF filename from Java webserver

I need to send to the client a byte[] with a pdf data from my tomcat server.
I'm using this:
response.setContentType("application/pdf");
response.setHeader("Content-Disposition:","inline; filename=test.pdf");
But (at least) with firefox I get a file download instead of inline display.
The only way to show pdf data inline is to remove the Content-Disposition header record however, if I do so I cannot set the filename, the pdf name is get from the last folder of url.
You seems to be setting the right headers. But rendering of pdf or another such formats depends on the browser capabilities as well. I mean browser need to have a pdf plugin installed in order to render a pdf when it sees the same in the contentType header field. So make sure you install a pdf plugin for your firefox and try to test after that. You can download firefox pdf plugin from here:
https://addons.mozilla.org/en-US/firefox/addon/pdf-download/

smartgwt save image file instead show in browser

I've got a smartgwt application which create a link with a jpg/gif/png/pdf files. This files are shown in browser. I want to get the save dialog instead it which ask me the path when I want to save the file at local machine. How could I do that?
As I know, you have to change the response header by setting the Content-disposition to attachment. Like this:
'Content-disposition: attachment; filename=image.jpg'
'Content-type: image/jpeg'
With these the browser will understand most cases that it should show up a dialog to save the image with the name: image.jpg. Also it might offer you to send it directly to an application, for example to an image viewer.
To get it work from a simple link, perhaps you have to write a servlet which will return the requested file with the correct headers and call that servlet from every link with a parameter to the real file.

Display Image on web page using Java

In my Java EE app, there is a function to upload images. When uploading an image, I'm saving image path in MySQL database.
Now I want to display uploaded image on a web page using image path that saved when uploading the image in my MySQL database table.
How could I do this ?
It depends. Is the path someplace that a web server is serving documents from? Then just include the path, adjusted as necessary, in an IMG tag. If not, or if the image data itself is actually in the database (you're not entirely clear about this) then create a servlet which returns the contents of an image based on query parameters, and use that servlet's URL (plus the query parameters) in the SRC attribute of the IMG tag.
Write a servlet that extracts your stored image from database and writes back into servlet outputstream.
You need to set the relevant mime type of the image, for example "image/jpeg", before writing into outputstream.
You need to point the image source to this servlet url with required input parameters to load the correct image from the database. For example :
<img src="http://mydomain/servlet/imageServlet?imgid=xyz" />
There is nice example given by BalusC at: ImageServlet serving from database

Returning binary and text data in servlet response

Does anyone have an example of how to send both binary (image) and text-based data in a single servlet response? An example is returning an image and image-map all generated on the server. I was also not able to find a mixed-mode mime type to use to be able to perform this operation.
Thoughts?
Browser support for multipart responses is still pretty dicey (read here). But if you are planning to parse the response on the client side yourself there are some pretty good examples out there. The mime-type you are looking for is multipart/mixed.
You can use Data URI to embed binary objects into generated HTML.
E.g.
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4/8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
See also: https://serverfault.com/questions/241218/why-do-http-servers-not-send-a-single-file-back-when-possible#241224
This is not how HTTP and HTML work. A first request is made to load HTML code. This HTML code contains <img src="..."/> tags, which point to the URL of the image. A second request is then made by the browser to load the image. You can't download the HTML and the image in a single request.
Many WAP browsers supports multi-part responses, but I don't think "regular" browsers do.
Also see Browser support of multipart responses

Categories