简体   繁体   中英

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. But the problem is, we can't find files on the remote http-server or whatsoever.

We read online that jakarta file upload should be installed and we did this, but now we are stuck. 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. 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.

What we really want to do is to upload the file by the httpclient of apache to a remote httpserver

As the name suggests, it is a "http" client. Meaning that it works on HTTP protocol over the wire. 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)

Are you talking about the dependencies for http client ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM