一个简单的下载程序,GUI类负责界面,Download类负责下载,现在的问题是运行时出现MalFormedUrlException ,该怎么修改啊
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;class GUI extends Frame
{
final Download dl ;
static URL url;
URLConnection urlConn;
String str;
URL geturl(String str)throws Exception
{
return new URL(str);
}
GUI()throws Exception
{
setTitle("下载程序");
setSize(600,400);
setLocation(100,100);
setBackground(Color.lightGray);
Panel p=new Panel();
Label l=new Label("Please input URL:");
TextField tf=new TextField(30);
TextArea ta=new TextArea();
Button btn=new Button("Download");
p.add(l);
p.add(tf);
add(p,"North");
add(ta,"Center");
p.add(btn);
str=tf.getText();
url = geturl(str);
urlConn=url.openConnection();
dl = new Download(url);
String line=System.getProperty("line.separator");
ta.append("Host: "+url.getHost());
ta.append(line);
ta.append("Port: "+url.getDefaultPort());
ta.append(line);
ta.append("ContentType: "+urlConn.getContentType());
ta.append(line);
ta.append("ContentLength: "+urlConn.getContentLength());
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dl.downLoad();
}
});
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}class Download
{
URL url;
URLConnection urlConn;
public String getFileName(URL url)
{
String fileName = url.getFile();
return fileName.substring(fileName.lastIndexOf('/') + 1);
}
public Download(URL url)
{
this.url = url;
}
public void downLoad()
{
try
{
urlConn=url.openConnection();
InputStream is=urlConn.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
FileOutputStream fos=new FileOutputStream(getFileName(url));
int data;
while((data=is.read())!=-1)
{
fos.write(data);
}
is.close();
fos.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}class test
{
public static void main(String[] args)throws Exception
{
GUI gui = new GUI();
gui.show();
}
}
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;class GUI extends Frame
{
final Download dl ;
static URL url;
URLConnection urlConn;
String str;
URL geturl(String str)throws Exception
{
return new URL(str);
}
GUI()throws Exception
{
setTitle("下载程序");
setSize(600,400);
setLocation(100,100);
setBackground(Color.lightGray);
Panel p=new Panel();
Label l=new Label("Please input URL:");
TextField tf=new TextField(30);
TextArea ta=new TextArea();
Button btn=new Button("Download");
p.add(l);
p.add(tf);
add(p,"North");
add(ta,"Center");
p.add(btn);
str=tf.getText();
url = geturl(str);
urlConn=url.openConnection();
dl = new Download(url);
String line=System.getProperty("line.separator");
ta.append("Host: "+url.getHost());
ta.append(line);
ta.append("Port: "+url.getDefaultPort());
ta.append(line);
ta.append("ContentType: "+urlConn.getContentType());
ta.append(line);
ta.append("ContentLength: "+urlConn.getContentLength());
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dl.downLoad();
}
});
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}class Download
{
URL url;
URLConnection urlConn;
public String getFileName(URL url)
{
String fileName = url.getFile();
return fileName.substring(fileName.lastIndexOf('/') + 1);
}
public Download(URL url)
{
this.url = url;
}
public void downLoad()
{
try
{
urlConn=url.openConnection();
InputStream is=urlConn.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
FileOutputStream fos=new FileOutputStream(getFileName(url));
int data;
while((data=is.read())!=-1)
{
fos.write(data);
}
is.close();
fos.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}class test
{
public static void main(String[] args)throws Exception
{
GUI gui = new GUI();
gui.show();
}
}
解决方案 »
- 有关Jtable的问题(旧帖新发!!!)
- 关于搜索引擎的问题
- 关于建立文件提示invalid escape sequence的问题
- 请用过Jacob的朋友指点!
- swing中的图片问题
- 在一个由Jbx向导生成的Application工程面,添加一个JPane,再往这个JPane里再添加一个JButton,对它调用JButton.setLocation,在程序运行
- java数据库链接和读取表操作,急!请高手指教!
- 阿里巴巴的笔试题,请大神给我解释下n的值。
- 大家来看看,这段程序为啥选了单选按扭没反应,(JAVA初手)
- 一个scjp问题,我想不通,麻烦进来看看!
- 问一个转码的问题?
- 奇怪问题??? 关于.jar 文件的执行????
但不应该只能这么写吧 太难看了