image not showing when transferred using InputStream and OutputStream in java - java

This is my test program. I need it to apply somewhere.This may be small, sorry for that. But I'm a starter still. So kindly help me.
try{
File file1 = new File("c:\\Users\\prasad\\Desktop\\bugatti.jpg");
File file2 = new File("c:\\Users\\prasad\\Desktop\\hello.jpg");
file2.createNewFile();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file1)));
String data = null;
StringBuilder imageBuild = new StringBuilder();
while((data = reader.readLine())!=null){
imageBuild.append(data);
}
reader.close();
BufferedWriter writer = new BufferedWriter(new PrintWriter(new FileOutputStream(file2)));
writer.write(imageBuild.toString());
writer.close();
}catch(IOException e){
e.printStackTrace();
}
This is file1
and This is file2

You can do either of these two:
private static void copyFile(File source, File dest) throws IOException {
Files.copy(source.toPath(), dest.toPath());
}
or maybe this if you want to use streams:
private static void copyFile(File source, File dest)
throws IOException {
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(source);
output = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
} finally {
input.close();
output.close();
}
}

Images do not contain lines or even characters. You therefore should not be using readLine() or even Readers or Writers. You should rewrite the copy loop using input and output streams directly.

Related

How to create a json_template?

I'm creating some code and I saw an example here on this forum, and I have a hard time using geojson.
Whenever it is giving an error in raw because I did not add this json_template
private String getGeoString() throws IOException{
InputStream is = getResources().openRawResource(R.raw.json_template);
Writer writer = new StringWriter();
char[] buffer = new char [1024];
try{
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
while ((n= reader.read(buffer)) != -1){
writer.write(buffer, 0, n);
}
}finally {
is.close();
}
String jsonString = writer.toString();
return jsonString();
}
How can I solve this error?
If you would like to create a json file in your app, you can first create a json_template.json file in your app -> res -> raw folder.
In that file, you can put the entire json response received upon querying.
A useful tool to see the json traversal in the nested response is JSON Pretty Print.
Then you can try:
public static String getGeoString(Context context) throws IOException {
InputStream is = context.getResources().openRawResource(R.raw.json_template);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
// The local variable 'jsonString' can be inlined as below
return writer.toString();
}
It should work. Hope this is helpful.

Display or download image through the browser

I have a server that displays what the user asks for from the browser, I am using linux and when i run the server and ask for a file like Image.png using this link localhost:9999/Image.png on FireFox i get this message:
The image "localhost:9999/Image.png" cannot be displayed because it
contains errors.
But when i change the variable fileName to an HTML file it works perfectly and i can visualize the html page.
What am I doing wrong??
This is my server:
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Server {
public static void main(String args[]) throws IOException {
// Declarem les variables a utilitzar
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inS = null;
OutputStream outS = null;
try
{
serverSocket = new ServerSocket(9999);
while(true)
{
socket= serverSocket.accept();
inS = socket.getInputStream();
outS = socket.getOutputStream();
try{
BufferedReader br = new BufferedReader(new InputStreamReader(inS));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outS));
System.out.println("THis is what the user wants = " + br.readLine());
String fileName = "Image.png";
String extension= "";
int i = fileName.lastIndexOf('.');
if (i > 0) {
extension = fileName.substring(i+1);
}
String dataReturn = "";
if(extension.equals("png"))
{
bw.write("HTTP/1.0 200 OK\r\n");
bw.write("Content-Type: image/png\r\n");
bw.write("\r\n");
FileReader myFilepng = new FileReader(fileName);
Scanner scanner1 = new Scanner(myFilepng);
dataReturn = "";
while(scanner1.hasNextLine()) {
dataReturn = scanner1.nextLine();
System.out.println(dataReturn);
bw.write(dataReturn);
}
scanner1.close();
}else{
if(extension.equals("html"))
{
bw.write("HTTP/1.0 200 OK\r\n");
bw.write("Content-Type: text/html\r\n");
bw.write("\r\n");
bw.write("<TITLE>"+fileName+"/TITLE>");
FileReader myFile = new FileReader(fileName);
Scanner scanner = new Scanner(myFile);
dataReturn = "";
while(scanner.hasNextLine()) {
dataReturn = scanner.nextLine();
System.out.println(dataReturn);
bw.write(dataReturn);
}
scanner.close();
}
}
bw.close();
}catch(Exception e)
{
}
}
}catch (IOException e) {
System.out.println(e);
}
inS.close();
outS.close();
socket.close();
}
}
You are not writing the contents of your png file to your bw BufferedWriter. Instead you are only sending the header of the response to the client. As you are indicating your response is a png image and there is no data, your browser is telling you the image contains errors (in fact, it does not contains nothing at all).
Open the png filename, write the data to your "bw" buffer to send it to the client. That should be enough.
Edit:
To to that, try the following code for your "if" is image:
if(extension.equals("png"))
{
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
fis.read(data);
fis.close();
DataOutputStream binaryOut = new DataOutputStream(outS);
binaryOut.writeBytes("HTTP/1.0 200 OK\r\n");
binaryOut.writeBytes("Content-Type: image/png\r\n");
binaryOut.writeBytes("Content-Length: " + data.length);
binaryOut.writeBytes("\r\n\r\n");
binaryOut.write(data);
binaryOut.close();
}
Note the use of a binary stream in comparison to the text stream you use in case of html.

How to reference a File in raw folder in Android

I just want to create a File object like this
File myImageFile = new File ("image1") ;
but it is giving me exception of FileNotFoundException
How can i reference a file inside my raw Folder
EDIT:
Actually i wanted to do something like this
MultipartEntity multipartEntity= new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("uploaded", new FileBody(new File("myimage")));
Generally you access the files through getResources().openRawResource(R.id._your_id). If you absolutely need a File reference to it, one option is to copy it over to internal storage:
File file = new File(this.getFilesDir() + File.separator + "DefaultProperties.xml");
try {
InputStream inputStream = resources.openRawResource(R.id._your_id);
FileOutputStream fileOutputStream = new FileOutputStream(file);
byte buf[]=new byte[1024];
int len;
while((len=inputStream.read(buf))>0) {
fileOutputStream.write(buf,0,len);
}
fileOutputStream.close();
inputStream.close();
} catch (IOException e1) {}
Now you have a File that you can access anywhere you need it.
here are 2 functions. one to read from RAW and one from the Assets
/**
* Method to read in a text file placed in the res/raw directory of the
* application. The method reads in all lines of the file sequentially.
*/
public static void readRaw(Context ctx,int res_id) {
InputStream is = ctx.getResources().openRawResource(res_id);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr, 8192); // 2nd arg is buffer
// size
// More efficient (less readable) implementation of above is the
// composite expression
/*
* BufferedReader br = new BufferedReader(new InputStreamReader(
* this.getResources().openRawResource(R.raw.textfile)), 8192);
*/
try {
String test;
while (true) {
test = br.readLine();
// readLine() returns null if no more lines in the file
if (test == null)
break;
}
isr.close();
is.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
and from Assets folder
/**
* Read a file from assets
*
* #return the string from assets
*/
public static String getQuestions(Context ctx,String file_name) {
AssetManager assetManager = ctx.getAssets();
ByteArrayOutputStream outputStream = null;
InputStream inputStream = null;
try {
inputStream = assetManager.open(file_name);
outputStream = new ByteArrayOutputStream();
byte buf[] = new byte[1024];
int len;
try {
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
}
outputStream.close();
inputStream.close();
} catch (IOException e) {
}
} catch (IOException e) {
}
return outputStream.toString();
}
You can open it as InputStream, I don't know if possible as a file:
int rid = resources.getIdentifier(packageName + ":raw/" + fileName, null, null);
//get the file as a stream
InputStrea is = resources.openRawResource(rid);
You can use InputStreamBody instead of FileBody so you can use it like this:
InputStream inputStream = resources.openRawResource(R.raw.yourresource);
MultipartEntity multipartEntity= new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("uploaded", new InputStreamBody(inputStream));

Reading single InputStream from multiple methods

