这个例程实现的功能很简单在applet中有2个按键,按不同的按键能从servlet返回不同的字符串

解决方案 »

  1.   

    你得在applet中重新实现post方法,我正好做了一个,用于上传文件的,将客户端与client贴出来看看。方法是自己实现http的post方法,我只实现了text,与file组件。当然也有其他更好的方法,在
    sun.net.www.http.HttpClient中实现更加方便。
    package com.powerise.power.upload;
    import java.net.*;
    import java.io.*;
    /**
     *  Title: Powerise 上传组件 Description: Copyright: Copyright (c) 2001 Company:
     *  Powerise
     *
     *@author     Charles Tamz
     *@created    2001年9月19日
     *@version    1.0
     */public class JPowerUploadClient {
    private String servletURL;
    private String KeyReturn = "\15\12";
    private String boundary = "-----------------------------7d13b52d5011a";
    /**
     *  Description of the Field
     */
    public final static int
    EXISTS = 3,
    REMOTEERROR = 2,
    LOCALEERROR = 1,
    OK = 0;
    /**
     *  Constructor for the JPowerUploadClient object
     */
    public JPowerUploadClient() {
    }
    /**
     *  Sets the servletURL attribute of the JPowerUploadClient object
     *
     *@param  servletURL  The new servletURL value
     */
    public void setServletURL(String servletURL) {
    this.servletURL = servletURL;
    }
    /**
     *  Gets the servletURL attribute of the JPowerUploadClient object
     *
     *@return    The servletURL value
     */
    public String getServletURL() {
    return servletURL;
    }
    /**
     *  Adds a feature to the SubmitTextField attribute of the JPowerUploadClient
     *  object
     *
     *@param  fieldName   The feature to be added to the SubmitTextField attribute
     *@param  MIME        The feature to be added to the SubmitTextField attribute
     *@param  fieldValue  The feature to be added to the SubmitTextField attribute
     *@return             Description of the Returned Value
     */
    public String addSubmitTextField(String fieldName, String MIME, String fieldValue) { String tmp =
    boundary + KeyReturn
     + "Content-Disposition: form-data; name=\""
     + fieldName + "\"" + KeyReturn + KeyReturn
     + fieldValue + KeyReturn;
    return tmp;
    }
    /**
     *  Adds a feature to the SubmitFileField attribute of the JPowerUploadClient
     *  object
     *
     *@param  fieldName  The feature to be added to the SubmitFileField attribute
     *@param  fileName   The feature to be added to the SubmitFileField attribute
     *@return            Description of the Returned Value
     */
    public String addSubmitFileField(String fieldName, String fileName) {
    String MIME = null;
    String fieldValue = null;
    String ext = getFileExt(fileName);
    if (ext.toLowerCase().equals("txt") || ext.toLowerCase().equals("htm") || ext.toLowerCase().equals("html")
    ) {
    MIME = "text/plain";
    }
    else {
    MIME = "application/octet-stream";
    }
    try { java.io.File file = new java.io.File(fileName);
    FileInputStream reader = new FileInputStream(file);
    byte[] bytes = new byte[(int) file.length()];
    reader.read(bytes);
    fieldValue = new String(bytes, 0);
    System.out.println("Trying to upload "
     + fieldValue.length()
     + "bytes;" + fieldValue.getBytes().length);
    reader.close(); }
    catch (Exception ex) {
    ex.printStackTrace();
    } String tmp =
    boundary + KeyReturn
     + "Content-Disposition: form-data; name=\""
     + fieldName + "\"; filename=\"" + fileName
     + "\"" + KeyReturn
     + "Content-Type: " + MIME
     + KeyReturn + KeyReturn
     + fieldValue + KeyReturn;
    return tmp;
    }
    /**
     *  上传一个文件
     *
     *@param  FileName  文件名
     *@return           返回上传的结果
     */
    public int uploadFile(String FileName) {
    try {
    URL url = new URL(servletURL);
    URLConnection con = url.openConnection();
    con.setUseCaches(false);
    con.setDoOutput(true);
    con.setDoInput(true);
    String sW = "";
    //boundary + KeyReturn;
    sW = sW.concat(addSubmitTextField("PATH", "",
    com.powerise.power.util.CRMUploadProperties.getUploadDirectory()));
    sW = sW.concat(addSubmitFileField("FILE1", FileName));
    sW = sW.concat(boundary) + "----"; byte buf[] =
    //sW.getBytes("");
    new byte[sW.length()];
    sW.getBytes(0, buf.length, buf, 0); con.setRequestProperty("Content-type", "application/octet-stream");
    con.setRequestProperty("Content-length", "" + buf.length);
    DataOutputStream dataOut = new DataOutputStream(con.getOutputStream());
    dataOut.write(buf);
    dataOut.flush();
    dataOut.close(); ObjectInputStream in = new ObjectInputStream(con.getInputStream());
    String s = (String) in.readObject();
    System.out.println(s);
    in.close();
    if (s.equals("exists")) {
    return EXISTS;
    }
    else if (s.equals("error")) {
    return REMOTEERROR;
    }
    else if (s.equals("ok")) {
    return OK;
    }
    }
    catch (Exception e) {
    System.out.println(e);
    return LOCALEERROR;
    }
    return -1;
    }
    /**
     *  Gets the remoteFileExists attribute of the JPowerUploadClient object
     *
     *@param  fileName  Description of Parameter
     *@return           The remoteFileExists value
     */
    boolean isRemoteFileExists(String fileName) {
    return false;
    }
    /**
     *  取得文件扩展名
     *
     *@param  fileName  Description of Parameter
     *@return           The fileExt value
     */
    private String getFileExt(String fileName) {
    String value = new String();
    int start = 0;
    int end = 0;
    if (fileName == null) {
    return null;
    }
    start = fileName.lastIndexOf(46) + 1;
    end = fileName.length();
    value = fileName.substring(start, end);
    if (fileName.lastIndexOf(46) > 0) {
    return value;
    }
    else {
    return "";
    }
    }
    /**
     *@param  args  The command line arguments
     */
    public static void main(String[] args) {
    JPowerUploadClient c = new JPowerUploadClient();
    c.setServletURL("http://localhost:8080/upload/servlet/com.powerise.power.upload.ServletExample");
    c.uploadFile("F:\\DelForEx2.42 for D56.zip");
    }
    }
      

  2.   

    to hicharlie(黑查理):
    谢谢
    能把servlet部分的代码也贴给我吗?
      

  3.   

    con.setRequestProperty("Content-type", "application/octet-stream");
    con.setRequestProperty("Content-length", "" + buf.length);
    DataOutputStream dataOut = new DataOutputStream(con.getOutputStream());
    dataOut.write(buf);
    dataOut.flush();
    dataOut.close();ObjectInputStream in = new ObjectInputStream(con.getInputStream());
    String s = (String) in.readObject();
    System.out.println(s);
    in.close();从以上的代码我看到了客户端以post的方式向servlet输出和接受输入,但servlet是如何响应并回复的呢?
                
      

  4.   

    - Applet一端,用一个Java HTTP-client(如URLConnection)发出POST
    - Servlet一端,overridedoPost方法具体顺序:
    - Applet -> write
    - Servlet-> read + write
     Applet ->  read
      

  5.   


    如果是post发送过来的流,servlet响应与其它post过来的流一样没有什么特殊的。
      

  6.   


    在《Java Servelt编程指南》一书中有类似黑查理写的这个程序的一个例子。servlet端的操作与客户端基本一样:从request.getInputStream()取出DataInputStream来读数据
    从response.getOutputStream()取出DataOutputStream来写回数据记得先response.setContentType("application/octer-stream");
    注意如果写回是对象就不是DataOutputStream而是ObjectOutputStream了:)反正就是这么一回事了,你再参考一下API就可以明白了。其中最大的关键是用到了URLConnection而不是从URL中取stream,并设置URLConnection的setRequestProperty()……
      

  7.   

    to sharetop(天生不笨):
    能否给个servlet端的例程?谢谢
      

  8.   

    没什么呀。你在service方法里操作,先
    DataInputStream in=new DataInputStream(request.getInputStream());
    response.setContentType("application/octer-stream");然后你就读数据了,in.readChar()或是in.readFloat()等等。如果要写数据到客户端,一样,先
    ByteArrayOutputStream byteOut=new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(byteOut);然后你就out.writeBoolean(),out.writeFloat()等等写进去。然后
    byte[] buf = byteOut.toByteArray();
    response.setContentLength(buf.length);ServletOutputStream sout = response.getOutputStream();
    sout.write(buf);
    sout.close();即可,差不多吧,细节你自己补充一下。