URLConnection ? getOutputStream()?我能想到的也是这个做法哦。 你的为什么不行呢?难道你们使用了代理服务器?如果是,你需要设置代理服务器的用户名和密码!如果内网服务器可以用URLConnection访问外网,那还有什么问题呢?
POST过去就行了!比如直接wite(byte)传过去,那面使用 request.getInputStream() 直接读取,然后保存就行了。

解决方案 »

  1.   

    先感谢老紫竹前辈的开导.
    我贴个修改后的源吗码出来吧.还是有些毛病.
    客户端:
    public synchronized String uploadfile(final String strfile,
    final String strurl) { String result = null; try {
    URL url = new URL(strurl + "?filename=" + strfile);
    System.out.println(strurl + "?filename=" + strfile);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    conn.setRequestProperty("enctype", "multipart/form-data");
    conn.setRequestProperty("contenttype", "charset=gbk");
    conn.setRequestMethod("POST"); // 上传xml文件
    DataInputStream file = new DataInputStream(new FileInputStream
                            (new File(strfile)));
    OutputStream out = conn.getOutputStream();
    int bytesout = 0;
    byte[] bufferout = new byte[8192];
    while ((bytesout = file.read(bufferout, 0, 8192)) != -1) {
    out.write(bufferout, 0, bytesout);  }
    out.flush();
    out.close(); // 获得上传的结果
    InputStream isresult = conn.getInputStream();
    byte[] buffer = new byte[isresult.available()];
    isresult.read(buffer);
    result = new String(buffer);
    System.out.print(result);
    isresult.close(); } catch (Exception e) {
    e.printStackTrace();
    result = "error";
    System.out.println(e.getMessage());
    }
    return result;
    }
    //以下是测试POST
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String file = request.getParameter("filename");
    download uxf = new download();
    System.out.println("上传文件结果");
    //局域网内做测试 本地C盘2222.txt,POST给239机器下sendTest的方法urltest
    String sb = uxf.uploadfile
    ("C:\\2222.txt","http://192.168.3.239:8083/sendTest/urltets");
    System.out.println(sb);
    }服务端: urltest.java
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { System.out.println(this.getClass());
                    response.setContentType("text/html");
    PrintWriter outresult = null;
    try {
    outresult = response.getWriter();
    } catch (IOException ioe) {
    } String savefile = "C:\\22.txt";//暂时在服务端保存在C盘下22.txt;
    String uploadreult = "error";
    int reqstreamlen = request.getContentLength();
    BufferedReader readfile = null;
    BufferedWriter writefile = null; try {
    InputStream is = request.getInputStream();
    OutputStream os = new FileOutputStream(savefile);
                            int bytesread = 0;
    byte[] buffer = new byte[8192];
    while ((bytesread = is.read(buffer, 0, 8192)) != -1) {
    os.write(buffer, 0, bytesread);// 传过来的文件写入文件
    // system.out.println(new string(buffer));
    }
    os.flush();
    os.close();
    is.close(); } catch (UnsupportedEncodingException e) {
    uploadreult = "unsupportedencodingexception";
    log.info(e.getMessage());
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    uploadreult = "filenotfoundexception";
    log.info(e.getMessage());
    e.printStackTrace();
    } catch (IOException e) {
    uploadreult = "ioexception";
    log.info(e.getMessage());
    e.printStackTrace();
    } String uploadfile = (String) request.getParameter("filename");
    System.out.print(uploadfile);
    File file = new File(savefile);
    log.info(String.valueOf(file.length()));
    log.info(String.valueOf(reqstreamlen));
    if (file.exists() && (file.length() == reqstreamlen)) {
    uploadreult = "success";

                    log.info(request.getRemoteAddr() + "上传文件:" + uploadfile + "   成功");
    } else {
    uploadreult = "error";

                    log.info(request.getRemoteAddr() + "上传文件:" + uploadfile + "   失败");
    } outresult.println(uploadreult);
    outresult.flush();
    outresult.close(); }
    在本机做测试的时候,客户端InputStream isresult = conn.getInputStream();时
    进入SERVER端很顺利.但是在局域网内做测试的时候,不会进入239机器的服务端代码中.
    ~~~~~~~~这是为什么呢~~~~~~~~.大家帮帮忙~万分感谢了