简体   繁体   English

要使用apache httpclient,必须在远程http服务器上安装什么

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

In a project we use apache httpclient and there are no exceptions when we use it. 在一个项目中,我们使用apache httpclient,并且使用它时也没有例外。 But the problem is, we can't find files on the remote http-server or whatsoever. 但是问题是,我们无法在远程http服务器或任何其他位置上找到文件。

We read online that jakarta file upload should be installed and we did this, but now we are stuck. 我们在线阅读了应该安装jakarta文件上传的信息,并且这样做了,但是现在我们陷入了困境。 Because no files were uploaded to the server or we can't find them. 由于没有文件上传到服务器,或者我们找不到它们。

Anybody? 有人吗

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);

}
}

This is the code to upload the file to the http server. 这是将文件上传到http服务器的代码。 The file is uploaded through a form and passed through to this class? 该文件是通过表单上传并传递给此类的? And when we try to upload it, we get a status code 301 response. 当我们尝试上传时,我们会收到状态码301的响应。

What we really want to do is to upload the file by the httpclient of apache to a remote httpserver 我们真正想要做的是通过apache的httpclient将文件上传到远程httpserver

As the name suggests, it is a "http" client. 顾名思义,它是一个“ http”客户端。 Meaning that it works on HTTP protocol over the wire. 这意味着它可以在线上使用HTTP协议。 As long as you are sending valid http-requests, your server need not and should not know about the client here (could be even a browser which can talk http) 只要您发送有效的http请求,您的服务器就不需要也不应该知道此处的客户端(甚至可以是可以使用http的浏览器)

Are you talking about the dependencies for http client ? 您是否在谈论http客户端的依赖关系?

When you say that you don't see exceptions, are you handling them (unlike empty catch blocks ?) Need code to look and replicate the issue 当您说看不到异常时,您是否正在处理它们(与空的catch块不同?)需要代码来查找和复制问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Apache HTTPClient的post方法将列表发送到服务器 - How to use Apache HTTPClient's post method to send a List to the server org.apache.http 已弃用,该使用什么? - org.apache.http is deprecated, what to use? Apache HttpClient获取服务器证书 - Apache HttpClient get server certificate 带有https方案的URL中的Apache HttpClient和远程文件 - Apache HttpClient and remote files in URL with https scheme 在Apache HttpClient上使用HTTPS代理 - Use HTTPS proxy on Apache HttpClient Apache HttpClient - 需要使用MultiThreadedHttpConnectionManager吗? - Apache HttpClient - Need to use MultiThreadedHttpConnectionManager? apache HttpClient:无效使用SingleClientConnManager - apache HttpClient: Invalid use of SingleClientConnManager apache HttpClient API中的setConnectionTimeout,setSoTimeout和“http.connection-manager.timeout”之间有什么区别 - What is the difference between the setConnectionTimeout , setSoTimeout and “http.connection-manager.timeout” in apache HttpClient API HttpClient 4.3.1的org.apache.http.client.utils.URIBuilder中`removeQuery()`和`clearParameters()`有什么区别? - What is the difference between `removeQuery()` and `clearParameters()` in org.apache.http.client.utils.URIBuilder of HttpClient 4.3.1? 我需要设置什么才能使org.apache.http.client.HttpClient的execute()工作? - What do I need to set up for the execute() of org.apache.http.client.HttpClient to work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM