Java httppost文件发布上传速度

时间:2014-01-26 10:20:13

标签: java httpclient apache-httpclient-4.x

使用此java函数将文件上传到服务器。 在上传过程中,上传速度和发送的数据大小如何打印?

public void PostFile()  {
    try {
        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        File file = new File("D:/sqldata.zip");
        HttpPost httppost = new HttpPost("http://localhost/upload.php");  
        HttpEntity httpEntity = MultipartEntityBuilder.create()
            .addBinaryBody("userfile", file, ContentType.create("application/zip"), file.getName())
            .build();

        httppost.setEntity(httpEntity);

        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();
    } catch (Exception e) {
        System.out.println("hata: "+e);
    }
}

1 个答案:

答案 0 :(得分:0)

谢谢大家。我解决了这个问题。这样,您就可以打印即时发送的数据。

main.java

public void post(String url, File sendFile) throws UnsupportedEncodingException, IOException {
    long start = System.nanoTime();    
    HttpParams params = new BasicHttpParams();
    params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, true);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpClient client = new DefaultHttpClient(params);
    HttpPost post = new HttpPost(url);
   MultipartEntity reqEntity = new MultipartEntity(){
    ProgressListener listener;           
                public void writeTo(final OutputStream outstream) throws IOException {
                    super.writeTo(new UploadStream(outstream,listener));                                 
                }
            };
    reqEntity.addPart("userfile", new FileBody(sendFile));
    post.setEntity(reqEntity);
    HttpResponse response = client.execute(post);
    if (response != null) {
    HttpEntity resEntity = response.getEntity();
    System.out.println(response.getStatusLine());
    if (resEntity != null) {
    System.out.println(EntityUtils.toString(resEntity));
    }
    if (resEntity != null) {
      resEntity.consumeContent();
    }
    }  
}

UploadStream.java

public class UploadStream extends FilterOutputStream {

final ProgressListener listener;
private long transferred;

public UploadStream(final OutputStream out, final ProgressListener listener)
{
    super(out);
    this.listener = listener;
    this.transferred = 0;
}

public void write(byte[] b, int off, int len) throws IOException
{
    out.write(b, off, len);
    this.transferred += len;
    System.out.println(this.transferred/1024+" KB");
}

public void write(int b) throws IOException
{
    out.write(b);
    this.transferred++;
}

}