ES Holidays

DE Holidays

BR Holidays

Thursday, December 30, 2010

I want to read a image from webpage using socket..., plzz help me!!!...,?

here is the code.....,


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package socket_application;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
*
* @author suresh
*/
public class Connect_API {

public static Socket s = null;
public static String bHost=null;
public static int port=0;
public static String bData=null;

public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the host name");
bHost = br.readLine();
System.out.println("Enter Port number");
String port1 = br.readLine();
port = Integer.parseInt(port1);
System.out.println("Enter Data");
bData = br.readLine();
connection((String)bHost, port);
setData(s, (String)bData,bHost);
getData(s);
close(s);
}

private static Socket connection(String bHost, Integer port) throws UnknownHostException, IOException {
StringBuffer str=new StringBuffer();
str.append(bHost);
s = new Socket(str.toString(), port);
if(s.isConnected())
{
System.out.println("Connected!!");
}
return s;
}

private static int getData(Socket s) {
// private void getData() {
int ret = 0;
byte[] buffer=null;
try {
//String text = "";
int bytes_read = 0;
InputStream from_server = s.getInputStream();
buffer = new byte[700000];
System.out.println("********************…
while ((bytes_read = from_server.read(buffer)) != -1) {
System.out.write(buffer, 0, bytes_read);
}
System.out.println("********************…
} catch (IOException e) {
System.out.println("" + e.getMessage());
ret = -1;
}
return ret;
}


private static void setData(Socket s, String bData,String bHost) {
try {
PrintWriter to_server = new PrintWriter((s.getOutputStream()), true);
String data=bData;
//blogs/big-optometry-blog/eiffel-tower-…
//http://blogimages.flashedcoder.com/images/mywallpaperz.com/101506100102.jpg
//www.opticianonline.net
StringBuffer str1=new StringBuffer();
data="GET /"+data+" HTTP/1.0\nConnection: close\nHost: "+bHost+"\n\n";
str1.append(data);

//to_server.print(str1.toString());
to_server.write(str1.toString());
to_server.flush();
//System.out.println(""+bData);
System.out.println("inside setdata method");
} catch (IOException e) {
System.out.println("" + e.getMessage());
}
}

private static void close(Socket s) throws IOException {
s.close();
System.out.println("Connection closed!!!");
}
}



my output is :
--------------------------------------…

Enter the host name
www.opticianonline.net
Enter Port number
80
Enter Data
blogs/big-optometry-blog/eiffel-tower-…
Connected!!
inside setdata method
*************************************
HTTP/1.1 200 OK
Date: Wed, 17 Mar 2010 07:39:46 GMT
Server: Apache/2.2.3 (Red Hat) mod_fastcgi/2.4.6 PHP/5.1.6 mod_perl/2.0.4 Perl/v5.8.8
Last-Modified: Thu, 20 Nov 2008 15:55:39 GMT
ETag: "274e45-16af2-f5c6d4c0"
Accept-Ranges: bytes
Content-Length: 92914
Connection: close
Content-Type: image/jpeg

����



how do i get the binary data of that image here so that i can make into a picture by copying that..,







Answer :
Assuming you are running your program from the command line, you can't display the image there because your command line interface (shell prompt) can't display images.

Just read the content as you would when you are reading a string, then save the content to a file. When you open the file with a program that can display images (web browser, ms paint, photoshop, etc), it should then come up as the image there.

No comments:

Post a Comment

IT Holidays

USA Holidays

JP Holidays

UK Holidays