Getting the binary of an image in java http - java

I need to read an image file and send it to a web browser using HTTP protocol, and I cannot figure out how to send the bytes of the image. I cannot for the life of me figure it out. Thanks in advance. Here is my code:
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import javax.imageio.ImageIO;
public class Main {
byte[] byt=null;
public String read(String message){
String httpHeader="";
String toReturn="";
try{
if(message!=null){
String[] parts=message.split("\n");
String[] RequstParts=parts[0].split(" ");
System.out.println("This is a "+RequstParts[0]+" request for "+RequstParts[1]);
if(RequstParts[1].equals("/"))RequstParts[1]="index.html";
if(RequstParts[0].equals("GET")){
if(RequstParts[1].endsWith(".html")){
httpHeader="HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
BufferedReader in = new BufferedReader(new FileReader(new File("WebContent/"+RequstParts[1])));
String line="";
while(null!=(line=in.readLine())){
toReturn+=line;
}
}else if(RequstParts[1].endsWith(".jpg")){
httpHeader="HTTP/1.1 200 OK\r\nContent-Type: image/jpg\r\nContent-Length: 13312\r\n\r\n";
}
}
}
}catch(Exception e){
toReturn+="<br>ERROR: "+e.toString();
return httpHeader+toReturn;
}
return httpHeader+toReturn;
}
public Main(){
try{
ServerSocket listener = new ServerSocket(9090);
System.out.println("HTTP server started!");
while(true){
Socket socket = listener.accept();
System.out.println("\nRecieved Data!");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String message=read(in.readLine());
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
System.out.println("\nSENDING "+message);
out.println(message);
out.println(byt);
out.close();
}
}catch(Exception e){
System.out.println("ERROR "+e.toString());
e.printStackTrace();
}
}
public static void main(String args[]){
new Main();
}
}

This should work:
public byte[] extractBytes (String ImageName) throws IOException {
// open image
File imgPath = new File(ImageName);
BufferedImage bufferedImage = ImageIO.read(imgPath);
// get DataBufferBytes from Raster
WritableRaster raster = bufferedImage .getRaster();
DataBufferByte data = (DataBufferByte) raster.getDataBuffer();
return ( data.getData() );
}
So call this extractBytes on an image (specified as a filename to the method here), and send the returned byte[] using:
OutputStream socketOutputStream = socket.getOutputStream();
socketOutputStream.write(byte[]);
Method code taken from here.

Related

Java Send String(filename) and file over same socket

