| | |
How get Image byte size & transfer Image over Socket?
![]() |
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.
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
----------------------------------------------
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
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
----------------------------------------------
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
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Solved Threads: 0
Ok thanks for ur reply
In client side Applet to create an image and send it to the HTTP server:
In server side(servlet): try to store the coming image to file.
But I dont know much in java.
Please correct me where I am doing wrong.
In client side Applet to create an image and send it to the HTTP server:
Java Syntax (Toggle Plain Text)
import java.applet.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import java.net.*; public class test_client extends Applet { public void init() { Button b = new Button("submit"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { showStatus("button pressed"); try { BufferedImage bi =new BufferedImage(100,100,BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = bi.createGraphics(); Graphics g = getGraphics(); g.setColor(Color.BLUE); g.drawLine(100,100,200,200); g2=(Graphics2D)g; URL url = new URL("http://10.70.70.1:8080/servlet/send_client"); HttpURLConnection con =(HttpURLConnection)url.openConnection(); con.setDoInput(true); con.setDoOutput(true); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "image/jpeg"); ImageIO.write(bi,"JPEG",con.getOutputStream()); con.disconnect(); } catch (Exception e) { showStatus(e.toString()); } } }); add(b); } }
In server side(servlet): try to store the coming image to file.
Java Syntax (Toggle Plain Text)
import java.net.*; import java.io.*; import java.util.*; import java.lang.*; import java.lang.Object; import javax.servlet.*; import javax.servlet.http.*; public class send_client extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { try{ InputStream in = req.getInputStream(); int count; byte[] buffer = new byte[8192]; ServletContext servletContext = getServletContext(); String file = servletContext.getRealPath("new.jpg"); FileOutputStream fis = new FileOutputStream(file); while ((count = in.read(buffer)) > 0){ fis.write(buffer, 0, count); fis.flush(); } fis.close(); } catch (Exception e) { e.printStackTrace(); } } }
But I dont know much in java.
Please correct me where I am doing wrong.
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.
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
----------------------------------------------
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
•
•
Join Date: May 2009
Posts: 2
Reputation:
Solved Threads: 0
java Syntax (Toggle Plain Text)
import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.ServletException; import java.io.*; public class IShareServer extends HttpServlet { public void doPost(HttpServletRequest req,HttpServletResponse res) { try { DataInputStream fromClient = new DataInputStream( req.getInputStream() ); ByteArrayOutputStream baos=new ByteArrayOutputStream(Integer.parseInt(req.getParameter("LEN"))); byte[] buff = new byte[1024]; int cnt = 0; while( (cnt = fromClient.read( buff )) > -1 ) { baos.write(buff, 0, cnt ); } baos.flush(); byte [] check=baos.toByteArray(); saveimage(baos.toByteArray()); baos.close(); fromClient.close(); res.setContentType("text/html"); ServletOutputStream out = res.getOutputStream(); out.flush(); out.close(); } catch(Exception e) { e.printStackTrace(); } } private void saveimage(byte [] buf) { String fileName = "D:/temp1/ServletImage.jpg"; String path = fileName; try { File yourFile = new File(path); FileOutputStream toFile = new FileOutputStream( yourFile ); toFile.write(buf); toFile.flush(); toFile.close(); } catch (Exception e) { } } }
Last edited by Tekmaven; May 30th, 2009 at 9:09 pm. Reason: Code Tags
•
•
•
•
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
Failure is not fatal, but failure to change might be. - John Wooden
![]() |
Similar Threads
- Image in DataGridView (C#)
- Image read and write operations in turbo C (C)
- Working with SQL server's Image data type (ASP.NET)
- How to reduce the size of an image to a standard size say 40 kb (C#)
- Padding and margins (HTML and CSS)
Other Threads in the Java Forum
- Previous Thread: Using a method of an object - cannot find variable
- Next Thread: Need Help!
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class client code compile compiler component database development digit eclipse equation error event fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying pearl problem program programming project qt recursion scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver string superclass swing system text-file thread threads tree variablebinding windows xor






