手上刚好有一个反过来的例子(对象序列化的),稍改一下,就象你说的了,你自己改吧.
package urltest;import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */public class ServletTest extends HttpServlet {    public ServletTest() {
    }    public void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException ,IOException
    {
      doPost(request,response);
    }    public void doPost(HttpServletRequest request, HttpServletResponse response)  throws ServletException ,IOException
    {
        try {
            System.out.println("Start write object...");
            MyModel model = new MyModel();
            model.setField("This is a test!");            ObjectOutputStream os = new ObjectOutputStream(
                                        new BufferedOutputStream(
                                            response.getOutputStream()));            os.writeObject(model);
            os.flush();
            os.close();        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}
package urltest;import java.io.*;
import java.net.*;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */public class Receiver {    public Receiver() {
    }    public void receive() {
        try {
            URL url = new URL( "http://localhost:8080/aaa" );
            URLConnection conn = url.openConnection();            conn.setDoOutput( true );
            conn.setDoInput( true );
            conn.setUseCaches( false );            System.out.println("Receive begin...");            ObjectInputStream is = new ObjectInputStream(
                                        new BufferedInputStream(
                                            conn.getInputStream()));            MyModel model = (MyModel)is.readObject();            model.printField();            System.out.println("Receive end...");
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }    public static void main(String args[]) {
        Receiver receiver = new Receiver();
        receiver.receive();
    }}

解决方案 »

  1.   

    我用:
    URL url = new URL( "http://localhost:8080/servlet/Myservlet" );
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setDoOutput( true );
    conn.setRequestMethod("POST");
    conn.setUseCaches( false );
    BufferedOutputStream bos = new BufferedOutputStream(con.getOutputStream());
    bos.write(...);
    这样的方式向servlet上传文件,但是servlet没有响应,请问为什么呢?
    然而,下面的文件下载程序则是成功的:
    URL url = new URL( "http://localhost:8080/servlet/Myservlet" );
    InputStream is = url.openStream();
    BufferedInputStream bos = new BufferedInputStream(is);
    bos.read(...);
     
      

  2.   

    你在服务器端就必须先从response读输入流以响应客户端,而不是输出。
    还有,我不清楚conn.setRequestMethod("POST");会对服务器端造成什么影响,
    是否先加一个信息头,没试过。
      

  3.   

    请问,您的意思是应该这样写吗:
    URL url = new URL( "http://localhost:8080/servlet/Myservlet" );
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setDoOutput( true );
    conn.setRequestMethod("POST");
    conn.setUseCaches( false );
    //********
    InputStream is = conn.getInputStream();
    //*******
    BufferedOutputStream bos = new BufferedOutputStream(con.getOutputStream());
    bos.write(...);
    您这样做成功过吗???
    万分感谢!!
      

  4.   

    你客户端应该没没问题,我是指在服务器端先要读数据.
    我前面说"先从response读输入流",错了,是request,sorry.
    你doPost中改一下试试.
    public void doPost(HttpServletRequest request, HttpServletResponse response)  throws ServletException ,IOException
    {
        try 
        {
            ByteArrayOutputStream byteArrOut = new ByteArrayOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(byteArrOut);        BufferedInputStream bis = new BufferedInputStream(request.getInputStream());        int bit;
            while((bit = bf.read()) != -1)
            {
                bos.write(bit);
            }        bos.flush();
            bos.close();
            bis.close();       //do some with byteArrOut
           //...
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
      

  5.   

    又写错了一点点,贴出才看到.
    while((bit = bf.read()) != -1)

    while((bit = bis.read()) != -1)