偶尔无法上传zip文件

时间:2009-06-16 15:11:01

标签: java https

Tomcat服务器在美国运行。我使用Java的HTTPURLConnection连接到中国的服务器。请参阅下面的tomcat服务器端中的客户端使用的代码段和https连接器配置。

<!-- Define a SSL Coyote HTTP/1.1 Connector on port 8443 -->
    <Connector acceptCount="100" clientAuth="false" connectionTimeout="-1" debug="4" disableUploadTimeout="true" enableLookups="false" keystoreFile="conf/server.keystore" keystorePass="passw47d" maxSpareThreads="75" maxThreads="150" minSpareThreads="25" port="443" scheme="https" secure="true" sslProtocol="TLS" useBodyEncodingForURI="true"/>


    URL url=new URL(urlString);
    HttpsURLConnection connection=null;
    try
    {
        connection=(HttpsURLConnection)url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/zip");
        connection.setRequestProperty("Transfer-Encoding", "chunked" );
        connection.setChunkedStreamingMode(4096);
        connection.connect();
        sout=new BufferedOutputStream(connection.getOutputStream());
        break;
    }
    catch(FileNotFoundException exc)
    {
        throw exc;
    }
    bis=new FileInputStream(zipfile);

    int i;    
    byte bytes[]=new byte[4096]; 
    while((i=bis.read(bytes))!=-1)
    {
        sout.write(bytes,0,i);
        sout.flush();
    }  
    sout.close();
    bis.close();

客户端大多数时间成功上传zip文件。有时,客户端程序会抛出以下异常。

java.io.IOException: Error writing request body to server
at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.checkError(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.write(Unknown Source)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.flush(Unknown Source)
at SendFiles.sendNowThruHttp(SendFiles.java:449)
at SendFiles.run(SendFiles.java:180)

可能是什么问题?

1 个答案:

答案 0 :(得分:1)

可能是网络超时。但可以肯定的是,看看服务器日志文件。它们也将包含错误消息。

我还建议你看一下HttpClient Java library,这使得这些事情变得更加简单可靠。有关示例,请参阅this article(接近结尾)。