Receiving Image file in java - java

I am using basic java Server Client module to send a picture.
I am using this link for guidance
Below is my source code for the client.I have issues while receiving the file.
package sclient;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.Socket;
public class Sclient {
public static void main(String[] argv) throws Exception {
Socket sock = new Socket("192.168.0.10", 123);
byte[] mybytearray = new byte[1024*1024];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream(System.getProperty("user.dir")+"/imageTest.jpg");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(mybytearray, 0, mybytearray.length);
bos.write(mybytearray, 0, bytesRead);
bos.close();
sock.close();
}
}

Thats one util method of an android project, but its does the same. Reads and write is somewhere
/**
* #param inputStream
* #param inClose
* #return
* #throws IOException
*/
public static byte[] readContent(#NonNull final InputStream inputStream, final boolean inClose)
throws IOException {
Preconditions.checkNotNull(inClose, "InputStream");
//
final ByteArrayOutputStream theStream = new ByteArrayOutputStream(BUFFER_2K);
final byte[] theBuffer = new byte[BUFFER_2K];
try {
int theLength = inputStream.read(theBuffer);
while (theLength >= 0) {
theStream.write(theBuffer, 0, theLength);
theLength = inputStream.read(theBuffer);
}
return theStream.toByteArray();
} finally {
if (inClose) {
close(theStream);
}
}
}

Related

How to download WordPress with Java

I want to download WordPress with Java.
My code looks like this:
public void file(String surl, String pathToSave) throws IOException {
URL url = new URL(surl);
sun.net.www.protocol.http.HttpURLConnection con = (HttpURLConnection) url.openConnection();
try (InputStream stream = con.getInputStream()) {
Files.copy(stream, Paths.get(pathToSave));
}
}
I am using this url to download the latest version of WordPress: http://wordpress.org/latest.tar.gz
But when I try extracting the tar.gz file I get an error saying the file is not in a gzip format.
I read this Issues uncompressing a tar.gz file and it looks like when I download WordPress I need to have a cookie enabled to accept the terms and services.
How would I do this?
Or am I incorrectly downloading the tar.gz file?
Here is what my tar.gz extracting code:
public class Unzip {
public static int BUFFER = 2048;
public void tar(String pathToTar, String outputPath) throws IOException {
File tarFile = new File(pathToTar);
TarArchiveInputStream tarInput =
new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(tarFile)));
TarArchiveEntry currentEntry = tarInput.getNextTarEntry();
while(currentEntry != null) {
if (currentEntry.isDirectory()) {
File f = new File(outputPath + currentEntry.getName());
f.mkdirs();
}
else {
int count;
byte data[] = new byte[BUFFER];
FileOutputStream fos = new FileOutputStream(outputPath
+ currentEntry.getName());
BufferedOutputStream dest = new BufferedOutputStream(fos,
BUFFER);
while ((count = tarInput.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.close();
}
}
}
}
Thanks in advance.
Change sun.net.www.protocol.http.HttpURLConnection to java.net.HttpURLConnection
Add fos.close() after dest.close()
You must call currentEntry = tarInput.getNextTarEntry(); inside the while loop, too.
There is nothing with cookie enabled or accept the terms and services.
Here is my complete code.
Please try this and compare it to your code:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.zip.GZIPInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
public class Downloader {
public static final int BUFFER = 2048;
private void download(String surl, String pathToSave) throws IOException {
URL url = new URL(surl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
try (InputStream stream = con.getInputStream()) {
Files.copy(stream, Paths.get(pathToSave));
}
}
private void unGz(String pathToGz, String outputPath) throws IOException {
FileInputStream fin = new FileInputStream(pathToGz);
BufferedInputStream in = new BufferedInputStream(fin);
try (FileOutputStream out = new FileOutputStream(outputPath)) {
try (GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in)) {
final byte[] buffer = new byte[BUFFER];
int n = 0;
while (-1 != (n = gzIn.read(buffer))) {
out.write(buffer, 0, n);
}
}
}
}
public void unTarGz(String pathToTar, String outputPath) throws IOException {
File tarFile = new File(pathToTar);
TarArchiveInputStream tarInput
= new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(tarFile)));
TarArchiveEntry currentEntry;
while ((currentEntry = tarInput.getNextTarEntry()) != null) {
if (currentEntry.isDirectory()) {
File f = new File(outputPath + currentEntry.getName());
f.mkdirs();
} else {
int count;
byte data[] = new byte[BUFFER];
try (FileOutputStream fos = new FileOutputStream(outputPath
+ currentEntry.getName())) {
try (BufferedOutputStream dest = new BufferedOutputStream(fos,
BUFFER)) {
while ((count = tarInput.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
}
}
}
}
}
public static void main(String[] args) throws IOException {
Downloader down = new Downloader();
down.download("https://wordpress.org/latest.tar.gz", "/tmp/latest.tar.gz");
down.unTarGz("/tmp/latest.tar.gz", "/tmp/untar/");
}
}

Java code downloading data from this url

So I am new to java programming and I am supposed to download data from the URL below every 5 minute.
host: 204.8.38.210
Get:/Iowa.Sims.AllSites.C2C/IADOT_SIMS_AllSites_C2C.asmx/OP_ShareTrafficDetectorData?MSG_TrafficDetectorDataRequest=string%20HTTP/1.1
I get this error-"Server returned HTTP response code : 400 " using this code below
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class JavaDownload {
public static void main(String[] args) throws IOException{
String fileName = "file3x.html";
URL link = new URL("http://204.8.38.210/Iowa.Sims.AllSites.C2C/IADOT_SIMS_AllSites_C2C.asmx/OP_ShareTrafficDetectorData?MSG_TrafficDetectorDataRequest=string%20HTTP/1.1");
InputStream in = new BufferedInputStream(link.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n=0;
while(-1!=(n=in.read(buf)))
{
out.write(buf,0,n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(response);
fos.close();
System.out.println("Finished");
}
}

FileOutputStream is reducing the file size to 0

I have created a file using RandomAccessFile with preallocated size. But when I am using FileOutputStream to write into the same it is changing the size of the file. Is there any way to stop this using FileOutputStream
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.io.Serializable;
public class testFileSize {
public static class Status implements Serializable {
}
public static void preAllocate(String path, long maxSize, boolean preAllocate)
throws IOException {
RandomAccessFile raf = new RandomAccessFile(path, "rw");
try {
raf.setLength(maxSize);
} finally {
raf.close();
}
}
/**
* #param args
* #throws IOException
*/
public static void main(String[] args) throws IOException {
FileOutputStream fileOutput = null;
ObjectOutputStream objectOutput = null;
try {
final File f = new File("/tmp/test.bin");
preBlow(f.getAbsolutePath(), 2048, false);
Status s = new Status();
fileOutput = new FileOutputStream(f);
objectOutput = new ObjectOutputStream(fileOutput);
objectOutput.writeObject(new Status());
objectOutput.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
objectOutput.close();
fileOutput.close();
}
}
}
Looks like your file is changing size because you're opening the file on create mode, so the previous contents are lost
fileOutput = new FileOutputStream(f);
Try opening your file in append mode, using an extra boolean flag while constructing your FileOutputStream
fileOutput = new FileOutputStream(f, true);

Reading video data and writing to another file java

I am reading a video file data in bytes and sending to another file but the received video file is not playing properly and is chattered.
Can anyone explain me why this is happening and a solution is appreciated.
My code is as follows
import java.io.*;
public class convert {
public static void main(String[] args) {
//create file object
File file = new File("B:/music/Billa.mp4");
try
{
//create FileInputStream object
FileInputStream fin = new FileInputStream(file);
byte fileContent[] = new byte[(int)file.length()];
fin.read(fileContent);
//create string from byte array
String strFileContent = new String(fileContent);
System.out.println("File content : ");
System.out.println(strFileContent);
File dest=new File("B://music//a.mp4");
BufferedWriter bw=new BufferedWriter(new FileWriter(dest));
bw.write(strFileContent+"\n");
bw.flush();
}
catch(FileNotFoundException e)
{
System.out.println("File not found" + e);
}
catch(IOException ioe)
{
System.out.println("Exception while reading the file " + ioe);
}
}
}
This question might be dead but someone might find this useful.
You can't handle video as string. This is the correct way to read and write (copy) any file using Java 7 or higher.
Please note that size of buffer is processor-dependent and usually should be a power of 2. See this answer for more details.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FileCopy {
public static void main(String args[]) {
final int BUFFERSIZE = 4 * 1024;
String sourceFilePath = "D:\\MyFolder\\MyVideo.avi";
String outputFilePath = "D:\\OtherFolder\\MyVideo.avi";
try(
FileInputStream fin = new FileInputStream(new File(sourceFilePath));
FileOutputStream fout = new FileOutputStream(new File(outputFilePath));
){
byte[] buffer = new byte[BUFFERSIZE];
while(fin.available() != 0) {
bytesRead = fin.read(buffer);
fout.write(buffer, 0, bytesRead);
}
}
catch(Exception e) {
System.out.println("Something went wrong! Reason: " + e.getMessage());
}
}
}
Hope this also helpful for you - This can read and write a file into another file (You can use any file type to do that)
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Copy {
public static void main(String[] args) throws Exception {
FileInputStream input = new FileInputStream("input.mp4"); //input file
byte[] data = input.readAllBytes();
FileOutputStream output = new FileOutputStream("output.mp4"); //output file
output.write(data);
output.close();
}
}
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import javax.imageio.ImageIO;
public class Reader {
public Reader() throws Exception{
File file = new File("C:/Users/Digilog/Downloads/Test.mp4");
FileInputStream fin = new FileInputStream(file);
byte b[] = new byte[(int)file.length()];
fin.read(b);
File nf = new File("D:/K.mp4");
FileOutputStream fw = new FileOutputStream(nf);
fw.write(b);
fw.flush();
fw.close();
}
}
In addition to Jakub Orsula's answer, one needs to check the result of read operation to prevent garbage being written to end of file in last iteration.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class FileCopy {
public static void main(String args[]) {
final int BUFFERSIZE = 4 * 1024;
String sourceFilePath = "D:\\MyFolder\\MyVideo.avi";
String outputFilePath = "D:\\OtherFolder\\MyVideo.avi";
try(
FileInputStream fin = new FileInputStream(new File(sourceFilePath));
FileOutputStream fout = new FileOutputStream(new File(outputFilePath));
){
byte[] buffer = new byte[BUFFERSIZE];
int bytesRead;
while(fin.available() != 0) {
bytesRead = fin.read(buffer);
fout.write(buffer, 0, bytesRead);
}
}
catch(Exception e) {
System.out.println("Something went wrong! Reason: " + e.getMessage());
}
}
}

Base64 image encoding using java

I am trying to encode images using base64 encoding on the image URL.
But it gives the same encoding for all the URLs.
My code is as follows:
Object namee = url.openConnection().getContentType();
String name = (String) namee;
BufferedImage image = ImageIO.read(url);
//getting image extension from content type
String ext = name.substring(name.lastIndexOf("/") + 1);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, ext, baos);
byte[] imageData = baos.toByteArray();
String base64value = Base64.encodeBase64URLSafeString(imageData);
This Code convert your image file/data to Base64 Format which is nothing but text representation of your Image.
To convert this you need Encoder & Decoder which you will get from http://www.source-code.biz/base64coder/java/. It is File Base64Coder.java you will need.
Now to access this class as per your requirement you will need class below:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Base64 {
public static void main(String args[]) throws IOException {
/*
* if (args.length != 2) {System.out.println(
* "Command line parameters: inputFileName outputFileName");
* System.exit(9); } encodeFile(args[0], args[1]);
*/
File sourceImage = new File("back3.png");
File sourceImage64 = new File("back3.txt");
File destImage = new File("back4.png");
encodeFile(sourceImage, sourceImage64);
decodeFile(sourceImage64, destImage);
}
private static void encodeFile(File inputFile, File outputFile) throws IOException {
BufferedInputStream in = null;
BufferedWriter out = null;
try {
in = new BufferedInputStream(new FileInputStream(inputFile));
out = new BufferedWriter(new FileWriter(outputFile));
encodeStream(in, out);
out.flush();
} finally {
if (in != null)
in.close();
if (out != null)
out.close();
}
}
private static void encodeStream(InputStream in, BufferedWriter out) throws IOException {
int lineLength = 72;
byte[] buf = new byte[lineLength / 4 * 3];
while (true) {
int len = in.read(buf);
if (len <= 0)
break;
out.write(Base64Coder.encode(buf, 0, len));
out.newLine();
}
}
static String encodeArray(byte[] in) throws IOException {
StringBuffer out = new StringBuffer();
out.append(Base64Coder.encode(in, 0, in.length));
return out.toString();
}
static byte[] decodeArray(String in) throws IOException {
byte[] buf = Base64Coder.decodeLines(in);
return buf;
}
private static void decodeFile(File inputFile, File outputFile) throws IOException {
BufferedReader in = null;
BufferedOutputStream out = null;
try {
in = new BufferedReader(new FileReader(inputFile));
out = new BufferedOutputStream(new FileOutputStream(outputFile));
decodeStream(in, out);
out.flush();
} finally {
if (in != null)
in.close();
if (out != null)
out.close();
}
}
private static void decodeStream(BufferedReader in, OutputStream out) throws IOException {
while (true) {
String s = in.readLine();
if (s == null)
break;
byte[] buf = Base64Coder.decodeLines(s);
out.write(buf);
}
}
}
In Android you can convert your Bitmap to Base64 for Uploading to Server/Web Service.
Bitmap bmImage = //Data
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageData = baos.toByteArray();
String encodedImage = Base64.encodeArray(imageData);
This “encodedImage” is text representation of your Image. You can use this for either uploading purpose or for diplaying directly into HTML Page as below:
<img alt="" src="data:image/png;base64,<?php echo $encodedImage; ?>" width="100px" />
<img alt="" src="data:image/png;base64,/9j/4AAQ...........1f/9k=" width="100px" />
Here is a working example on how to convert image to base64 with Java.
public static String encodeToString(BufferedImage image, String type) {
String base64String = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ImageIO.write(image, type, bos);
byte[] imageBytes = bos.toByteArray();
BASE64Encoder encoder = new BASE64Encoder();
base64String = encoder.encode(imageBytes);
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return base64String;
}

Categories