How get Image byte size & transfer Image over Socket?

Reply

Join Date: Feb 2006
Posts: 2,380
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: How get Image byte size & transfer Image over Socket?

 
0
  #11
Oct 19th, 2007
I.E.

You: "Write the code for me."

Me: "NO!"

There is a wealth of information on doing file uploads out there. I do not have an example in front of me, so to give you one I would have to search (Google) for one. And, if someone has to do that anyway, it just as well be you, as it is your assignment.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,380
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: How get Image byte size & transfer Image over Socket?

 
0
  #12
Oct 19th, 2007
As far as your code, from your last post goes, the actual error message would help. Otherwise your "new.jpg" file will be written in the current working directory of the Application server, and that can vary depending on how your server was started. Did you try running a find to see if you could find it?
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 6
Reputation: sharmila.al is an unknown quantity at this point 
Solved Threads: 0
sharmila.al sharmila.al is offline Offline
Newbie Poster

Re: How get Image byte size & transfer Image over Socket?

 
0
  #13
Oct 19th, 2007
Ok thanks for ur reply

In client side Applet to create an image and send it to the HTTP server:
  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.awt.image.*;
  5. import java.io.*;
  6. import java.net.*;
  7. public class test_client extends Applet {
  8. public void init() {
  9. Button b = new Button("submit");
  10. b.addActionListener(new ActionListener() {
  11. public void actionPerformed(ActionEvent ae) {
  12. showStatus("button pressed");
  13. try {
  14. BufferedImage bi =new BufferedImage(100,100,BufferedImage.TYPE_INT_ARGB);
  15. Graphics2D g2 = bi.createGraphics();
  16. Graphics g = getGraphics();
  17. g.setColor(Color.BLUE);
  18. g.drawLine(100,100,200,200);
  19. g2=(Graphics2D)g;
  20. URL url = new URL("http://10.70.70.1:8080/servlet/send_client");
  21. HttpURLConnection con =(HttpURLConnection)url.openConnection();
  22. con.setDoInput(true);
  23. con.setDoOutput(true);
  24. con.setRequestMethod("POST");
  25. con.setRequestProperty("Content-Type", "image/jpeg");
  26. ImageIO.write(bi,"JPEG",con.getOutputStream());
  27. con.disconnect();
  28. } catch (Exception e) {
  29. showStatus(e.toString());
  30. }
  31. }
  32. });
  33. add(b);
  34. }
  35. }

In server side(servlet): try to store the coming image to file.

  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.lang.Object;
  6. import javax.servlet.*;
  7. import javax.servlet.http.*;
  8. public class send_client extends HttpServlet {
  9. public void doPost(HttpServletRequest req, HttpServletResponse res)
  10. throws ServletException, IOException
  11. {
  12. try{
  13. InputStream in = req.getInputStream();
  14. int count;
  15. byte[] buffer = new byte[8192];
  16. ServletContext servletContext = getServletContext();
  17. String file = servletContext.getRealPath("new.jpg");
  18. FileOutputStream fis = new FileOutputStream(file);
  19. while ((count = in.read(buffer)) > 0){
  20. fis.write(buffer, 0, count);
  21. fis.flush();
  22. }
  23. fis.close();
  24. }
  25. catch (Exception e)
  26. {
  27. e.printStackTrace();
  28. }
  29. }
  30. }


But I dont know much in java.

Please correct me where I am doing wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,380
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: How get Image byte size & transfer Image over Socket?

 
0
  #14
Oct 19th, 2007
I still don't see what error is or is not happening.

What error are you getting? What is getting written into the file (soemthing is, if you are not getting an error.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 6
Reputation: sharmila.al is an unknown quantity at this point 
Solved Threads: 0
sharmila.al sharmila.al is offline Offline
Newbie Poster

Re: How get Image byte size & transfer Image over Socket?

 
0
  #15
Oct 22nd, 2007
Am not getting any error. But i didnt get file. I searched all directory. But file is not available.

Thanks for your reply.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 2
Reputation: sandeepdhankar is an unknown quantity at this point 
Solved Threads: 0
sandeepdhankar sandeepdhankar is offline Offline
Newbie Poster

Re: How get Image byte size & transfer Image over Socket?

 
0
  #16
May 30th, 2009
  1. import javax.servlet.*;
  2. import javax.servlet.http.*;
  3. import javax.servlet.ServletException;
  4. import java.io.*;
  5.  
  6. public class IShareServer extends HttpServlet {
  7.  
  8. public void doPost(HttpServletRequest req,HttpServletResponse res)
  9. {
  10. try
  11. {
  12. DataInputStream fromClient = new DataInputStream( req.getInputStream() );
  13. ByteArrayOutputStream baos=new ByteArrayOutputStream(Integer.parseInt(req.getParameter("LEN")));
  14. byte[] buff = new byte[1024];
  15. int cnt = 0;
  16. while( (cnt = fromClient.read( buff )) > -1 )
  17. {
  18. baos.write(buff, 0, cnt );
  19. }
  20. baos.flush();
  21. byte [] check=baos.toByteArray();
  22. saveimage(baos.toByteArray());
  23. baos.close();
  24. fromClient.close();
  25. res.setContentType("text/html");
  26. ServletOutputStream out = res.getOutputStream();
  27. out.flush();
  28. out.close();
  29. }
  30. catch(Exception e) { e.printStackTrace(); }
  31. }
  32.  
  33. private void saveimage(byte [] buf)
  34. {
  35. String fileName = "D:/temp1/ServletImage.jpg";
  36. String path = fileName;
  37. try
  38. {
  39. File yourFile = new File(path);
  40. FileOutputStream toFile = new FileOutputStream( yourFile );
  41. toFile.write(buf);
  42. toFile.flush();
  43. toFile.close();
  44.  
  45. }
  46. catch (Exception e) { }
  47. }
  48.  
  49. }
Last edited by Tekmaven; May 30th, 2009 at 9:09 pm. Reason: Code Tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 469
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: How get Image byte size & transfer Image over Socket?

 
0
  #17
May 31st, 2009
Originally Posted by Joncamp View Post
I cant read from a file stream, because the client application is an Applet (browser applet) in this client/server image transrfer program. You can't access files from an Applet in Java.

I can paste an Image into the Applet and cast it to a BufferedImage object, but I don't know how to send the BufferedImage object over a Socket. I need the size of the BufferedImage object so I can notify the server Java application of how many bytes it is to expect on the receiving end.

Jon
You can access files from an applet in java with signed applets or policy based permission.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC