首页 \ 问答 \ HttpClient上传大文件并显示发送的字节数(HttpClient upload big file and show sent bytes number)

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
更新时间:2022-09-11 12:09

最满意答案

你有三个选择。

将所有项目存储在列表中,然后使用索引编制。 但这会在内存中创建一个不必要的列表:

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.

相关问答

更多

相关文章

更多

最新问答

更多
  • Firebird客户端安装(Firebird client installation)
  • 如何检查一个文件是否已被C中的另一个进程打开?(How to check if a file is already open by another process in C?)
  • 将对象引用存储在控件标签属性确定中(Is storing an object reference in a controls Tag property OK)
  • 谁能介绍《商务谈判》课程的高职高专教材???谢谢!!!
  • 递归图像下载与请求(recursive image download with requests)
  • C ++对齐字符以便在任何实现的输入中整齐地显示(C++ aligning characters to display neatly with any input implemented)
  • 根据字段值插入一行(Insert a row, based on a fields value)
  • 在Ubuntu上使用TCP_REPAIR套接字选项编译代码[关闭](Compiling code with TCP_REPAIR socket option on Ubuntu [closed])
  • 在开发React应用程序时编译/转换代码(Compile/transpile code while developing React app)
  • 重庆软件开发培训 Java培训哪好
  • 将MultiPoint序列化为GeoJSON文件(Serialize MultiPoint to GeoJSON file)
  • 将文本从多个文件,相同的名称复制到bash(linux)中的不同路径(Copy text from multiple files, same names to different path in bash (linux))
  • 将字符串截断为特定数量的字符,忽略HTML(Truncate string to certain amount of characters, ignoring HTML)
  • 如何为崩溃的JNI调用编写JUnit测试?(How can I write a JUnit test for a JNI call that crashes?)
  • 多点触摸两个手指轻拍(Multi-touch two fingers taps)
  • Sqlserver监视时间的变化(Sqlserver watch for time change)
  • Srcset属性 - 最大宽度问题(Srcset attribute - max-width issue)
  • 如何验证数据库中是否存在记录?(How to verify existence of a record in database?)
  • SQL JOIN来自不同表的行具有相同的值(SQL JOIN row from different table with the same values)
  • NSTextField - 使用KVO进行输入验证?(NSTextField - Input validation with KVO?)
  • 如何使用DBPedia从根类别中提取不同级别的子类别?(How to extract sub-categories of different levels from a root category using DBPedia?)
  • 在Javascript中,如何检查数组是否有重复值?(In Javascript, how do I check if an array has duplicate values? [duplicate])
  • 有什么区别:焦点:必需:无效:焦点和:焦点:必需:无效?(What's the difference between :focus:required:invalid:focus and :focus:required:invalid?)
  • 使用OData创建新数据(Creating new data with OData)
  • 获取过滤器从spark数据帧中删除的行的示例(Get examples for rows that are removed by a filter from a spark dataframe)
  • 使用@RequestMapping编码到Spring Controller方法的接口(Coding to an interface on a Spring Controller method with @RequestMapping)
  • 如果Shape在屏幕外,是否执行Graphics2D.draw?(Is Graphics2D.draw performed if the Shape is offscreen?)
  • 与ajax的成功(success with ajax)
  • 如何获取在Javascript中单击的文本?(How can I get the text that was clicked on in Javascript?)
  • 如果没有头文件,如何定义静态成员?(How to define a static member in case there is not header file?)