2016-11-17 78 views
-2

我開發一個Java程序,將通過一定的URL http://ServerUrl:portNumber/file把我的文件中的文件全部垃圾到我的服務器進行掃描,但我在我的Eclipse編譯器收到此錯誤
setEntity方法是如何工作的Java

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

} 
+0

是的,一個''InputStream''是不一樣''HttpEntity''。有什麼必須嘗試解決這個問題? – f1sh

+0

在我的電腦中有一個文件,我想使用java代碼將它發送到服務器的url進行掃描。 –

回答

0

FileEntity將在這裏工作

post.setEntity(new FileEntity(file)); 
+0

我已經添加了你的代碼,它現在顯示我沒有錯誤,但它顯示了一些包mssing。你有任何推薦的網站可以下載我的包嗎? –

+0

嗯,我認爲它應該來自'org.apache.http'只有這是導入語句'import org.apache.http.entity.FileEntity;'。 – Mritunjay