本帖最后由 toby_s_j 于 2010-11-12 13:56:15 编辑

解决方案 »

  1.   

    网上搜到的例子,是把一个字符串从客户端传到服务端的,上传文件也可以用同样的方法。上传端处理:import java.net.*;
    import java.io.*;public class HTTP_post
    {
        static URL u;
        public static void main(String args[])
        {
    String s=URLEncoder.encode("A Test string to send to a servlet"); try
    {
        HTTP_post post = new HTTP_post();
        post.u = new URL("http://myhost/servlet");
          
        // Open the connection and prepare to POST
        URLConnection uc = u.openConnection();
        uc.setDoOutput(true);
        uc.setDoInput(true);
        uc.setAllowUserInteraction(false);     DataOutputStream dstream = new DataOutputStream(uc.getOutputStream());
          
        // The POST line
        dstream.writeBytes(s);
        dstream.close();     // Read Response
        InputStream in = uc.getInputStream();
        int x;
        while ( (x = in.read()) != -1)
        {
    System.out.write(x);
        }
        in.close();     BufferedReader r = new BufferedReader(new InputStreamReader(in));
        StringBuffer buf = new StringBuffer();
        String line;
        while ((line = r.readLine())!=null) {
    buf.append(line);
        } }
    catch (IOException e)

        e.printStackTrace(); // should do real exception handling
    }
        }
    }
    接受端处理:例如:     InputStream in = request.getInputStream();
        BufferedReader r = new BufferedReader(new InputStreamReader(in));
        StringBuffer buf = new StringBuffer();
        String line;
        while ((line = r.readLine())!=null) {
    buf.append(line);
        }
        String s = buf.toString();
      

  2.   

    URL url = null;
            try
            {
                url = new URL(imageUrl);
                BufferedImage image = ImageIO.read(url);
                ImageIO.write(image, imageType, new File(localSaveImagePath));
            }
            catch (MalformedURLException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }供参考!