I have initialized an InputStream in a single method in a class and passing it to next method for processing. The InputStream essentially encapsulates CSV file for processing.
Another method calls 2 different methods passing in same InputStream one for retrieving headers and another for processing contents. The structure looks something as given below:
main() {
FileInputStream fis = new FileInputStream("FileName.CSV");
BufferedInputStream bis = new BufferedInputStream(fis);
InputStreamReader isr = new InputStreamReader(bis);
processCSV(isr);
}
processCSV(Reader isr) {
fetchHeaders(isr);
processContentRows(isr);
}
fetchHeaders(Reader isr) {
//Use BufferedReader to retrieve first line of CSV
//Even tried mark() and reset() here
}
processContentRows(Reader isr) {
//Cannot read the values, fetches null from InputStream :(
}
Am I doing something wrong here? Is there any way I can reuse InputStream across different method calls.
I am putting up complete program that can mimic the issue below:
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class MarkResetTest
{
public static void main(String a[])
{
FileInputStream fis = null;
BufferedInputStream bis = null;
InputStreamReader isr = null;
BufferedReader br = null;
BufferedReader br2 = null;
try {
fis = new FileInputStream("C:/Test/Customers.csv");
bis = new BufferedInputStream(fis);
isr = new InputStreamReader(bis, "Unicode");
System.out.println("BR readLine()");
br = new BufferedReader(isr);
//System.out.println(br.markSupported());
br.mark(1000);
System.out.println(br.readLine());
br.reset();
//System.out.println(br.readLine());
System.out.println("BR2 readLine()");
br2 = new BufferedReader(isr);
System.out.println(br2.readLine());
}
catch(Exception e) {
System.out.println("Exception::" + e);
}
finally {
try {
br.close();
isr.close();
bis.close();
fis.close();
}
catch(Exception e) {
System.out.println("Exception while closing streams :: " + e);
}
}
}
}
The problem is in creating two BufferedReaders on top of the same Reader. When you read data from BufferedReader, it's likely to read more than the data it returns, into its buffer (hence the name). In other words, even though you've only read a single line from the BufferedReader, the InputStreamReader may have had a lot more data read from it - so if you read again from that InputStreamReader then you'll miss that data. The data has effectively been sucked from the InputStreamReader to the BufferedReader, so the only way of getting it out to client code is to read it from that BufferedReader.
In other words, your claim that:
Nope. fetchHeaders() only reads first line of CSV containing Headers.
is incorrect. It only uses that much data, but it reads more from the InputStreamReader.
As Ilya said, you should only create one BufferedReader on top of the original InputStreamReader, and pass that into both methods.
fetchHeaders can then use that BufferedReader to read a line, and processContentRows can do what it likes with the BufferedReader at that point - it's just a Reader as far as it needs to know.
So to modify Ilya's example slightly:
public static void main(String[] args) {
FileInputStream fis = new FileInputStream("FileName.CSV");
BufferedInputStream bis = new BufferedInputStream(fis);
InputStreamReader isr = new InputStreamReader(bis);
BufferedReader br = new BufferedReader(isr);
processCSV(br);
}
private static void processCSV(BufferedReader reader) {
fetchHeaders(reader);
processContentRows(reader);
}
private static void fetchHeaders(BufferedReader reader) {
// Use reader.readLine() here directly... do *not* create
// another BufferedReader on top.
}
private static void processContentRows(Reader reader) {
// This could be declared to take a BufferedReader if you like,
// but it doesn't matter much.
}
You're not doing anything wrong. Just make sure that the method opening a stream/reader also closes it, in a finally block.
If you need a BufferedReader, I think you need to create it in in main method:
main() {
FileInputStream fis = new FileInputStream("FileName.CSV");
BufferedInputStream bis = new BufferedInputStream(fis);
InputStreamReader isr = new InputStreamReader(bis);
BufferedReader br = new BufferedReader(isr);
processCSV(br);
}
processCSV(Reader isr) {
fetchHeaders(isr);
processContentRows(isr);
}
fetchHeaders(Reader isr) {
//Use BufferedReader to retrieve first line of CSV
//Even tried mark() and reset() here
}
processContentRows(Reader isr) {
//Cannot read the values, fetches null from InputStream :(
}
You can do this by using byte
public static void main(String[] args) throws IOException {
String str = "01 Lorem ipsum\n 02 dolor sit\n 03 amet\n";
InputStream is = new ByteArrayInputStream(str.getBytes("UTF-8"));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) > -1) {
outputStream.write(buffer, 0, len);
}
outputStream.flush();
InputStream in1 = new ByteArrayInputStream(outputStream.toByteArray());
InputStream in2 = new ByteArrayInputStream(outputStream.toByteArray());
InputStreamReader isr1 = new InputStreamReader(in1);
BufferedReader br1 = new BufferedReader(isr1);
InputStreamReader isr2 = new InputStreamReader(in2);
BufferedReader br2 = new BufferedReader(isr2);
System.out.println("One line from br1:");
System.out.println(br1.readLine());
System.out.println();
System.out.println("One line from br2:");
System.out.println(br2.readLine());
System.out.println(br2.readLine());
System.out.println();
}

