//... Some annoying getter
ExecutorService es = Executors.newSingleThreadExecutor();
Future<Integer> result = es.submit(new Callable<Integer>() {
public Integer call() throws Exception {
//Get some value from the SQL database.
}
});
return result;
Run Code Online (Sandbox Code Playgroud)
好的,我看了一遍.我需要知道如何让它等待,直到它完成从数据库中检索一个值来返回它.
我无法使用WebClient,在任何人建议之前,因为它使我的合法应用程序看起来像迈克菲病毒.所以请不要这么做.
我有一个存储在我的服务器上的binary.txt文件.它大约是1,240kb.但是,HttpWebRequest下载的随机数量从1,300kb到1,700kb.
HttpWebRequest httpRequest = (HttpWebRequest)
WebRequest.Create("http://deviantsmc.com/binary.txt");
httpRequest.Method = WebRequestMethods.Http.Get;
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream httpResponseStream = httpResponse.GetResponseStream();
byte[] buffer = new byte[1240];
int bytesRead = 0;
StringBuilder sb = new StringBuilder();
//FileStream fileStream = File.Create(@"tcontent.txt");
while ((bytesRead = httpResponseStream.Read(buffer, 0, 1240)) != 0)
{
sb.Append(Encoding.ASCII.GetString(buffer));
//fileStream.Write(buffer, 0, bytesRead);
}
File.WriteAllText(@"tcontent1.txt", sb.ToString());
Run Code Online (Sandbox Code Playgroud)
(服务器上的binary.txt文件的内容是ASCII,因此我也将Encoding字符串转换为ASCII).
这是我编码该文本文件的方式(在该服务器上)
我的文件基本上是这样的:
byte[] bytes = File.ReadAllBytes("binary.txt");
String encBytes = Encoding.ASCII.GetString(bytes);
File.WriteAllText(file("binary.txt"), encBytes);
Run Code Online (Sandbox Code Playgroud)
我联系了AV公司关于WebDownloader被视为C#中的一些恶意导入,但他们没有回复我,所以我被迫使用HttpWebRequest.