Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: image url cannot be converted to bytes

  1. #1
    Junior Member
    Join Date
    Jan 2018
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default image url cannot be converted to bytes

    I want to get an image from a url, convert it into bytes and encrypt it using AES. However, when I try to compile the code for my Server, I get this error:
        Server1.java:77: error: cannot find symbol
                                    plainpic = Base64.getUrlEncoder().encodeToString(myimage.getBytes());
     
          symbol:   method getBytes()
          location: variable myimage of type URL
    Why is this so? Below is the code for my server which generates an AES key and tries to convert a image url to bytes.

    Server1.java
        public class Server1 { 
        public static void main (String [] args ) throws InvalidKeyException, IOException, Exception { 
    		ServerSocket serverSocket = new ServerSocket(15123);
    		Socket socket = null;
    		//start of AES
    		//Generate AES Key
    		int sizeofkey = 128;
    		KeyGenerator kg = KeyGenerator.getInstance("AES");
    		//initialize with sizeofkey
    		kg.init(sizeofkey);
    		Key mykey = kg.generateKey();
    		System.out.println("Key Generated");
    		//create cipher object & initialize with generated key
    		Cipher cipher = Cipher.getInstance("AES/ECB/PCKS5PADDING");
    		cipher.init(Cipher.ENCRYPT_MODE, mykey);
     
    		while(true)	{
    			socket = serverSocket.accept();
    			System.out.println("Accepted connection : " + socket.getRemoteSocketAddress().toString() + " <-> /127.0.0.1:15123" );
    			DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
     
    			// get the image from a webcam
    			URL myimage = new URL("http://183.76.13.58:80/SnapshotJPEG?Resolution=640x480");
     
     
    			DataInputStream in = null;
     
    			String plainpic = null;
    			try	{
    				in = new DataInputStream(myimage.openStream()); 
     
                    //Error
    				plainpic = Base64.getUrlEncoder().encodeToString(myimage.getBytes());
    			} 	       
    			catch (Exception ee)	{
    				System.out.println("Check internet connection please");
    				socket.close(); 
    				return;
    			}          
    			DateFormat dateFormat = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
    			Date date = new Date();
    			System.out.println("Sending image " + dateFormat.format(date));
     
    			try	{
    				while (true) {
    					dos.writeByte(in.readByte()); 
    				} 
    			}
    			catch (EOFException ee)	{ 
    				System.out.println("-------------- Done ----------"); 
    				in.close();
    			}
     
    			dos.flush();
    			dos.close();
    			socket.close();
        }
     
          } 
        }
    Last edited by wei123; February 13th, 2018 at 09:10 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: image url cannot be converted to bytes

    Server1.java:77: error: cannot find symbol
    plainpic = Base64.getUrlEncoder().encodeToString(myimage.getB ytes());

    symbol: method getBytes()
    location: variable myimage of type URL
    The compiler can not find a method named getBytes() for the class URL.
    Try reading the image file into a byte array that can be passed to the encode method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2018
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: image url cannot be converted to bytes

    Is this fine?
    			is = myimage.openStream ();
    			byte[] byteChunk = new byte[4096]; 
    			int n;
     
    			while ( (n = is.read(byteChunk)) > 0 ) {
    			baos.write(byteChunk, 0, n);
    				}
    				plainpic=Base64.getUrlEncoder().encodeToString(baos.toByteArray());

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: image url cannot be converted to bytes

    Did you try it?
    What happened?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2018
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: image url cannot be converted to bytes

    Yup, it worked! I printed the string and the output was what I wanted.

Similar Threads

  1. How display captcha image from url link [J2ME]
    By meomap007 in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: August 3rd, 2018, 12:49 AM
  2. incompatible types: void cannot be converted to String
    By Nomi in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 28th, 2014, 04:43 AM
  3. Url Image stream? Getting an Image url.
    By turtlemaster in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 30th, 2012, 09:43 AM
  4. ImageIcon Erratically Loading URL Image
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 3rd, 2011, 04:05 PM

Tags for this Thread