下:
FileLoad.java:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
public class FileLoad extends JFrame implements ActionListener
{
JPanel norJp=new JPanel();
JPanel souJp=new JPanel();
JLabel addLabel=new JLabel("下载地址:");
JTextField jtf=new JTextField(20);
JButton jb=new JButton("下载");
JProgressBar jpb=new JProgressBar(0,0,500);
public static void main(String[] args)
{
new FileLoad();
}
public FileLoad()
{
this.setTitle("FileLoad");
this.setSize(500,200);
Container con=super.getContentPane();
BorderLayout border=new BorderLayout();
con.setLayout(border);
jb.addActionListener(this);
norJp.setLayout(new FlowLayout());
norJp.add(addLabel);
norJp.add(jtf);
norJp.add(jb);
souJp.setLayout(new FlowLayout());
jpb.setValue(50);
souJp.add(jpb);
con.add(norJp,BorderLayout.NORTH);
con.add(souJp,BorderLayout.CENTER);
this.show();
}
public void actionPerformed(ActionEvent e)
{
//System.out.println("++++++++");
int i=0;
int value=10;
if(e.getSource()==jb)
{
//String jtfStr=jtf.getText();
new URLFileLoad(jpb,this,souJp,jtf);
//System.out.println("jtfStr");
}
}
}URLFileLoad.java:
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class URLFileLoad
{
JProgressBar jpb;
public URLFileLoad(JProgressBar jpb,JFrame frame,JPanel jpl,JTextField jtf)
{
try
{
this.jpb=jpb;
FileOutputStream out=new FileOutputStream("e:\\UUCall3.exe");
String urlStr=jtf.getText();
System.out.println("urlStr="+urlStr);
//URL url=new URL("http://127.0.0.1:8585/manager/UUCall3.exe");
URL url=new URL(urlStr);
URLConnection urlCon=url.openConnection();
Map m=urlCon.getHeaderFields();
Set set=m.entrySet();
Iterator it=set.iterator();
while(it.hasNext())
{
Map.Entry me=(Map.Entry)it.next();
System.out.println(me.getKey()+": "+me.getValue());
}
String fileStr=(String)m.get("Content-Length").toString();
fileStr=fileStr.replace('[',' ');
fileStr=fileStr.replace(']',' ');
fileStr=fileStr.trim();
System.out.println(fileStr);
int fileLen=Integer.valueOf(fileStr);
//System.out.println("k="+k);
InputStream in=urlCon.getInputStream();
BufferedInputStream buffIn=new BufferedInputStream(in);
byte[] buf=new byte[1024*10];
int size=0;
int fileNum=0;
int setValue=0;
while((size=buffIn.read(buf))!=-1)
{ //System.out.println("fileNum"+fileNum);
out.write(buf,0,size);
if(fileNum>1024)
{
System.out.println("已经下载了"+fileNum/1024+"k");
}
fileNum+=size;
setValue+=5;
jpb.setValue(setValue);
frame.setVisible(true);
Thread.sleep(500);
}
}
catch(Exception e)
{
System.out.println(e);
}
}}
FileLoad.java:
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
public class FileLoad extends JFrame implements ActionListener
{
JPanel norJp=new JPanel();
JPanel souJp=new JPanel();
JLabel addLabel=new JLabel("下载地址:");
JTextField jtf=new JTextField(20);
JButton jb=new JButton("下载");
JProgressBar jpb=new JProgressBar(0,0,500);
public static void main(String[] args)
{
new FileLoad();
}
public FileLoad()
{
this.setTitle("FileLoad");
this.setSize(500,200);
Container con=super.getContentPane();
BorderLayout border=new BorderLayout();
con.setLayout(border);
jb.addActionListener(this);
norJp.setLayout(new FlowLayout());
norJp.add(addLabel);
norJp.add(jtf);
norJp.add(jb);
souJp.setLayout(new FlowLayout());
jpb.setValue(50);
souJp.add(jpb);
con.add(norJp,BorderLayout.NORTH);
con.add(souJp,BorderLayout.CENTER);
this.show();
}
public void actionPerformed(ActionEvent e)
{
//System.out.println("++++++++");
int i=0;
int value=10;
if(e.getSource()==jb)
{
//String jtfStr=jtf.getText();
new URLFileLoad(jpb,this,souJp,jtf);
//System.out.println("jtfStr");
}
}
}URLFileLoad.java:
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class URLFileLoad
{
JProgressBar jpb;
public URLFileLoad(JProgressBar jpb,JFrame frame,JPanel jpl,JTextField jtf)
{
try
{
this.jpb=jpb;
FileOutputStream out=new FileOutputStream("e:\\UUCall3.exe");
String urlStr=jtf.getText();
System.out.println("urlStr="+urlStr);
//URL url=new URL("http://127.0.0.1:8585/manager/UUCall3.exe");
URL url=new URL(urlStr);
URLConnection urlCon=url.openConnection();
Map m=urlCon.getHeaderFields();
Set set=m.entrySet();
Iterator it=set.iterator();
while(it.hasNext())
{
Map.Entry me=(Map.Entry)it.next();
System.out.println(me.getKey()+": "+me.getValue());
}
String fileStr=(String)m.get("Content-Length").toString();
fileStr=fileStr.replace('[',' ');
fileStr=fileStr.replace(']',' ');
fileStr=fileStr.trim();
System.out.println(fileStr);
int fileLen=Integer.valueOf(fileStr);
//System.out.println("k="+k);
InputStream in=urlCon.getInputStream();
BufferedInputStream buffIn=new BufferedInputStream(in);
byte[] buf=new byte[1024*10];
int size=0;
int fileNum=0;
int setValue=0;
while((size=buffIn.read(buf))!=-1)
{ //System.out.println("fileNum"+fileNum);
out.write(buf,0,size);
if(fileNum>1024)
{
System.out.println("已经下载了"+fileNum/1024+"k");
}
fileNum+=size;
setValue+=5;
jpb.setValue(setValue);
frame.setVisible(true);
Thread.sleep(500);
}
}
catch(Exception e)
{
System.out.println(e);
}
}}
解决方案 »
- 单元测试由谁来做?
- Error: cannot find symbol symbol: constructor Main(int,int)
- 高分求救:有人知道à, è这些法文对应的html编码吗?来者都有分,有答案马上结贴,谢谢
- 关于JAVA系统进程的问题>?请各位帮忙解决.
- 用opi包把汉字写入excel里,出现乱码!!
- 用编程语言写一个在局域网里检测DHCP主机的小工具?
- struts 国际化中hk 的locale怎么设置,配置文件应该什么名字?
- 请问怎样将一个Byte[]类型的数据保存到Oracel里面
- 使用Runtime.exec("calc.exe")时,编译 出现错误,看不懂这个错误,怎么回事?
- 另一道关于数组的排序——冒泡问题
- 关于泛型的问题,新手上路
- 菜鸟问题,如何在按钮上面加上图片
{ //System.out.println("fileNum"+fileNum);
out.write(buf,0,size);if(fileNum>1024)
{
System.out.println("已经下载了"+fileNum/1024+"k");
}
fileNum+=size;
setValue+=5;
jpb.setValue(setValue);
frame.setVisible(true);
Thread.sleep(500);
}
-----------------------------------------
首先确保这段代码能执行,用输出信息查看一下就行了,
jpb的取值范围为[0, fileLength]
最后不行的话,再加句jpb.repaint()试试
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
public class FileLoad extends JFrame implements ActionListener {
JPanel norJp = new JPanel();
JPanel souJp = new JPanel(); JLabel addLabel = new JLabel("下载地址:");
JTextField jtf = new JTextField(20);
JButton jb = new JButton("下载"); JProgressBar jpb = new JProgressBar(0, 0, 500); public static void main(String[] args) {
new FileLoad(); }
public FileLoad() {
this.setTitle("FileLoad");
this.setSize(500, 200);
Container con = super.getContentPane();
BorderLayout border = new BorderLayout();
con.setLayout(border);
jb.addActionListener(this); norJp.setLayout(new FlowLayout());
norJp.add(addLabel);
norJp.add(jtf);
norJp.add(jb); souJp.setLayout(new FlowLayout());
jpb.setValue(0);
jpb.setStringPainted(true);
souJp.add(jpb); con.add(norJp, BorderLayout.NORTH);
con.add(souJp, BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true);
} public void actionPerformed(ActionEvent e) {
//System.out.println("++++++++");
int i = 0;
int value = 10;
if (e.getSource() == jb) {
//String jtfStr=jtf.getText();
URLFileLoad ufl = new URLFileLoad(jpb, this, souJp, jtf);
ufl.start();
//System.out.println("jtfStr");
} }}
import java.net.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;public class URLFileLoad extends Thread{
JProgressBar jpb;
JPanel jpl;
int setValue = 0;
int size = 0;
BufferedInputStream buffIn;
FileOutputStream out;
int fileNum = 0; public URLFileLoad(JProgressBar jpb, JFrame frame, JPanel jpl, JTextField jtf) {
try {
this.jpb = jpb;
this.jpl = jpl;
out = new FileOutputStream("d:\\temp.txt");
String urlStr = jtf.getText();
System.out.println("urlStr=" + urlStr);
URL url=new URL("http://download.csdn.net/filedown/aHR0cDovL2RsMi5jc2RuLm5ldC9kb3duNC8yMDA3MDcxMC8xMDE0MDQwOTgyOC5yYXI=!207514");
//URL url = new URL(urlStr); URLConnection urlCon = url.openConnection(); Map m = urlCon.getHeaderFields();
Set set = m.entrySet();
Iterator it = set.iterator();
while (it.hasNext()) {
Map.Entry me = (Map.Entry) it.next();
System.out.println(me.getKey() + ": " + me.getValue());
}
String fileStr = (String) m.get("Content-Length").toString();
fileStr = fileStr.replace('[', ' ');
fileStr = fileStr.replace(']', ' ');
fileStr = fileStr.trim();
System.out.println(fileStr);
int fileLen = Integer.parseInt(fileStr);// /
jpb.setMaximum(fileLen); // System.out.println("k="+k);
InputStream in = urlCon.getInputStream();
buffIn = new BufferedInputStream(in);
} catch (Exception e) {
System.out.println(e);
} } /* (non-Javadoc)
* @see java.lang.Thread#run()
*/
public void run() {
byte[] buf = new byte[1024 * 10];
try {
while ((size = buffIn.read(buf)) != -1) { // System.out.println("fileNum"+fileNum);
out.write(buf, 0, size); if (fileNum > 1024) {
System.out.println("已经下载了" + fileNum / 1024 + "k");
}
fileNum += size;
setValue += size;
jpb.setValue(setValue);
jpb.repaint();
jpl.validate();
//frame.setVisible(true);
Thread.sleep(500);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}}