2009-06-16 87 views
1

Tomcat服務器在美國運行。我使用Java的HTTPURLConnection連接到來自中國的服務器。請參閱下面的tomcat服務器端的客戶端使用的代碼片段和https連接器配置。無法偶爾上傳zip文件

<!-- 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

可能是網絡超時。但可以肯定的是,看看服務器日誌文件。它們也會包含一條錯誤消息。

我也建議看看HttpClient Java library這使得這些事情更簡單,更可靠。舉例來說,參見this article(接近尾聲)。

+0

用於Apache HttpClient的+1。它內置了錯誤處理邏輯,只需重試大多數基本錯誤的請求,並允許您指定自己的邏輯。 – 2009-06-16 15:40:02