This question already has answers here:
URL Encode and Decode Special character in Java
(4 answers)
Closed 11 months ago.
I am new to java and i have characters like this
"%6D%61%61%20%77%61%72%64%20%6C%6F%20%65%6E%6E%69%20%73%61%72%6C%75%20%68%6F%75%73%65%20%73%69%74%65%20%6B%6F%72%61%6B%75%20%61%70%70%6C%79%20%63%68%65%73%69%6E%61%20%6D%61%27%61%6D%20%65%74%75%76%61%6E%74%69%20%75%70%61%79%6F%67%61%6D%20%6C%65%6B%75%6E%64%61%20%69%6E%64%69%76%69%64%75%61%6C%20%70%6F%79%69%6E%64%69%20%74%72%79%20%63%68%65%79%61%6D%61%6E%74%61%72%61%61%20%76%61%64%69%6C%65%79%61%6D%61%6E%74%61%61%72%61%20%70%6C%65%61%73%65%20%73%65%6E%64%20%6D%65%20%61%6E%73%77%65%72"
When i try to convert them using this tool. i am able to convert to the values as desired.
converted results :
"maa ward lo enni sarlu house site koraku apply chesina ma'am etuvanti upayogam lekunda individual poyindi try cheyamantaraa vadileyamantaara please send me answer"
but how can i do it using programmatically can someone help.
I tried this
String raw = "%6D%61%61%20%77%61%72%64%20%6C%6F%20%65%6E%6E%69%20%73%61%72%6C%75%20%68%6F%75%73%65%20%73%69%74%65%20%6B%6F%72%61%6B%75%20%61%70%70%6C%79%20%63%68%65%73%69%6E%61%20%6D%61%27%61%6D%20%65%74%75%76%61%6E%74%69%20%75%70%61%79%6F%67%61%6D%20%6C%65%6B%75%6E%64%61%20%69%6E%64%69%76%69%64%75%61%6C%20%70%6F%79%69%6E%64%69%20%74%72%79%20%63%68%65%79%61%6D%61%6E%74%61%72%61%61%20%76%61%64%69%6C%65%79%61%6D%61%6E%74%61%61%72%61%20%70%6C%65%61%73%65%20%73%65%6E%64%20%6D%65%20%61%6E%73%77%65%72";
String searchSection = new String(raw.getBytes(StandardCharsets.UTF_8), "UTF-8");
System.out.println( searchSection);
if this question is duplicate of any other question please point me in the right direction thanks
Converted text should match
You can achieve it using URLDecoder.decode(yourMessage, "UTF-8") from java.net:
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
public class Main {
public static void main(String[] args) {
String yourMessage = "%6D%61%61%20%77%61%72%64%20%6C%6F%20%65%6E%6E%69%20%73%61%72%6C%75%20%68%6F%75%73%65%20%73%69%74%65%20%6B%6F%72%61%6B%75%20%61%70%70%6C%79%20%63%68%65%73%69%6E%61%20%6D%61%27%61%6D%20%65%74%75%76%61%6E%74%69%20%75%70%61%79%6F%67%61%6D%20%6C%65%6B%75%6E%64%61%20%69%6E%64%69%76%69%64%75%61%6C%20%70%6F%79%69%6E%64%69%20%74%72%79%20%63%68%65%79%61%6D%61%6E%74%61%72%61%61%20%76%61%64%69%6C%65%79%61%6D%61%6E%74%61%61%72%61%20%70%6C%65%61%73%65%20%73%65%6E%64%20%6D%65%20%61%6E%73%77%65%72";
try {
System.out.println( URLDecoder.decode(yourMessage, "UTF-8"));
} catch (UnsupportedEncodingException uee) { }
}
}
Output:
maa ward lo enni sarlu house site koraku apply chesina ma'am etuvanti upayogam lekunda individual poyindi try cheyamantaraa vadileyamantaara please send me answer
Related
This question already has answers here:
What is the recommended way to escape HTML symbols in plain Java?
(12 answers)
Closed 3 years ago.
I have a program, in which a table is shown on the screen. The content is displayed correctly as long as there are no accents.
This is my code
StringBuffer sBuff = new StringBuffer();
if(!("CSV".equalsIgnoreCase(exportFormat) || "HTML".equalsIgnoreCase(exportFormat))){
sBuff.append("<p title=\""+displayparseado+"\">");
sBuff.append(displayparseado);
sBuff.append("</p>");
}
else{
sBuff.append(display);
}
I have also tried parsing the string.
byte arr[] = display.getBytes("UTF-8");
String displayparseado = new String(arr);
Expected output: Descripción de prueba.
Actual output:
A note; I can not use external libraries.
Try StringEscapeUtils to escape your String for html. This did a good job for me till now.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I saved my Java source file specifying it's encoding type as UTF-8 in my eclipse. It is working fine in eclipse.
When I create a build with maven & execute it in my system Unicode characters are not working.
This is my code :
byte[] bytes = new byte[dataLength];
buffer.readBytes(bytes);
String s = new String(bytes, Charset.forName("UTF-8"));
System.out.println(s);
Eclipse console & windows console screenshot attached.
Expecting eclipse output in other systems(windows command prompt, powershell window, Linux machine, etc.,).
You could use the Console class for that.The following code could give you some inspiration:
public class Foo {
public static void main(String[] args) throws IOException {
String s = "öäü";
write(s);
}
private static void write(String s) throws IOException {
String encoding = new OutputStreamWriter(System.out).getEncoding();
Console console = System.console();
if (console != null) {
// if there is a console attached to the jvm, use it.
System.out.println("Using encoding " + encoding + " (Console)");
try (PrintWriter writer = console.writer()) {
writer.write(s);
writer.flush();
}
} else {
// fall back to "normal" system out
System.out.println("Using encoding " + encoding + " (System out)");
System.out.print(s);
}
}
}
Tested on Windows 10(poowershell), Ubuntu 16.04(bash) with default settings. Also works from within IntelliJ (Windows and Linux).
From what I can tell, you either have the wrong character, which I don't think is the case, or you are trying to display it on a terminal that doesn't handle the character. I have written a short test to separate the issues.
public static void main(String[] args){
String testA = "ֆޘᜅᾮ";
String testB = "\u0586\u0798\u1705\u1FAE";
System.out.println(testA.equals(testB));
System.out.println(testA);
System.out.println(testB);
try(BufferedWriter check = Files.newBufferedWriter(
Paths.get("uni-test.txt"),
StandardCharsets.UTF_8,
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING) ){
check.write(testA);
check.write("\n");
check.write(testB);
check.close();
} catch(IOException ioc){
}
}
You could replace the values with the characters you want.
The first line should print out true if the string is the actual string you want. After that it is a matter of displaying the characters. For example if I open the text file with less then half of them are broken. If I open it with firefox, then I see all four characters, but some are wonky. You'll need a font that has characters for the corresponding unicode value.
One thing you can do is open the file in a word processor and select a font that displays the characters you want correctly.
As suggested by the OP, including the -Dfile.encoding=UTF8causes the characters to display correctly when using System.out.println. Similar to this question which changes the encoding of System.out.
This question already has answers here:
File to byte[] in Java
(27 answers)
Closed 6 years ago.
So, I have this .mp4 video file which I would like to convert into bytes and send it to my client.
At client, I'll receive all the bytes over RTP and then construct my own .mp4 file.
Please help me doing this, I'm not posting any code because, I don't know where to start and I'm all new to file handling in java
Thanks
You can use the Apache commons IOUtils.toByteArray method to create a byte array from an InputStream
Example:
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
class ConvertToByteArray
{
public static void main(String args[])
{
FileInputStream is = null;
try
{
is = new FileInputStream("file.mp4");
byte [] byteArr = IOUtils.toByteArray(is);
}
catch (IOException ioe)
{}
finally
{
// close things
}
}
}
You can download the jar her at Apache Commons IO
This question already has answers here:
How can I download and save a file from the Internet using Java?
(23 answers)
Closed 7 years ago.
I am using Yahoo's URL request ability to download stock data. I have found a java application that downloads the data every 5 seconds. I cant figure out how one would go about downloading stock data to a certain folder in ones computer while using the basic code I have used in the application. There has been other posts about how to save yahoo URL request data to a certain location but it does not use the same code format that I'm using. All ideas, source code, and links explaining how I should do this would be very helpful. Thank you for your help!
My code:
import java.awt.Desktop;
import java.net.URL;
public class Downloader {
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 10; i++) {
Thread.sleep(5000);
try {
Desktop.getDesktop()
.browse(new URL(
"http://finance.yahoo.com/d/quotes.csv?s=AAPL+GOOG+MSFT&f=nab")
.toURI());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
you could use Java's IO capabilities, Are you trying to write a GUI, if not any specific reason to use AWT's Desktop class?
Try this sample code:
URL yahooFinance = new URL("http://finance.yahoo.com/d/quotes.csv?s=AAPL+GOOG+MSFT&f=nab");
ReadableByteChannel channel = Channels.newChannel(yahooFinance.openStream());
FileOutputStream fos = new FileOutputStream("quotes.csv");
fos.getChannel().transferFrom(channel, 0, Long.MAX_VALUE);
You can also use org.apache.commons.io.FileUtils
java.net.URL yahooFinance = new java.net.URL("http://finance.yahoo.com/d/quotes.csv?s=AAPL+GOOG+MSFT&f=nab");
File f = new File("quotes.csv");
FileUtils.copyURLToFile(yahooFinance, f);
This question already has answers here:
URL encoding in Android
(7 answers)
Closed 9 years ago.
I am newbie in android, i have this url now i need to encode this url: the url that has to be encoded.
With Reference of downloading apk in Mobile using Java how can i pass this url to this Line :
updateAppInstance.execute("http://demo.ingresssolutions.com/proposalmanagement/services/user/getApkFile");
Previous post Regarding Apk Download
http://demo.ingresssolutions.com/proposalmanagement/services/user/uploadFile
Contrary to many answers you will read, java.net.URLEncoder isn't for encoding URLs. It is for encoding URL and POST arguments.
The correct way to encode a URL in Java is as given in this answer, or, if you only need to encode the path component:
String encodedPath = new URI(null, path, null).toASCIIString();
import java.net.URL;
import java.net.URLEncoder;
import java.net.MalformedURLException;
import java.io.UnsupportedEncodingException;
public class Encoder{
public static void main(String[] args){
URL url = null;
try{
url = new URL("http://www.stackoverflow.com");
}catch(MalformedURLException mue){
System.err.println(mue);
}
System.out.println(url + "
");
try{
String encodedurl = URLEncoder.encode(url.toString(),"UTF-8");
System.out.println(encodedurl);
}catch(UnsupportedEncodingException uee){
System.err.println(uee);
}
}
}
Hope this helps you