P
pcouas
Hi,
I try to Serialise an Applet on my WebServer, i convert Object to
ByteArray, before sending it to webserver.
There is 2050 bytes in byte array on my applet before sending it, and
2062 bytes in byte array on tomcat !
Where is my mistake ?
Regards
Philippe
Applet
void WriteApplet(String filename)
{
ObjectOutputStream out;
System.out.println("Debut Test WRITE APPLET...");
//Serialisation de la frame
try
{
String m_URL=new
String("http://172.16.0.101:8080/test/servlet/servletgraph.XXServlet");
String string4=m_URL;
System.out.println("w01 "+string4);
URL u=new URL(string4);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
oos.flush();
oos.close();
byte b[] = baos.toByteArray();
HttpURLConnection urlc = (java.net.HttpURLConnection)
u.openConnection();
// Set the Post (true) or Get (false) method.
urlc.setDoOutput(true);
urlc.setDoInput(true);
urlc.setUseCaches(false);
urlc.setAllowUserInteraction(false);
urlc.setRequestProperty("Content-Type",
"application/octet-stream");
String lengthString = String.valueOf(baos.size());
urlc.setRequestProperty("Content-Length", lengthString);
System.out.println("Object is avant ecriture " + lengthString + "
bytes");
out = new ObjectOutputStream(urlc.getOutputStream());
out.write(b);
if (urlc.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.out.println("Error...");
} else {
System.out.println("I have now access to the stream...");
// blaha balaaha blaahahaha
}
out.flush();
out.close();
System.out.println("Fin Test WRITE APPLET...");
}
catch (IOException ioe) {
System.err.println(ioe);
ioe.printStackTrace();
}
}
-------------------------------------------------------------------------
Servlet
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
System.out.println("XXServlet doPost X1 "+new Date());
boolean debug=true;
InputStream in = req.getInputStream();
byte[] bufimage=null;
bufimage = new byte[4 * 1024]; // 4K buffer
int len;
RandomAccessFile f = new
java.io.RandomAccessFile(req.getRealPath("/")+"xxservlet", "rw");
int size = req.getIntHeader("content-length");
bufimage = new byte[size]; // 4K buffer
if(debug) System.out.println("XXServlet bytearray02 "+size);
int nb=0;
while ((len = in.read(bufimage, 0, bufimage.length)) != -1)
{
if(debug) System.out.println("XXServlet bytearray03
content-length "+len);
f.write(bufimage, 0, len); //Fichier
nb=+len;
}
if(debug) System.out.println("XXServlet bytearray05 "+nb);
f.close();
if(debug) System.out.println("XXServlet write04 ");
}
}
I try to Serialise an Applet on my WebServer, i convert Object to
ByteArray, before sending it to webserver.
There is 2050 bytes in byte array on my applet before sending it, and
2062 bytes in byte array on tomcat !
Where is my mistake ?
Regards
Philippe
Applet
void WriteApplet(String filename)
{
ObjectOutputStream out;
System.out.println("Debut Test WRITE APPLET...");
//Serialisation de la frame
try
{
String m_URL=new
String("http://172.16.0.101:8080/test/servlet/servletgraph.XXServlet");
String string4=m_URL;
System.out.println("w01 "+string4);
URL u=new URL(string4);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(this);
oos.flush();
oos.close();
byte b[] = baos.toByteArray();
HttpURLConnection urlc = (java.net.HttpURLConnection)
u.openConnection();
// Set the Post (true) or Get (false) method.
urlc.setDoOutput(true);
urlc.setDoInput(true);
urlc.setUseCaches(false);
urlc.setAllowUserInteraction(false);
urlc.setRequestProperty("Content-Type",
"application/octet-stream");
String lengthString = String.valueOf(baos.size());
urlc.setRequestProperty("Content-Length", lengthString);
System.out.println("Object is avant ecriture " + lengthString + "
bytes");
out = new ObjectOutputStream(urlc.getOutputStream());
out.write(b);
if (urlc.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.out.println("Error...");
} else {
System.out.println("I have now access to the stream...");
// blaha balaaha blaahahaha
}
out.flush();
out.close();
System.out.println("Fin Test WRITE APPLET...");
}
catch (IOException ioe) {
System.err.println(ioe);
ioe.printStackTrace();
}
}
-------------------------------------------------------------------------
Servlet
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
System.out.println("XXServlet doPost X1 "+new Date());
boolean debug=true;
InputStream in = req.getInputStream();
byte[] bufimage=null;
bufimage = new byte[4 * 1024]; // 4K buffer
int len;
RandomAccessFile f = new
java.io.RandomAccessFile(req.getRealPath("/")+"xxservlet", "rw");
int size = req.getIntHeader("content-length");
bufimage = new byte[size]; // 4K buffer
if(debug) System.out.println("XXServlet bytearray02 "+size);
int nb=0;
while ((len = in.read(bufimage, 0, bufimage.length)) != -1)
{
if(debug) System.out.println("XXServlet bytearray03
content-length "+len);
f.write(bufimage, 0, len); //Fichier
nb=+len;
}
if(debug) System.out.println("XXServlet bytearray05 "+nb);
f.close();
if(debug) System.out.println("XXServlet write04 ");
}
}