簡體   English   中英

要使用apache httpclient,必須在遠程http服務器上安裝什么

[英]What has to be installed on the remote http server to use apache httpclient

在一個項目中,我們使用apache httpclient,並且使用它時也沒有例外。 但是問題是,我們無法在遠程http服務器或任何其他位置上找到文件。

我們在線閱讀了應該安裝jakarta文件上傳的信息,並且這樣做了,但是現在我們陷入了困境。 由於沒有文件上傳到服務器,或者我們找不到它們。

有人嗎

public class HttpServerHandler {

/**
 * A generic method to execute any type of Http Request and constructs a response object
 * @param requestBase the request that needs to be exeuted
 * @return server response as <code>String</code>
 */
private static String executeRequest(HttpRequestBase requestBase){
    String responseString = "" ;

    InputStream responseStream = null ;
    HttpClient client = new DefaultHttpClient () ;

    try{

        HttpResponse response = client.execute(requestBase);

        // get response of the server
        if (response != null){
            HttpEntity responseEntity = response.getEntity() ;

            if (responseEntity != null){
                responseStream = responseEntity.getContent();
                if (responseStream != null){
                    BufferedReader br = new BufferedReader (new InputStreamReader (responseStream)) ;
                    String responseLine = br.readLine() ;
                    String tempResponseString = "" ;
                    while (responseLine != null){
                        tempResponseString = tempResponseString + responseLine + System.getProperty("line.separator") ;
                        responseLine = br.readLine() ;
                    }
                    br.close() ;
                    if (tempResponseString.length() > 0){
                        responseString = tempResponseString ;
                    }
                }
            }
        }


    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally{
        if (responseStream != null){
            try {
                responseStream.close() ;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    client.getConnectionManager().shutdown() ;

    return responseString ;
}

/**
 * Method that builds the multi-part form data request
 * @param urlString the urlString to which the file needs to be uploaded
 * @param file the actual file instance that needs to be uploaded
 * @param fileName name of the file, just to show how to add the usual form parameters
 * @param fileDescription some description for the file, just to show how to add the usual form parameters
 * @return server response as <code>String</code>
 */
public String executeMultiPartRequest(String urlString, File file, String fileName, String fileDescription) {

    HttpPost postRequest = new HttpPost (urlString) ;
    try{

        MultipartEntity multiPartEntity = new MultipartEntity () ;

        //The usual form parameters can be added this way
        multiPartEntity.addPart("fileDescription", new StringBody(fileDescription != null ? fileDescription : "")) ;
        multiPartEntity.addPart("fileName", new StringBody(fileName != null ? fileName : file.getName())) ;

        /*Need to construct a FileBody with the file that needs to be attached and specify the mime type of the file. Add the fileBody to the request as an another part.
        This part will be considered as file part and the rest of them as usual form-data parts*/
        FileBody fileBody = new FileBody(file, "application/octect-stream") ;
        multiPartEntity.addPart("attachment", fileBody) ;

        postRequest.setEntity(multiPartEntity) ;
    }catch (UnsupportedEncodingException ex){
        ex.printStackTrace() ;
    }

    return executeRequest (postRequest);

}
}

這是將文件上傳到http服務器的代碼。 該文件是通過表單上傳並傳遞給此類的? 當我們嘗試上傳時,我們會收到狀態碼301的響應。

我們真正想要做的是通過apache的httpclient將文件上傳到遠程httpserver

顧名思義,它是一個“ http”客戶端。 這意味着它可以在線上使用HTTP協議。 只要您發送有效的http請求,您的服務器就不需要也不應該知道此處的客戶端(甚至可以是可以使用http的瀏覽器)

您是否在談論http客戶端的依賴關系?

當您說看不到異常時,您是否正在處理它們(與空的catch塊不同?)需要代碼來查找和復制問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM