Show Images with name containing special characters - java

I am trying to display some images containing special characters like ☻ ☺ ♥ or Chinese or Arabic characters in their names using jsp...but the images are not getting displayed !!
<img src = "pipo².jpg" />
<img src = "pip☺☻♥o².jpg" />
What am I doing wrong !!

Try encoding the filename using URLEncoder.encode() method before the HTML is sent to the page, e.g.
String encodedString = URLEncoder.encode(filename, "UTF-8").
This will convert the characters to entities which can be passed in HTML.

you can percent encode the urls using encodeURIComponent in javascript to give you
<img src="pip%C3%A2%C2%98%C2%BA%C3%A2%C2%98%C2%BB%C3%A2%C2%99%C2%A5o%C3%82%C2%B2.jpg">

I'd recommend renaming your files.
Using special characters in src paths is not strictly allowed, you'd have to find the URL style escape codes for those characters.

Related

Java - Escape quotes in style attribute

In a Java application I have HTML, as a String, that looks like this:
<DIV STYLE="font-family:"Times New Roman"">
And I wish to decode the encoded quotes so that it is correctly displayed on the page. The problem is that conventional StringEscapeUtils escape methods will decode each quote as a double quote, resulting in HTML like this:
<DIV STYLE="font-family:"Times New Roman"">
Which will not correctly render on the page. The desired result is for the HTML to look like this:
<DIV STYLE='font-family:"Times New Roman"'>
I can algorithmically examine the string to replace the encoded quotes to what I want but is there a dedicated method to correctly decode quotes for such a String?
If it is defined in your java code
you may try to add \ before "
I assume you are expecting something like this right?
String randomHtmlCode = " <DIV STYLE='font-family:\"Times New Roman\"'> ";

href in HTML and forward slash (/) and %2F?

I am generating an HTML page that lists files in a directory in Java. Each file name is placed in a link to the file. Since the directory names and file names contain non-ASCII characters and spaces, I used the following method to encode them.
URLEncoder.encode(str, "UTF-8").replace("+", "%20");
I can either put the full directory to href of <base> or append it to the href of <a> for each file. That method above converts / to %2F. But I have seen href contains /.
So,
should I replace / to %2F or
should I leave / as it is or
it does not matter whether it is / or %2F?
If the answer is 2, what Java method should I use instead of URLEncoder.encode() because it replaces / with %2F.

Dynamic file path in assets reverse routing

I am trying to use reverse routing to access static Assets using:
#routes.Assets.at("path", "file")
However I would like to define file as dynamic part as well like:
#for(c <- models.WebContent.find.all) {
<img src="#routes.Assets.at("/contentfiles/useruploads", "#c.picture1")">
}
Statement above however results in HTML code:
<img src="/contentfiles/userupload/#c.picture1">
Where you can see dynamic part #c.picture1 is not interpreted as dynamic filename but is parsed as raw text resulting in broken link. What I am expecting is that both dynamic parts are interpreted as dynamic resulting in eg.:
<img src="/contentfiles/userupload/1776446515.jpg">
How to define it so both dynamic statements are parsed as dynamic?
PS: I have tried to escape it as ##c.picture or $#c.picture with no luck
Thank you
When using variables as a function argument use it w/out # char and also not within quotes, otherwise as you can is it's used as a... String
<img src="#routes.Assets.at("/contentfiles/useruploads", c.picture1)">
The same as in condition:
Use:
#if(foo==bar){...}
NOT
#if(#foo==#bar){...}

How to convert url encoded string to plain text in JAVA

I am looking for html references in a HTML page I am retrieving from the server. The problem is for all the hyperlinks I am retrieving, the text that I am getting is URL encoded. Lets say, the URL is "http://abc.def.com/gh?ij=x&kl=y&mn=z", my program parses it as "http://abc.def.com/gh?ij=3Dx&kl=3Dy&mn=3Dz" . (look at the difference around "=" and "&" in the two URL's) . Some searching on the Web tells me that the second URL is a URL encoded form of the first URL.
What should I do to retrieve the actual URL as it is, and not its URL encoded version? Right now, I am replacing =3D with 3D and & with &, but that is a very bad hack.
Try to use java.net.URLDecoder

Java utf-8 encoding from url

I have a problem with some symbols in UTF-8 encoding.
I am reading the index.html from http://wordki.pl to get the list of sets of words with their name.
it looks like this
THE NAME<span>(20)</span><img src="krecha.png">
and when THE NAME has "Ł" it doesent work and puts there "??" but the "??" is not a sign that i can change with replaceAll("str", "str") because my console just doesent show the char hidden behind it.
But when i view the source in chrome/firefox etc it shows "Ł".
And all the other funny signs like "ó, ł, ą, ś" work fine in my program.
So I am asking if there is a way to change the "??" into "Ł" ? I tried encoding it byte by byte but then i lose all the other signs like "ó, ł, ą" etc.
EDIT: Ok i have the problem solved
I needed to save my *.java file as UTF-8 : O
You should set the page content-type as "UTF-8"
Do something like this:
request.getCharacterEncoding() = ISO-8859-1
response.getCharacterEncoding() = UTF-8
request.getParameter("query") = déjeuner
OR
if(null == request.getCharacterEncoding())
request.setCharacterEncoding(encoding);
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
Refer this URL for more info:
How to get UTF-8 working in Java webapps?
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">

Categories

Resources