实现目的:通过HTTP上传文件(由Client端应用程序上传,由SERVLET接收保存).目前,我在客户端生成了一个流,发送给SERVLET,再从SERVLET的Request中取出这个流,保存成文件.但是我没成功.
这种思路对不对,请大家讨论.
如果可以的话,能否帮忙给一个简单的代码,拜托大家了.网上有用SERVLET上传文件的例子,但是我是想用Client端应用程序上传,由SERVLET接收保存.
         

解决方案 »

  1.   

    呵呵,可以用commons-httpclient,还有cookie功能。
    在服务器端就可以当一般的页面上传文件处理。
      

  2.   

    服务器:
    1.jsp
    <body>
        <form name="_ctl0" method="post" action="TestFileManager.aspx" id="_ctl0" enctype="multipart/form-data">
    <input type="hidden" name="__VIEWSTATE" value="dDwyNTIzNjA5NDU7Oz7rsE3eBYzQHDVtl+aTn96MvQW6PQ==" />        <p>
                <input name="uploadfile1" id="uploadfile1" type="file" size="49" />
                <input type="submit" name="Button1" value="?" id="Button1" />
            </p>
            <p>
                <span id="Label1" style="width:459px;"></span>
            </p>
            <!-- Insert content here -->
        </form>
    </body>
    客户端:
    首先创建一个到服务器http的请求
    HttpRequest request = new HttpRequest("http://服务器/1.jsp");
    第一次使用的是GET方式
    request.setMethod("GET");
    紧接着进行一些请求的属性设置
    request.setRequestHeader("Cache-Control", "no-cache");
    这里保持连接,因为后面还要发送数据到服务器呢
    request.setRequestHeader("Connection", "Keep-Alive");
    下面是一些无关紧要的属性设置了。
    request.setRequestHeader("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
    request.setRequestHeader("Accept-Encoding", "gzip, deflate");
    request.setRequestHeader("Accept-Language", "en-au");
    request.setRequestHeader("Referer", "http://服务器/1.jsp");
    request.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3215; .NET CLR 1.0.3705)");构造好了连接请求,然后连接
    request.connect();紧接着提取Cookie值,在后文的post中可以用到。
    String strCookie = request.getResponseHeader("Set-Cookie");
    strCookie = strCookie.substring(0,strCookie.indexOf(";"));下面通过循环查找,提取__VIEWSTATE的值
    for ( int i = 0; i < nlist.getLength(); i++) {
     node = nlist.item(i);
     strName = getNodeAttributeValue(node,"name");
     if ( strName.equals("__VIEWSTATE") ) {
      strValue = getNodeAttributeValue(node,"value");
      break;
     }
    }往服务器组织发送数据
    DataOutputStream dos = new DataOutputStream(request.getOutputStream());
    dos.writeBytes("-----------------------------"+strBoundary);//这是每个要被发送数据间的间隔
    dos.writeBytes("\r\nContent-Disposition: form-data; name=\"__VIEWSTATE\"");
    dos.writeBytes("\r\n\r\n"+strValue);
    dos.writeBytes("\r\n-----------------------------"+strBoundary);
    这里面是发送文件的部分
    dos.writeBytes("\r\nContent-Disposition: form-data; name=\"uploadfile1\"; filename=\"" + strFileName + "\"");
    dos.writeBytes("\r\nContent-Type: text/xml");
    dos.writeBytes("\r\n\r\n");
    dos.writeBytes(new String(data));
    dos.writeBytes("\r\n-----------------------------"+strBoundary);
    dos.writeBytes("\r\nContent-Disposition: form-data; name=\"Button1\"");
    dos.writeBytes("\r\n上传");
    dos.writeBytes("\r\n-----------------------------"+strBoundary+"--");
    dos.writeBytes("\r\n");
    dos.close();
      

  3.   

    参考http://www.jguru.com/faq/view.jsp?EID=62798