2013-05-12 81 views
0

在项目中,我们使用apache httpclient,并且在使用它时没有例外。但问题是,我们无法在远程http服务器上找到任何文件或任何文件。需要在远程http服务器上安装什么才能使用apache httpclient

我们在线阅读应该安装雅加达文件上传,我们这样做,但现在我们被卡住了。因为没有文件上传到服务器或者我们找不到它们。

有人吗?

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响应。

我们真正想要做的是阿帕奇的HttpClient的文件上传到远程的httpserver

+0

你的问题还不清楚。请更详细地解释您的使用案例。 – javadeveloper 2013-05-12 10:18:48

回答

0

,顾名思义,它是一个“HTTP”客户端。这意味着它可以在网络上使用HTTP协议。只要你发送有效的http请求,你的服务器就不需要也不应该知道这里的客户端(甚至可以是可以对话的浏览器)

你是否在谈论http client的依赖关系?

当你说你没有看到异常,你处理它们(不同于空的catch块?)需要代码来查找和复制问题

+0

我已经添加了一些代码!并且不会抛出异常,只会返回状态码301。 – 2013-05-15 16:16:20

相关问题