Socket Programming file upload issues?Complete file not uploading

Hi i am using the following code for uploding my file from android phone to the server bt the file does not upload completely..e.g i uploded a 11kb file and got only 8kb file at the server.What am i doing wrong?
Client side
Socket skt = new Socket"112.***.*.**", 3000);
String FileName=fil.getName();
PrintWriter out2 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(skt.getOutputStream())),true);
out2.println("Upload");
out2.println(FileName);
out2.println(spinindx);
out2.println(singleton.arrylst_setngs.get(0).toString());
out2.println(singleton.arrylst_setngs.get(1).toString());
out2.println(singleton.arrylst_setngs.get(2).toString());
out2.println(singleton.arrylst_setngs.get(3).toString());
out2.println(singleton.arrylst_setngs.get(4).toString());
out2.flush();
//Create a file input stream and a buffered input stream.
FileInputStream fis = new FileInputStream(fil);
BufferedInputStream in = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream(skt.getOutputStream());
//Write the file to the server socket
int i;
byte[] buf = new byte[512];
while ((i = in.read(buf)) != -1) {
out.write(buf,0,i);
publishProgress(in.available());
System.out.println(i);
}
//Close the writers,readers and the socket.
in.close();
out.flush();
out.close();
out2.close();
skt.close();
}
catch( Exception e ) {
System.out.println(e);
}
The server side
InputStream inStream = socket.getInputStream();
BufferedReader inm = new BufferedReader(new InputStreamReader(inStream));
String Request=inm.readLine();
if(Request.equals("Upload")){
fileName = inm.readLine();
chosn = inm.readLine();
lt=inm.readLine();
cs = inm.readLine();
om = inm.readLine();
o = inm.readLine();
check=inm.readLine();
//Read, and write the file to the socket
BufferedInputStream in = new BufferedInputStream(inStream);
int i=0;
File f=new File("D:/data/"+filePrefx+fileName);
if(!f.exists()){
f.createNewFile();
}
FileOutputStream fos = new FileOutputStream("D:/data/"+filePrefx+fileName);
BufferedOutputStream out = new BufferedOutputStream(fos);
byte[] buf = new byte[512];
while ((i = in.read(buf)) != -1) {
System.out.println(i);
out.write(buf,0,i);
System.out.println("Receiving data...");
}
in.close();
inStream.close();
out.close();
fos.close();
socket.close();
Looks like you are using both a BufferedReader and a BufferedInputStream on the same underlying socket at the server side, and two kinds of output stream/writer at the client. So your BufferedReader is buffering, which is what it's supposed to do, and thus 'stealing' some of the data you're expecting to read with the BufferedInputStream. Moral: you can't do that. Use DataInputStream & DataOutputStream only, and writeUTF()/readUTF() for the 8 lines you are reading from the client before the file.
You shared the same underlying InputStream between your BufferedReader and bufferedInputStream.
What happened is, when you do the reading through BufferedReader, it reads more than the a few lines you requested from the underlying InputStream into its own internal buffer. And when you create the BufferedInputStream, the data has already been read by the BufferedReader. So Apart from what EJP suggested not to use any buffered class, you can create the BufferedInputStream, and then create the Reader on Top of it. The code is something like this:
BufferedInputStream in = new BufferedInputStream(inStream);
Reader inm = new InputStreamReader(in);
Add it to the beginning of your server code and remove this line:
BufferedInputStream in = new BufferedInputStream(inStream);
See this, i never tried though
void read() throws IOException {
log("Reading from file.");
StringBuilder text = new StringBuilder();
String NL = System.getProperty("line.separator");
Scanner scanner = new Scanner(new FileInputStream(fFileName), fEncoding);
try {
while (scanner.hasNextLine()){
text.append(scanner.nextLine() + NL);
}
}
finally{
scanner.close();
}
log("Text read in: " + text);
}
Shamelessly copied from
http://www.javapractices.com/topic/TopicAction.do?Id=42

Categories

Resources