package readwebfile; import java.awt.*; import java.awt.event.*; import java.applet.*; import java.net.*; import java.io.*; public class ReadWebFileApplet extends Applet{ TextField textField1=new TextField(); Button button1=new Button(); TextArea textArea1=new TextArea(); public ReadWebFileApplet(){ try{ jbInit(); } catch(Exception e){ e.printStackTrace(); } } private void jbInit() throws Exception{ textField1.setText("输入网络地址和文件名"); textField1.setBounds(new Rectangle(0,1,179,30)); this.setLayout(null); button1.setLabel("获取网络文件"); button1.setBounds(new Rectangle(186,4,108,28)); button1.addActionListener(new java.awt.event.ActionListener(){ public void actionPerformed(ActionEvent e){ button1_actionPerformed(e); } }); textArea1.setBounds(new Rectangle(1,37,294,261)); this.add(textField1,null); this.add(textArea1,null); this.add(button1,null); } public void ReadURL(String URLName) throws Exception{ int HttpResult; URL url=new URL(URLName); URLConnection urlconn=url.openConnection(); urlconn.connect(); HttpURLConnection httpconn=(HttpURLConnection)urlconn; HttpResult=httpconn.getResponseCode(); if(HttpResult!=HttpURLConnection.HTTP_OK) textArea1.setText("无法连接到"+textField1.getText()); else { int filesize=urlconn.getContentLength(); InputStreamReader isReader=new InputStreamReader(urlconn.getInputStream()); char[] buffer=new char[2048]; int num=0; while(num>-1){ num=isReader.read(buffer); if(num<0) break; textArea1.append(new String(buffer,0,num)); } isReader.close(); } } void button1_actionPerformed(ActionEvent e){ String str=e.getActionCommand(); try{ textArea1.setText(""); ReadURL(textField1.getText()); } catch(Exception ex){ textArea1.setText("读取失败"); } } public static void main(String[] args) { // TODO Auto-generated method stub Frame f=new Frame("获取网络文件"); ReadWebFileApplet readWebFile=new ReadWebFileApplet(); readWebFile.init(); readWebFile.start(); f.add("Center",readWebFile); f.setSize(350,350); f.show(); } } 这是我写的一段多线程下载代码可是总是提示错误 我用的是eclipse3.1M5+j2se1.4 错误如下: 1:声明的包与期望的包 不匹配 ReadWebFile.jav ReadWebFile/src 第一行 2: 2:公共类型 ReadWebFileApplet 必须在它自己的文件中定义 ReadWebFile.java ReadWebFile/src 第 7 行 百思不得其解,有缘人给解。
一个单线程下载方面的问题
最新推荐文章于 2022-08-16 11:23:39 发布