ftp下载中进度条的问题

long_4 2005-10-09 03:36:26
有谁知道进行ftp下载的时候如何显示进度条?我用javax.swing包里的ProgressMonitorInputStream类试了一下,但是显示不出进度条来,不知道是为什么也不知道还有没有其他的方法来显示进度条。
...全文
358 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
long_4 2005-10-10
  • 打赏
  • 举报
回复
这一段是我改的别人的一段代码,基本上能运行,就是不能显示出进度条。这段代码如果是读本地文件的话是可以显示出进度条的,但是通过ftp读取文件就显示不出来了,我也不知道是为什么。
long_4 2005-10-10
  • 打赏
  • 举报
回复
package com.belstar.dorado;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.ProgressMonitorInputStream;

import sun.net.*;
import sun.net.ftp.*;
public class ProcessBar {

public static void main(String[] args) throws IOException {
final JFrame f = new JFrame("ProgressMonitor Sample");
f.getContentPane().setLayout(new FlowLayout());
JButton b = new JButton("Click me");
f.getContentPane().add(b);
f.pack();
File f1=new File("e://test1//a.rmvb");
final RandomAccessFile getFile =new RandomAccessFile(f1,"rw");
getFile.seek(0);
FtpClient client=new FtpClient("192.168.1.217");
client.login("ftpuser","123456");
client.binary();
TelnetInputStream ftpFile=client.get("/a.rmvb");
final InputStream in=new DataInputStream(ftpFile);

b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Thread() {
public void run() {
try {
InputStream in = new FileInputStream("e://anywhere for you.rmvb"); ProgressMonitorInputStream pm = new ProgressMonitorInputStream (f,"Reading file: a.rmvb",in);

int c;

while((c=pm.read()) != -1) {
getFile.write(c);
}
System.out.println("下载完毕");
pm.close();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}.start();
}});

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
client.closeServer();
}
}
long_4 2005-10-10
  • 打赏
  • 举报
回复
代码好长啊,而且好像跟我说的ftp下载时显示进度条关系不大,我的意思是说你的代码具有普遍性,但是用到ftp下载上可能不行。我不知道我说的对不对,不过还是很感谢你的!
yorkchen 2005-10-10
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.swing.*;

public class ProgressList extends JPanel implements Scrollable
{
public ProgressList()
{
super(null);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setBackground(Color.WHITE);
}

public Dimension getPreferredScrollableViewportSize()
{
return getPreferredSize();
}

public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
{
if (getComponentCount() > 0) {
Component c = getComponent(0);
return c.getHeight();
}
return 0;
}

public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
{
return orientation == SwingConstants.HORIZONTAL ? visibleRect.width : visibleRect.height;
}

public boolean getScrollableTracksViewportWidth()
{
return true;
}

public boolean getScrollableTracksViewportHeight()
{
return true;
}

private static class Progress extends JPanel {

//private static final ImageIcon CANCEL_BTN_ICON = new ImageIcon(ProgressList.class.getResource("imgs/cancelIcon.png"));
//private static final ImageIcon TITLE_ICON = new ImageIcon(ProgressList.class.getResource("imgs/titleIcon.png"));

private static final Color SELECTION_BG = UIManager.getColor("List.selectionBackground");
private static final Color SELECTION_FG = UIManager.getColor("List.selectionForeground");
private static final Color UNSELECTION_BG = new Color(245, 245, 245);
private static final Color UNSELECTION_FG = UIManager.getColor("List.foreground");


private JLabel titleIconLabel = new JLabel();
private JLabel titleLabel = new JLabel();
private JLabel progressInfoLabel = new JLabel();
private JButton cancelBtn = new JButton();
private JProgressBar progressBar = new JProgressBar();
private boolean selected;

private Progress(String title, int progress, String processInfo) {
super(new BorderLayout(5, 0));

titleLabel.setText(title);
progressBar.setValue(progress);
progressInfoLabel.setText(processInfo);

//titleIconLabel.setIcon(TITLE_ICON);
//cancelBtn.setIcon(CANCEL_BTN_ICON);

JToolBar cancelBtnPane = new JToolBar();
cancelBtnPane.setOpaque(false);
cancelBtnPane.setFloatable(false);
cancelBtnPane.setRollover(true);
cancelBtnPane.setLayout(new BoxLayout(cancelBtnPane, BoxLayout.X_AXIS));
cancelBtnPane.setBorder(null);
cancelBtnPane.add(cancelBtn);

cancelBtn.setContentAreaFilled(false);
cancelBtn.setEnabled(isCancelable());
cancelBtn.setMargin(new Insets(0,0,0,0));
cancelBtn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
cancel();
}
});