I would SEND " filename" and "file " through socket , I was able to easily send the file but when I try to send strings eg PrintWriter pw.println ( ) or DataOutputStream or " out.writeUTF ( ) " the file is sent corrupt, I read a lot of questions on StackOverflow but have not found the answer , I'm looking for some example to send strings and files , can you help ?
server
package serverprova;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class ServerProva {
final static int porta=8888;//porta server dove si collegano i client
public static void main(String[] args) throws IOException {
// TODO code application logic here
ServerSocket serverSocket=null;
boolean ascoltando=true;
serverSocket = new ServerSocket(porta);//avvia il server con il numero di porta
Socket s;
BufferedReader br1=null;
// ****** interfaccia f=new interfaccia);
boolean r=true;
BufferedInputStream bis=null;
Scanner sc;
FileOutputStream fout;
while(ascoltando)
{
s=serverSocket.accept();// this socket
String filename="";
String nome_cartella="";
InputStream in = null;
OutputStream out = null;
DataInputStream inString;
inString = new DataInputStream(new BufferedInputStream(s.getInputStream()));
// filename = inString.readUTF();
// nome_cartella = inString.readUTF();
in = s.getInputStream();
//out = new FileOutputStream(nome_cartella+"/"+filename);
out = new FileOutputStream("ciao"+"/"+"asd.jpg");
byte[] b = new byte[20*1024];
int i ;
while((i = in.read(b)) >0){
out.write(b, 0, i);
}
out.close();
in.close();
//inString.close();
s.close();
}
}
}
client
package clientprova;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class ClientProva {
final static int porta=8888;
public static void main(String[] args) throws FileNotFoundException, IOException {
File file;
file = new File("kirlian12.jpg");
InetAddress host = InetAddress.getLocalHost();
Socket sock = new Socket(host.getHostName(), 8888);
DataOutputStream outString = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));
// outString.writeUTF(file.getName());
// outString.writeUTF("rivelatore2");
// outString.flush();
byte[] b = new byte[20*1024];
InputStream in = new FileInputStream(file);
OutputStream out = sock.getOutputStream();
int i ;
while ((i = in.read(b)) >0 ) {
out.write( b , 0 , i);
}
out.close();
in.close();
sock.close();
}
}
when I try to uncomment the commented lines , my files get corrupted
When you wrap InputStream with BufferedInputStream, the latter "takes ownership" of InputStream. This means that when you call readUtf(), BufferedInputStream may read more bytes from InputStream than it is necessary for reading UTF string. So, when you next access InputStream directly, some part of transferred file is missing (because it was previously read into BufferedInputStream's buffer and currently resides there).
inString = new DataInputStream(new BufferedInputStream(s.getInputStream()));
filename = inString.readUTF();
nome_cartella = inString.readUTF();
...
in = s.getInputStream();
while((i = in.read(b)) >0){
You must choose from two alternatives: either to always use raw InputStream OR to always use BufferedInputStream. The same reasoning works also for OutputStream (but you managed to avoid problem by calling flush()).

Client/Server in java : Program gets stuck upon input of second file name

I am developing code to send multiple file names to server side and then making sure server recieves those contents and writes it to a file in its own folder. Its working well when I type first file name but when I type second file name the code kind of gets stuck.
Here is my client code:-
package fileTransfer;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class EchoClient {
public static void main(String[] args) throws UnknownHostException {
try {
Socket clientSock=new Socket("localhost",8888);
//to read from server
BufferedReader br=new BufferedReader(new InputStreamReader(clientSock.getInputStream())); //to read
//to write to server
PrintWriter pw=new PrintWriter(clientSock.getOutputStream(), true);
//for user input
BufferedReader userIn=new BufferedReader(new InputStreamReader(System.in));
BufferedReader fileContent=null;
String str=null;
String fileContentLine=null;
while(true){
if((str=br.readLine()).contains("file name")) //recieve echo from server
System.out.println(str);
str=userIn.readLine(); //read user input
fileContent=new BufferedReader(new FileReader(str));
pw.println(str);
while((fileContentLine=fileContent.readLine()) != null){
pw.println(fileContentLine);
}
while((str=br.readLine())!=null)
System.out.println(str);
pw.flush();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
And here is my server code
package fileTransfer;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class EchoServer {
public static void main(String[] args){
try {
ServerSocket serverSock=new ServerSocket(8888);
System.out.println("Waiting for client");
Socket connectFromClient=serverSock.accept();
File file=null;
//reading data from client
BufferedReader input=new BufferedReader(new InputStreamReader(connectFromClient.getInputStream()));
//will write back to client
PrintWriter pr=new PrintWriter(new OutputStreamWriter(connectFromClient.getOutputStream()));
PrintWriter writeToFile=null;
//sending following statements to client
pr.println("Connection established with server! Give a file name");
pr.flush();
String response;
while(true){
while((response=input.readLine()) != null)
{
if(response==null)
break;
else{
System.out.println(response);
if(response.contains(".txt")){
//file=new File("FromClient.txt");
file=new File("FromClient"+response);
if(!file.exists())
file.createNewFile();
writeToFile=new PrintWriter(file);
}
else{
//writeToFile=new PrintWriter(file);
writeToFile.println(response);
}
pr.println("Echo from server -> " + response);
//System.out.println("Adding these contents to a file");
writeToFile.flush();
pr.flush();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
It´s getting stuck in the client in this lines:
while ((str = br.readLine()) != null) {
System.out.println(str);
}
Waiting for more server lines.
You have to tell the client when to stop from reading file data from socket stream and jump for the next file. For instance you can send before the content of the file the number of bytes you are going to transfer, you read that number of bytes from the stream and then go for the next file.
I post this modifications of your code as an example, as the idea I want to express.
First I get the total linenumber of the file (I suppose text file), and I send it to server, which knows exactly how many lines to read before jumping to next file.
It can be changed to use the total size of the file in bytes, for instance.
This code is no 100% correct, it works, take it as an example.
Client:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class EchoClient {
public static void main(final String[] args) throws UnknownHostException {
try {
final Socket clientSock = new Socket("localhost", 8888);
// to read from server
final BufferedReader br = new BufferedReader(new InputStreamReader(clientSock.getInputStream())); // to
// read
// to write to server
final PrintWriter pw = new PrintWriter(clientSock.getOutputStream(), true);
// for user input
final BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));
BufferedReader fileContent = null;
String str = null;
String fileContentLine = null;
while (true) {
System.out.println("Print filename");
str = userIn.readLine(); // read user input
fileContent = new BufferedReader(new FileReader(str));
pw.println(str);
// first count the line number:
int lineno = 0;
while ((fileContentLine = fileContent.readLine()) != null) {
lineno++;
}
fileContent.close();
//
fileContent = new BufferedReader(new FileReader(str));
pw.println(String.valueOf(lineno));
while ((fileContentLine = fileContent.readLine()) != null) {
pw.println(fileContentLine);
}
for (int i = 0; i < lineno; i++) {
str = br.readLine();
System.out.println(str);
}
pw.flush();
}
} catch (final IOException e) {
e.printStackTrace();
}
}
}
Server:
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class EchoServer {
public static void main(final String[] args) {
try {
final ServerSocket serverSock = new ServerSocket(8888);
System.out.println("Waiting for client");
final Socket connectFromClient = serverSock.accept();
File file = null;
// reading data from client
final BufferedReader input = new BufferedReader(new InputStreamReader(connectFromClient.getInputStream()));
// will write back to client
final PrintWriter pr = new PrintWriter(new OutputStreamWriter(connectFromClient.getOutputStream()));
PrintWriter writeToFile = null;
// sending following statements to client
pr.println("Connection established with server! Give a file name");
pr.flush();
String response;
while (true) {
final String fileName = input.readLine();
file = new File("FromClient" + fileName);
if (!file.exists()) {
file.createNewFile();
}
writeToFile = new PrintWriter(file);
final String sLineNo = input.readLine();
final int lineno = Integer.parseInt(sLineNo);
for (int i = 0; i < lineno; i++) {
response = input.readLine();
System.out.println(response);
// writeToFile=new PrintWriter(file);
writeToFile.println(response);
pr.println("Echo from server -> " + response);
// System.out.println("Adding these contents to a file");
writeToFile.flush();
pr.flush();
}
writeToFile.close();
}
} catch (final IOException e) {
e.printStackTrace();
}
}
}
I Hope to be helpful.

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

Socket write error while using ObjectOutputStream's writeObject method

I am trying to implement FTP protocol using socket programing in java. I am using the ObjectOutputStream to write the data requested to the socket in the server side but i am getting the following error on the console window..
Software caused connection abort: socket write error
Here is the implementation of my program
Server side:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class FTPServer {
public static void main(String[] args) {
try {
#SuppressWarnings("resource")
ServerSocket ss = new ServerSocket(4550);
while(true) {
Socket socket = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
FileInstance file = new FileInstance();
System.out.println(file.srcDir = br.readLine());
System.out.println(file.destDir = br.readLine());
System.out.println(file.filename = file.srcDir.substring(file.srcDir.lastIndexOf("/") + 1));
File f = new File(file.srcDir);
byte[] bytes = new byte[(int)f.length()];
FileInputStream fis = new FileInputStream(f);
fis.read(bytes);
file.FILE_SIZE = bytes.length;
file.fileData = bytes;
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(file);
System.out.println("Success");
oos.close();
fis.close();
br.close();
}
} catch(IOException e) {
System.out.println(e.getMessage());
}
}
}
Client Side:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.Socket;
public class FTPClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("127.0.0.1", 4550);
BufferedReader sbr = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the path of requested file");
String path = sbr.readLine();
System.out.println(path);
PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
System.out.println("Enter Destination");
path = path + "\n" + sbr.readLine();
System.out.println(path);
pw.write(path);
pw.close();
sbr.close();
// receive file
ObjectInputStream ois= new ObjectInputStream(socket.getInputStream());
FileInstance file = (FileInstance)ois.readObject();
ois.close();
if(!new File(file.destDir).exists())
new File(file.destDir).mkdir();
File nfile = new File(file.destDir + "/" + file.filename);
FileOutputStream fos = new FileOutputStream(nfile);
fos.write(file.fileData);
fos.close();
socket.close();
System.out.println("Success");
} catch(IOException e) {
System.out.println(e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
this is the FileInstance class......
import java.io.Serializable;
public class FileInstance implements Serializable {
private static final long serialVersionUID = 1L;
public String destDir;
public String srcDir;
public String filename;
public long FILE_SIZE;
public byte[] fileData;
public String status;
}
You have two problems in FTPClient
You are closing the socket prematurely. At line 22 pw.close() needs to be pw.flush()
Even after you fix the first issue the server will hang. You need to add a newline to the end of the path string you send so the server, using readLine(), can read entire lines; otherwise it waits forever for a complete line that never arrives.
This was trivial to debug in Eclipse. If you want to be a good developer, debugging skills are crucial. Set more than one breakpoint and see what happens. Experiment. Play. Learn.

Sending an image from a Java websocket server

I want to send an image from java websocket server to HTML5 page. When I try to send String client received correct information, but when I want to send image in byteArray i get error:
"WebSocket connection to 'ws://127.0.0.1:9000/' failed: Unrecognized frame opcode: 15"
My server code:
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.MessageDigest;
import javax.imageio.ImageIO;
import javax.xml.bind.DatatypeConverter;
public class WebSocket {
private ServerSocket server;
private Socket sock;
private InputStream in;
private OutputStream out;
public WebSocket() {
}
public void listen(int port) throws IOException {
server = new ServerSocket(port);
sock = server.accept();
server.close();
in = sock.getInputStream();
out = sock.getOutputStream();
}
private void handshake() throws Exception {
BufferedReader br = new BufferedReader(
new InputStreamReader(in, "UTF8"));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out, "UTF8"));
// the first line of HTTP headers
String line = br.readLine();
if (!line.startsWith("GET"))
throw new IOException("Wrong header: " + line);
// we read header fields
String key = null;
// read line by line until we get empty line
while (!(line = br.readLine()).isEmpty()) {
if (line.toLowerCase().contains("sec-websocket-key")) {
key = line.substring(line.indexOf(":") + 1).trim();
}
}
if (key == null)
throw new IOException("No Websocket key specified");
System.out.println(key);
// add key and magic value
String accept = key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
// sha1
byte[] digest = MessageDigest.getInstance("SHA-1").digest(
accept.getBytes("UTF8"));
// and base64
accept = DatatypeConverter.printBase64Binary(digest);
// send http headers
pw.println("HTTP/1.1 101 Switching Protocols");
pw.println("Upgrade: websocket");
pw.println("Connection: Upgrade");
pw.println("Sec-WebSocket-Accept: " + accept);
pw.println();
pw.flush();
}
private void send(String message) throws Exception {
BufferedImage image = ImageIO.read(new File("image.jpg"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
byte[] byteArray = baos.toByteArray();
out.write(byteArray);
out.flush();
}
private void close() {
try {
sock.close();
} catch (IOException e) {
System.err.println(e);
}
}
/** throws Exception, because we don't really care much in this example */
public static void main(String[] args) throws Exception {
WebSocket ws = new WebSocket();
System.out.println("Listening...");
ws.listen(9000);
System.out.println("Handshake");
ws.handshake();
System.out.println("Handshake complete!");
ws.send("I got your message! It's length was");
ws.close();
}
}
and my page in html:
<!DOCTYPE HTML>
<html>
<body>
<button onclick="webs()">WebSocket message</button>
<script>
function webs() {
var ws = new WebSocket("ws://127.0.0.1:9000");
ws.onopen = function(){
console.log("Opened!");
};
ws.onmessage = function(evt){
console.log("Received!");
};
ws.onclose = function(ev){
console.log("Closing connection");
};
ws.onerror = function(ev){
console.log("Connection error: " + ev.reason);
};
}
</script>
</body>
</html>
I quess it is something wrong with this handshake and http headers but I have no idead what.
i think u miss "\r\n" at the end of each line
check this
http://en.wikipedia.org/wiki/WebSocket
hope this helps ..

Categories