一个简单的下载程序,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();
}
}
URL=geturl(str);//出错位置.你的初始URL格式不对(没有相匹配的协议)
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;class GUI extends Frame
{
final Download dl;
URL url;
URLConnection urlConn;
static String str;
GUI()throws Exception
{
super();
//dl = new Download();
setSize(600,400);
setLocation(100,100);
setBackground(Color.lightGray);
Panel p=new Panel();
Label l=new Label("Please input URL:");
final TextField tf=new TextField(30);
Button btn=new Button("Download");
final TextArea ta=new TextArea();
p.add(l);
p.add(tf);
add(p,"North");
add(ta,"Center");
p.add(btn);
tf.setText("http://");
str=tf.getText();
URL url= new URL(str);
urlConn=url.openConnection();
dl = new Download(url,urlConn);
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String line=System.getProperty("line.separator");
dl.downLoad();
ta.append("Host: "+dl.getHost());
ta.append(line);
ta.append("Port: "+dl.getPort());
ta.append(line);
ta.append("ContentType: "+dl.getType());
ta.append(line);
ta.append("ContentLength: "+dl.getLength());
}
});
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
class Download
{
String str;
URL url;
URLConnection urlConn;
public Download(URL url,URLConnection urlConn)
{
this.url =url;
this.urlConn = urlConn;
}
public String getFileName(URL url)
{
String fileName = url.getFile();
return fileName.substring(fileName.lastIndexOf('/') + 1);
}
public void downLoad()
{
try
{
//url=new URL("http:");
//urlConn=url.openConnection();
InputStream is=urlConn.getInputStream();
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();
}
}
public String getHost()
{
return url.getHost();
}
public int getPort()
{
return url.getDefaultPort();
}
public String getType()
{
return urlConn.getContentType();
}
public int getLength()
{
return urlConn.getContentLength();
}
}
class test1
{
public static void main(String[] args)throws Exception
{
GUI gui = new GUI();
gui.show();
}
}
但不应该只能这么写吧 太难看了