JPanel centerPane = new JPanel(new BorderLayout(0, 2));
centerPane.setOpaque(false);

centerPane.add(titleLabel, BorderLayout.NORTH);
centerPane.add(progressBar, BorderLayout.CENTER);
centerPane.add(progressInfoLabel, BorderLayout.SOUTH);
this.add(titleIconLabel, BorderLayout.WEST);
this.add(centerPane, BorderLayout.CENTER);
this.add(cancelBtnPane, BorderLayout.EAST);

titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD));
titleIconLabel.setVerticalAlignment(JLabel.TOP);

this.setOpaque(true);
this.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 5));

this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
setSelected(!isSelected());
}
});

setSelected(false);
}

public Dimension getMaximumSize()
{
Dimension d = getPreferredSize();
d.width = Integer.MAX_VALUE;

return d;
}

public void setSelected(boolean b)
{
this.selected = b;
if (b) {
setBackground(SELECTION_BG);
titleLabel.setForeground(SELECTION_FG);
progressInfoLabel.setForeground(SELECTION_FG);
}
else {
setBackground(UNSELECTION_BG);
titleLabel.setForeground(UNSELECTION_FG);
progressInfoLabel.setForeground(UNSELECTION_FG);
}
}

public boolean isSelected()
{
return selected;
}

protected void paintComponent(Graphics g)
{
((Graphics2D)g).setRenderingHint(
RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
GradientPaint gp = new GradientPaint(0, 0, getBackground().brighter(), 0, getHeight(), getBackground());

Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(gp);
g2d.fillRect(0, 0, getWidth(), getHeight());
}

private void removeFromProgressList()
{
Container parent = getParent();
if (parent != null) {
parent.remove(this);
parent.validate();
parent.repaint();
}
}

public int getProgress()
{
return progressBar.getValue();
}

public void setProgress(final int progress)
{
if (SwingUtilities.isEventDispatchThread()) {
progressBar.setValue(progress);
if (progress >= 100) {
completed();
}
}
else {
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
progressBar.setValue(progress);
if (progress >= 100) {
completed();
}
}
});
}
}

public String getProgressInfo()
{
return progressInfoLabel.getText();
}

public void setProcessInfo(final String progressInfo)
{
if (SwingUtilities.isEventDispatchThread()) {
progressInfoLabel.setText(progressInfo);
}
else {
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
progressInfoLabel.setText(progressInfo);
}
});
}
}

protected boolean isCancelable()
{
return true;
}

protected void cancel()
{
removeFromProgressList();
}

protected void completed()
{
removeFromProgressList();
}
}

private static int n = 0;

public static void main(String[] args)
{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel(new PlasticXPLookAndFeel());
} catch (Exception e) {
e.printStackTrace();
}

final Random random = new Random();
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
final ProgressList progressList = new ProgressList();

JButton btn = new JButton("Start");
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
final Progress progress = new Progress("Test Progress - " + (n++), 0, " ");
progressList.add(progress);
progressList.revalidate();
progressList.getParent().repaint();

new Thread() {
public void run()
{
while (true) {
try {
Thread.sleep(2000 + random.nextInt(200));
} catch (InterruptedException e) {
}
int t = progress.getProgress();
if (t >= 100) {
return;
}

t += random.nextInt(20);
t = Math.min(t, 100);
progress.setProgress(t);
progress.setProcessInfo("Progress " + t + "% complete, current time is " + sdf.format(new Date()));
}
}
}.start();
}
});
JPanel btnPane = new JPanel();
btnPane.add(btn);

JScrollPane sp = new JScrollPane(progressList);
JFrame f = new JFrame("Progress List Test");
f.getContentPane().add(sp, BorderLayout.CENTER);
f.getContentPane().add(btnPane, BorderLayout.SOUTH);
f.setSize(400, 400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}

long_4 2005-10-10
  • 打赏
  • 举报
回复
楼上,为什么GX呢?
TinyJimmy 2005-10-10
  • 打赏
  • 举报
回复
知道读取文件总长度/当前已读取长度就可以显示了, 还需要更多信息吗?
yorkchen 2005-10-09
  • 打赏
  • 举报
回复
GX

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