java httpclient 进度条_如何使用Apache HttpClient 4获取文件上传的进度条?
发布日期:2021-06-24 13:19:17 浏览次数:3 分类:技术文章

本文共 4871 字,大约阅读时间需要 16 分钟。

小编典典

大家好!

我自己解决了这个问题,并给出了一个简单的例子。

如有任何疑问,请随时提问。

开始了!

ApplicationView.java

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JProgressBar;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.HttpVersion;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpPut;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.params.BasicHttpParams;

import org.apache.http.params.HttpParams;

import org.apache.http.params.HttpProtocolParams;

import org.apache.http.util.EntityUtils;

public class ApplicationView implements ActionListener

{

File file = new File("C:/Temp/my-upload.avi");

JProgressBar progressBar = null;

public ApplicationView()

{

super();

}

public void createView()

{

JFrame frame = new JFrame("File Upload with progress bar - Example");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setBounds(0, 0, 300, 200);

frame.setVisible(true);

progressBar = new JProgressBar(0, 100);

progressBar.setBounds(20, 20, 200, 30);

progressBar.setStringPainted(true);

progressBar.setVisible(true);

JButton button = new JButton("upload");

button.setBounds(progressBar.getX(),

progressBar.getY() + progressBar.getHeight() + 20,

100,

40);

button.addActionListener(this);

JPanel panel = (JPanel) frame.getContentPane();

panel.setLayout(null);

panel.add(progressBar);

panel.add(button);

panel.setVisible(true);

}

public void actionPerformed(ActionEvent e)

{

try

{

sendFile(this.file, this.progressBar);

}

catch (Exception ex)

{

System.out.println(ex.getLocalizedMessage());

}

}

private void sendFile(File file, JProgressBar progressBar) throws Exception

{

String serverResponse = null;

HttpParams params = new BasicHttpParams();

params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, true);

HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

HttpClient client = new DefaultHttpClient(params);

HttpPut put = new HttpPut("http://localhost:8080/" + file.getName());

ProgressBarListener listener = new ProgressBarListener(progressBar);

FileEntityWithProgressBar fileEntity = new FileEntityWithProgressBar(file, "binary/octet-stream", listener);

put.setEntity(fileEntity);

HttpResponse response = client.execute(put);

HttpEntity entity = response.getEntity();

if (entity != null)

{

serverResponse = EntityUtils.toString(entity);

System.out.println(serverResponse);

}

}

}

FileEntityWithProgressBar.java

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import org.apache.http.entity.AbstractHttpEntity;

/**

* File entity which supports a progress bar.

* Based on "org.apache.http.entity.FileEntity".

* @author Benny Neugebauer (www.bennyn.de)

*/

public class FileEntityWithProgressBar extends AbstractHttpEntity implements Cloneable

{

protected final File file;

private final ProgressBarListener listener;

private long transferredBytes;

public FileEntityWithProgressBar(final File file, final String contentType, ProgressBarListener listener)

{

super();

if (file == null)

{

throw new IllegalArgumentException("File may not be null");

}

this.file = file;

this.listener = listener;

this.transferredBytes = 0;

setContentType(contentType);

}

public boolean isRepeatable()

{

return true;

}

public long getContentLength()

{

return this.file.length();

}

public InputStream getContent() throws IOException

{

return new FileInputStream(this.file);

}

public void writeTo(final OutputStream outstream) throws IOException

{

if (outstream == null)

{

throw new IllegalArgumentException("Output stream may not be null");

}

InputStream instream = new FileInputStream(this.file);

try

{

byte[] tmp = new byte[4096];

int l;

while ((l = instream.read(tmp)) != -1)

{

outstream.write(tmp, 0, l);

this.transferredBytes += l;

this.listener.updateTransferred(this.transferredBytes);

}

outstream.flush();

}

finally

{

instream.close();

}

}

public boolean isStreaming()

{

return false;

}

@Override

public Object clone() throws CloneNotSupportedException

{

return super.clone();

}

}

ProgressBarListener.java

import javax.swing.JProgressBar;

public class ProgressBarListener

{

private int transferedMegaBytes = 0;

private JProgressBar progressBar = null;

public ProgressBarListener()

{

super();

}

public ProgressBarListener(JProgressBar progressBar)

{

this();

this.progressBar = progressBar;

}

public void updateTransferred(long transferedBytes)

{

transferedMegaBytes = (int) (transferedBytes / 1048576);

this.progressBar.setValue(transferedMegaBytes);

this.progressBar.paint(progressBar.getGraphics());

System.out.println("Transferred: " + transferedMegaBytes + " Megabytes.");

}

}

编码愉快!

2020-09-16

转载地址:https://blog.csdn.net/weixin_33093437/article/details/114521882 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:下列不属于java语言特点的是_下列选项中,不属于Java语言特点的一项是( )。...
下一篇:java多态替换switch_使多态性无法解决那些switch / case语句的麻烦

发表评论

最新留言

不错!
[***.144.177.141]2024年04月26日 23时58分36秒