How to download an image using Java Socket from a remote Http server? -


i have been trying implement simple http client using java socket. in program requesting image server , trying copy requested jpeg image on local machine. have managed construct request , received desired content. have separated response header , content. problem when write bytes using fileoutputstream .jpeg file , after writing when open file in image viewer (like picasa) image seems invalid. here entire code. can plz tell me what's wrong code? why image invalid?

import java.io.bufferedinputstream; import java.io.bufferedreader; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.printwriter; import java.net.socket;  import sun.misc.ioutils;   public class imagecopy {    public static void main(string args[]) throws ioexception{                string host = "www.uni-koblenz-landau.de";   //declare host name                string resourceloc = "/images/starts-c-ko.jpg"; //declare specific pagename of                httprequester req = new httprequester();                req.request(host, resourceloc); //send request mentiong host , pagename      }   }  class httprequester{     public void request(string host, string resourceloc) throws ioexception{          socket httpsocket = new socket(host, 80); //create request port 80          printwriter writer = new printwriter(httpsocket.getoutputstream());          fileoutputstream foutstream = new fileoutputstream("e:\\quora.jpeg"); //creating file hold output stream           // building header fields         string protocol = "get /" +resourceloc+" http/1.1";         string connection ="connection: close";         string acceptedlanguage ="accept-language: de,en;q=0.7,en-us;q=0.3";         string headerend = "";         string hostheader = "host: www.uni-koblenz-landau.de";          // writing headers outputstream         writer.println(protocol);        writer.println(hostheader);        writer.println(connection);        writer.println(acceptedlanguage);        writer.println(headerend);         writer.flush();        // request sent         bufferedinputstream reader = new bufferedinputstream(httpsocket.getinputstream());         inputstream is;         int bytecode =0;        char ch ;            stringbuilder builder = new stringbuilder();         while((bytecode=reader.read())!=-1)        {            builder.append((char)bytecode);           // system.out.print((char)bytecode);         }         string text = builder.tostring();        // sub[0] supposed contain header , sub[1] should contain bytes of           image         string[] sub = text.split("\r\n\r\n");        system.out.println(sub[0]);         byte[] byts = sub[1].getbytes();         for(int i=0;i<byts.length;i++){            foutstream.write(bytecode);        }        system.out.println(byts.length);     }  }   

please try working code first:

import java.io.fileoutputstream; import java.io.inputstream; import java.io.outputstream; import java.net.httpurlconnection; import java.net.url;  public class urltest {    private static void sendget() throws exception {      string url = "http://www.uni-koblenz-landau.de/images/starts-c-ko.jpg";      url obj = new url(url);     httpurlconnection con = (httpurlconnection) obj.openconnection();      // optional default     con.setrequestmethod("get");      //add request header     con.setrequestproperty("user-agent", "mozilla/5.0");      int responsecode = con.getresponsecode();     system.out.println("\nsending 'get' request url : " + url);     system.out.println("response code : " + responsecode);      inputstream in = con.getinputstream();     outputstream out = new fileoutputstream("/users/ravikiran/desktop/abc.jpg");     try {       byte[] bytes = new byte[2048];       int length;        while ((length = in.read(bytes)) != -1) {         out.write(bytes, 0, length);       }     } {       in.close();       out.close();     }   }    public static void main(string[] args) throws exception {     sendget();   } } 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -