How to convert .mp4 video to byte array in Java? [duplicate] - java

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

Related

Read entire File to String Spring Boot [duplicate]

This question already has answers here:
How do I load a resource and use its contents as a string in Spring
(7 answers)
Closed 9 months ago.
I have a text file with path as resources/templates/temp.txt. I tried reading it with built in Files.readAllBytes() function found in java.nio but that results in an NullPointerException. My code looks as follows:
try {
File file = new File(getClass().getResource("templates/temp.txt").getFile());
String temp = new String(Files.readAllBytes(Paths.get(file.getPath())));
System.out.println(temp);
}catch(Exception e){
e.printStackTrace();
}
The file actually does exist at this location because I am able to get an InputStream and use that in an InputStreamReader and BufferedReader to read the file line-by-line.
InputStream input = this.getClass().getClassLoader().getResourceAsStream(path)
//path here is "templates/temp.txt"
I would like to read this file with a built in method rather than iterating over the entire thing.
Would appreciate it very much if someone can help with this. Thanks in advance!
How does your code even compile? You have static reference in non-static env:
... new File(getClass().getResource ...
try to get the bytearray before transforming into string:
try {
byte[] tmpba = Files.readAllBytes(Paths.get("templates/temp.txt"));
String s = new String(tmpba);
System.out.println(s);
}catch(Exception e){
e.printStackTrace();
}

Java Convert Unicode String to English String [duplicate]

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

How to save a file to a certain folder which was downloaded in a rest URL request? [duplicate]

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);

How to Decode a URL using Java in Android? [duplicate]

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

Write Base64-encoded image to file

How to write a Base64-encoded image to file?
I have encoded an image to a string using Base64.
First, I read the file, then convert it to a byte array and then apply Base64 encoding to convert the image to a string.
Now my problem is how to decode it.
byte dearr[] = Base64.decodeBase64(crntImage);
File outF = new File("c:/decode/abc.bmp");
BufferedImage img02 = ImageIO.write(img02, "bmp", outF);
The variable crntImage contains the string representation of the image.
Assuming the image data is already in the format you want, you don't need ImageIO at all - you just need to write the data to the file:
// Note preferred way of declaring an array variable
byte[] data = Base64.decodeBase64(crntImage);
try (OutputStream stream = new FileOutputStream("c:/decode/abc.bmp")) {
stream.write(data);
}
(I'm assuming you're using Java 7 here - if not, you'll need to write a manual try/finally statement to close the stream.)
If the image data isn't in the format you want, you'll need to give more details.
With Java 8's Base64 API
byte[] decodedImg = Base64.getDecoder()
.decode(encodedImg.getBytes(StandardCharsets.UTF_8));
Path destinationFile = Paths.get("/path/to/imageDir", "myImage.jpg");
Files.write(destinationFile, decodedImg);
If your encoded image starts with something like data:image/png;base64,iVBORw0..., you'll have to remove the part. See this answer for an easy way to do that.
No need to use BufferedImage, as you already have the image file in a byte array
byte dearr[] = Base64.decodeBase64(crntImage);
FileOutputStream fos = new FileOutputStream(new File("c:/decode/abc.bmp"));
fos.write(dearr);
fos.close();
import java.util.Base64;
.... Just making it clear that this answer uses the java.util.Base64 package, without using any third-party libraries.
String crntImage=<a valid base 64 string>
byte[] data = Base64.getDecoder().decode(crntImage);
try( OutputStream stream = new FileOutputStream("d:/temp/abc.pdf") )
{
stream.write(data);
}
catch (Exception e)
{
System.err.println("Couldn't write to file...");
}
Other option using apache-commons:
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
...
File file = new File( "path" );
byte[] bytes = Base64.decodeBase64( "base64" );
FileUtils.writeByteArrayToFile( file, bytes );
Try this:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
public class WriteImage
{
public static void main( String[] args )
{
BufferedImage image = null;
try {
URL url = new URL("URL_IMAGE");
image = ImageIO.read(url);
ImageIO.write(image, "jpg",new File("C:\\out.jpg"));
ImageIO.write(image, "gif",new File("C:\\out.gif"));
ImageIO.write(image, "png",new File("C:\\out.png"));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Done");
}
}

Categories