HttpClient上传大文件并显示发送的字节数(HttpClient upload big file and show sent bytes number)
我找到了这个代码示例
import org.apache.http.params.CoreProtocolPNames; import org.apache.http.util.EntityUtils; public class PostFile { public static void main(String[] args) throws Exception { HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPost httppost = new HttpPost("http://localhost:9001/upload.php"); File file = new File("c:/TRASH/zaba_1.jpg"); MultipartEntity mpEntity = new MultipartEntity(); ContentBody cbFile = new FileBody(file, "image/jpeg"); mpEntity.addPart("userfile", cbFile); httppost.setEntity(mpEntity); System.out.println("executing request " + httppost.getRequestLine()); HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); System.out.println(response.getStatusLine()); if (resEntity != null) { System.out.println(EntityUtils.toString(resEntity)); } if (resEntity != null) { resEntity.consumeContent(); } httpclient.getConnectionManager().shutdown(); } }
我只是想知道如何获得上传的字节总和?
I have found this code sample
import org.apache.http.params.CoreProtocolPNames; import org.apache.http.util.EntityUtils; public class PostFile { public static void main(String[] args) throws Exception { HttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPost httppost = new HttpPost("http://localhost:9001/upload.php"); File file = new File("c:/TRASH/zaba_1.jpg"); MultipartEntity mpEntity = new MultipartEntity(); ContentBody cbFile = new FileBody(file, "image/jpeg"); mpEntity.addPart("userfile", cbFile); httppost.setEntity(mpEntity); System.out.println("executing request " + httppost.getRequestLine()); HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); System.out.println(response.getStatusLine()); if (resEntity != null) { System.out.println(EntityUtils.toString(resEntity)); } if (resEntity != null) { resEntity.consumeContent(); } httpclient.getConnectionManager().shutdown(); } }
I am just wondering how to get uploaded bytes sum?
原文:https://stackoverflow.com/questions/5294532
最满意答案
你有三个选择。
将所有项目存储在列表中,然后使用索引编制。 但这会在内存中创建一个不必要的列表:
links = [item for item in stdout.read().splitlines() if 'Link' in item] index = 5 print links[index]
或者使用
itertools.islice
,并将您在代码中使用的生成器传递给它:from itertools import islice index = 5 links = (item for item in stdout.read().splitlines() if 'Link' in item) print next(islice(links, index, index+1))
或者甚至更好地使用
itertools.islice
和以下生成器。 这里我没有使用.read()
或.splitlines()
因为它们将所有内容读入内存:links = (item for item in stdout if 'Link' in item) print next(islice(links, index, index+1))
你也可以使用
item.startswith('Link')
,以防你只想在字符串的开头匹配'Link'
,但如果你想在字符串的任何地方匹配它,那么忽略它。You've three options.
Store all the items in a list and then use indexing. But this will create an unnecessary list in memory:
links = [item for item in stdout.read().splitlines() if 'Link' in item] index = 5 print links[index]
Or use
itertools.islice
, and pass it the generator created you've used in your code:from itertools import islice index = 5 links = (item for item in stdout.read().splitlines() if 'Link' in item) print next(islice(links, index, index+1))
Or even better use
itertools.islice
with the following generator. Here's I am not using either.read()
or.splitlines()
as they read everything into memory:links = (item for item in stdout if 'Link' in item) print next(islice(links, index, index+1))
You can also use
item.startswith('Link')
in case you only want to match'Link'
only at the start of the string, but if you want to match it anywhere in the string then ignore this.
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
这是我的代码: public static String replaceIfOdd(String stringToChange, String searchingWord, String replacingWord) { final String separator = "#######"; String splittingString = stringToChange.replaceAll(searchingWord, separator + sea ...
-
因此,您希望从字典中存在的一组字母中找到最长的排序组合。 为此,您将使用长度等于字符串长度的itertools.combinations() 。 您将根据排序的字典检查所有这些组合,如果找不到匹配项,请减小组合长度。 您还希望将整个字典加载到一个集合中以减少搜索时间。 我已将单词集加载到字典中,其中键是排序字符串,值是具有相同排序表示的单词列表。 像这样的东西: import itertools from collections import defaultdict words = defaultdict ...
-
你做的对我来说看起来很好,也可以使用collections.Counter (假设你是python 2.7或更新版本)来获取更多的信息,比如每个单词的数量。 我的解决方案看起来像这样,可能会有一些改进。 import sys from collections import Counter lines = open(sys.argv[1], 'r').readlines() c = Counter() for line in lines: for work in line.strip().split( ...
-
使用python在文本文件中查找单词出现的第n个实例(Find nth instance of occurrence of a word in a text file using python)[2023-04-30]
你有三个选择。 将所有项目存储在列表中,然后使用索引编制。 但这会在内存中创建一个不必要的列表: links = [item for item in stdout.read().splitlines() if 'Link' in item] index = 5 print links[index] 或者使用itertools.islice ,并将您在代码中使用的生成器传递给它: from itertools import islice index = 5 links = (item for item in ... -
你可以这样做: wordCheck = raw_input("please enter the word you would like to check the spelling of: ") with open("words.txt", "r") as f: found = False for line in f: if line.strip() == wordCheck: print ('That is the correct spelli ...
-
我建议你使用Python内置的JSON库。 您可以轻松地将JSON转换为Python Dict对象。 这也会阻止你使用复杂的正则表达式。 减少可读性的复杂性也会更好。 文档: Python 3.4 Python 2.7 在Python 2中 import json jsonDict = json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') print(jsonDict) 结果: ['foo', {'bar': ['baz', None, 1.0, 2]} ...
-
使用第二个文本文件作为参考Python在文本文件中查找完全匹配(Find exact match in text file using a second text file as reference Python)[2022-06-19]
您可以将所有搜索字词的所有小写字母存储在一个集合中,并更改您的代码,如下所示: 编辑:要打印频率,您可以使用字典 def search_terms(): with open("searchterms.txt", "r") as st: search_terms = [line.rstrip('\n').lower() for line in st] return search_terms terms = set(search_terms()) freq = {} for l ... -
假设“listen = 1.1.1.1”出现在自己的行上,要更改第4个实例: awk -v n=4 '/listen = / && ++m == n {$3 = "2.2.2.2"} 1' file > file.changed Assuming "listen = 1.1.1.1" appears on its own line, to change the 4th instance: awk -v n=4 '/listen = / && ++m == n {$3 = "2.2.2.2"} 1' fil ...