setEntity方法如何在java

时间:2016-11-17 10:56:21

标签: java

我正在开发一个java程序,它会将我的整个垃圾文件中的文件发送到我的服务器,以便通过某个URL http://ServerUrl:portNumber/file进行扫描,但我在我的eclipse编译器中收到此错误

The method setEntity(HttpEntity) in the type HttpEntityEnclosingRequestBase is not applicable for the arguments (InputStream). 

是不是因为Java Inputstream不能与setEntity方法一起使用?我无法逐字节读取文件,因为该文件不仅包含文档文件,也可能包含.exe文件。
这是我的源代码

public class SentFile {

public static void main(String [] args) {
      HttpClient client = new DefaultHttpClient();
      HttpPost post = new HttpPost("http://192.168.0.25:8008/file");
      File file = new File("testScanFile.txt");
      InputStream is = new FileInputStream(file);
      post.setEntity(is);
      HttpResponse response = client.execute(post);
      BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
      String line = "";
      while ((line = rd.readLine()) != null) {
       System.out.println(line);
      }
     }

}

1 个答案:

答案 0 :(得分:0)

FileEntity可以在这里使用

post.setEntity(new FileEntity(file));