/** * Get a connection to the servlet. */ private URLConnection getServletConnection() throws MalformedURLException, IOException { URL urlServlet = new URL(baseurl, "/echo"); System.out.println(urlServlet); HttpURLConnection con = (HttpURLConnection) urlServlet .openConnection(); con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/x-java-serialized-object"); return con; } /** * Send the inputField data to the servlet and show the result in the * outputField. */ private String onSendData(UpFile upfile) { try { // send data to the servlet URLConnection con = getServletConnection(); OutputStream outstream = con.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(outstream); oos.writeObject(upfile.getFile()); oos.writeObject(upfile.getFilePath()); oos.flush(); oos.close(); // receive result from servlet InputStream instr = con.getInputStream(); ObjectInputStream inputFromServlet = new ObjectInputStream( instr); String result = (String) inputFromServlet.readObject(); inputFromServlet.close(); instr.close(); return result; } catch (Exception ex) { ex.printStackTrace(); System.out.println("bad"); return null; } }这个就是我最近的项目中用到的applet和servlet通信的两个method 你可以学着照做
* Get a connection to the servlet.
*/
private URLConnection getServletConnection()
throws MalformedURLException, IOException { URL urlServlet = new URL(baseurl, "/echo");
System.out.println(urlServlet);
HttpURLConnection con = (HttpURLConnection) urlServlet
.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type",
"application/x-java-serialized-object");
return con;
} /**
* Send the inputField data to the servlet and show the result in the
* outputField.
*/
private String onSendData(UpFile upfile) {
try {
// send data to the servlet
URLConnection con = getServletConnection();
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(upfile.getFile());
oos.writeObject(upfile.getFilePath());
oos.flush();
oos.close(); // receive result from servlet
InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(
instr);
String result = (String) inputFromServlet.readObject();
inputFromServlet.close();
instr.close();
return result;
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("bad");
return null;
}
}这个就是我最近的项目中用到的applet和servlet通信的两个method 你可以学着照做