Hi,
Attached below is the file
Server(Testconn) and client-(Httptest)
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.zip.ZipFile;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class HttpTest extends Frame implements ActionListener
{
ZipFile zip = null;
java.awt.List list = null;
//The zip file name.
String filename = null;
MenuItem mOpen, mQuit;
public static void main(String args[])
{
new HttpTest();
}
public HttpTest()
{
setTitle("UnZip Tool");
setLayout(new BorderLayout(3, 3));
add(new Label("Http URL Test: Open a jar/zip file, it will visit your Servlet and go back."),
"North");
list = new java.awt.List(20, true);
add(list, "Center");
MenuBar mb = new MenuBar();
setMenuBar(mb);
Menu mFile = new Menu("File");
mb.add(mFile);
mOpen = new MenuItem ("Open", new MenuShortcut('O'));
mFile.add(mOpen);
mFile.addSeparator();
mQuit = new MenuItem("Quit", new MenuShortcut('Q'));
mFile.add(mQuit);
mOpen.addActionListener(this);
mQuit.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { quit(); }
});
setBounds(100,100, 400, 600);
show();
}
void open()
{
FileDialog f = new FileDialog(this, "Open a Zip File",
FileDialog.LOAD);
f.show();
if(f.getFile() == null)
{
return;
}
try
{
filename = new File(f.getDirectory(), f.getFile()).getAbsolutePath();
//FileInputStream fis = new FileInputStream(filename);
//ZipInputStream zis = new ZipInputStream(fis);
zip = new HTTPComu(new File(filename)).getZipFile();
list.removeAll();
ZipEntry entry;
if (zip == null) {
return;
}
Enumeration enum = zip.entries();
while(enum.hasMoreElements()) {
entry = (ZipEntry)(enum.nextElement());
if(!entry.isDirectory()) {
list.add(entry.getName());
}
}
}
catch(Exception e)
{
System.out.println("Error: Can't open zip file " + f.getFile());
e.printStackTrace();
}
}
void quit()
{
System.exit(0);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == mOpen) open();
else if (source == mQuit) quit();
}
}
class HTTPComu {
File input = null;
String outputFile = null;
public ZipFile getZipFile() {
if (outputFile == null) {
return null;
}
try {
return new ZipFile(outputFile);
} catch (Exception e) {
return null;
}
}
private String name = "Fred's test HTTP Connector!";
private Hashtable hash = new Hashtable();
public HTTPComu(File zip) {
try {
input = zip;
hash.put("hello", " helloValue ....");
hash.put("hello!", "Christmas");
/*
Properties props = new Properties();
FileInputStream in = new FileInputStream("PostTest.properties");
prps.load(in);
props.remove("URL");
*/
URL url = new URL("http://ferret:80/servlet/TestCon");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setAllowUserInteraction(false);
send(connection);
receive(connection);
connection.disconnect();
//output = input;
} catch (Exception e) {
System.out.println("Error: " + e);
e.printStackTrace();
}
}
private void send(HttpURLConnection connection) {
try {
String contentType = "application/x-www-form-upload";
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestProperty("Connection", "keep-alive");
connection.setRequestMethod("POST");
ObjectOutputStream out = new ObjectOutputStream(connection.getOutputStream());
out.writeObject(name);
out.writeObject(hash);
System.out.println("Sent hash table:\n" + hash);
FileInputStream in = new FileInputStream(input);
byte[] data = getFileData(in);
in.close();
out.writeObject(data);
System.out.println("Sending finished!");
out.flush();
out.close();
} catch (Exception e) {
System.out.println("Sedning error : " + e);
}
}
private void receive(HttpURLConnection connection) throws Exception {
try {
//HttpURLConnection.HTTP_CLIENT_TIMEOUT = 4096;
System.out.println("Receiving starts!");
System.out.println("Type: " + connection.getContentType() +
"\tLength: " + connection.getContentLength() +
"\tEncoding: " + connection.getContentEncoding());
ObjectInputStream in = new ObjectInputStream(connection.getInputStream());
System.out.println("Receiving going !");
File output = File.createTempFile("http", ".zip");
outputFile = output.getPath();
output.deleteOnExit();
System.out.println("Create tmp file " + output);
FileOutputStream fileout = new FileOutputStream(output);
Object obj;
obj = in.readObject();
hash = (Hashtable)obj;
System.out.println("Returned hash table:\n" + hash);
obj = in.readObject();
byte[] data = (byte[])obj;
fileout.write(data, 0, data.length);
fileout.close();
in.close();
System.out.println("Receiving finished! " + data.length);
} catch (Exception e) {
System.out.println("Receive error: " + e);
e.printStackTrace();
}
}
public byte[] getFileData(InputStream in)
throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int size = 2048;
byte data[] = new byte[size];
int readedSize;
long count = 0;
while((readedSize = in.read(data, 0, size)) != -1) {
out.write(data, 0, readedSize);
count += readedSize;
}
System.out.println("Reading file! " + count);
out.close();
return out.toByteArray();
}
}
--
import java.io.*;
import java.util.*;
import java.util.zip.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TestCon extends HttpServlet {
File output = null;
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
System.out.println("Do Get in the Echo!");
//doPost(req, res);
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.print("<html>");
out.print("<head><title>Hello World3</title></head>");
out.print("<body>");
out.print("<h1>Hello World" + new Date() + "</h1>");
System.out.println("TestCon doGet!");
out.print("</body></html>");
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
ZipFile zip;
String name;
Hashtable hash;
Object obj;
try {
//ByteArrayOutputStream buffer = new ByteArrayOutputStream();
res.setContentType("text/html");
ObjectInputStream in = new ObjectInputStream(req.getInputStream());
System.out.println("TestCon doPost!");
obj = in.readObject();
name = (String) obj;
obj = in.readObject();
hash = (Hashtable) obj;
output = File.createTempFile("http", ".zip");
output.deleteOnExit();
System.out.println("Create tmp file " + output);
FileOutputStream fileout = new FileOutputStream(output);
obj = in.readObject();
byte[] data = (byte[]) obj;
fileout.write(data, 0, data.length);
System.out.println("Reading Finished! " + data.length);
fileout.close();
//in.close();
System.out.println("Name " + name);
System.out.println("Hash " + hash);
//System.out.println("Length " + length);
////Send back
//ObjectOutputStream out = new ObjectOutputStream(buffer);
res.setBufferSize(data.length + 10240);
ObjectOutputStream out = new ObjectOutputStream(res.getOutputStream());
out.writeObject(hash);
System.out.println("Start to sending back!");
//FileInputStream filein = new FileInputStream(output);
//data = getFileData(filein);
//filein.close();
out.writeObject(data);
System.out.println("Writing data!");
//res.setBufferSize(buffer.size() + 1024);
//res.setContentLength(buffer.size());
out.close();
System.out.println("Sending back finished!" + data.length);
} catch (Exception e) {
System.out.println("Reading data error " + e);
e.printStackTrace();
}
}
public byte[] getFileData(InputStream in)
throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int size = 2048;
byte data[] = new byte[size];
int readedSize;
long count = 0;
while((readedSize = in.read(data, 0, size)) != -1) {
out.write(data, 0, readedSize);
count += readedSize;
}
System.out.println("\tReading file ! " + count);
out.close();
return out.toByteArray();
}
public String getServletInfo() {
return "Echo what it gets from client";
}
}
I hope this will help you.
Thanks
Bakrudeen
I need to Send/Recieve data through http firewall.
Should I use HttpUrlConnection on client and HttpServlet on Server side or manually generate messages for the sockets? How can I start treating request with servlet. Should I identify requests with sessionID or it is better to mantain single connection? Is it possible that Java VM will generate SessionIDs?