Java网络编程,高手进

发布网友 发布时间:2022-04-21 12:49

我来回答

3个回答

热心网友 时间:2023-07-21 19:10

package org.kevin.ftp;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class DLFrame extends JFrame
{
private JPanel contentPane;
private JTextField textField1=new JTextField();
private JTextField textField2=new JTextField();
private JButton button=new JButton();
private JLabel label1=new JLabel();
private JLabel label2=new JLabel();
private JTextArea textArea=new JTextArea();

private String dlURL=new String();
private String saveFileAs=new String();

public DLFrame()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
toInit();

}
catch (Exception ex)
{
ex.printStackTrace();
}
}

private void toInit() throws Exception
{
contentPane=(JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(380,320));
this.setLocation(100,100);
this.setTitle("多线程下载");
textField1.setBounds(new Rectangle(150,200,200,20));
textField1.setText("http://www.javafan.net/mydown1010/javawebrobot.rar");
textField2.setBounds(new Rectangle(150,240,120,20));
textField2.setText("d:\\try.rar");
label1.setBounds(new Rectangle(20,200,120,20));
label1.setText("下载的目标文件为: ");
label2.setBounds(new Rectangle(20,240,120,20));
label2.setText("下载的文件另存为: ");
button.setBounds(new Rectangle(280,240,60,20));
button.setText("下载");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
button_actionPerformed(e);
}
});
JScrollPane scrollPane = new JScrollPane(textArea);
//textArea.setBounds(new Rectangle(20,20,330,170));
scrollPane.setBounds(new Rectangle(20,20,330,170));
textArea.setEditable(false);

contentPane.add(textField1,null);
contentPane.add(textField2,null);
contentPane.add(label1,null);
contentPane.add(label2,null);
contentPane.add(button,null);
//contentPane.add(textArea,null);
contentPane.add(scrollPane,null);
dlURL=textField1.getText();
saveFileAs=textField2.getText();

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public void button_actionPerformed(ActionEvent e)
{
//启动分析下载文件的进程
dlURL=textField1.getText();
saveFileAs=textField2.getText();
if(dlURL.compareTo("")==0 && saveFileAs.compareTo("")==0)
{
textArea.setText("请输入要下载的文件和保存文件完整地址");
}
else
{
try
{
DownFile downFile=new DownFile(dlURL,saveFileAs,5,textArea);
downFile.start();
textArea.append("主线程启动、、、");
}
catch (Exception ex)
{
ex.printStackTrace();
}

}

}

public static void main(String[] args)
{
new DLFrame().setVisible(true);//要实例化一个对象的时候才会去执行构造方法

}
}

package org.kevin.ftp;
import javax.swing.JTextArea;
import java.net.*;
import java.io.*;

public class DownFile extends Thread //分析要下载的文件并启动下载进程
{
String dlURL;
String saveFileAs;
int nthread;
String info=new String();
JTextArea textArea=new JTextArea();

long[] position;
long[] startPos;
long[] endPos;
DownFileSplitter[] downFileSplitter; //子线程对象
long fileLength;
boolean stop=false;
//DataOutputStream output;

public DownFile(String sURL,String saveFileAs,int nthread,JTextArea textArea) throws IOException
{
this.dlURL=sURL;
this.saveFileAs=saveFileAs;
this.nthread=nthread;
this.textArea=textArea;

startPos=new long[nthread];
endPos=new long[nthread];
}

public void run()
{
info="目标文件: "+dlURL;
System.out.println(info);
textArea.append("\n"+info);
info="\n 线程总数: "+nthread;
System.out.println(info);
textArea.append("\n"+info);

try
{
fileLength=getFileSize();
if (fileLength==-1)
{
System.err.println("不可知的文件长度");
textArea.append("\n 不可知的文件长度!请重试!!");
}
else
{
if(fileLength==-2)
{
System.err.println("文件无法获取!");
textArea.append("\n 文件无法获取!请重试!!");
}
else
{
for(int i=0;i<startPos.length;i++){
startPos[i]=(long) (i*(fileLength/startPos.length));
}
for(int i=0;i<endPos.length-1;i++)
endPos[i]=startPos[i+1];
endPos[endPos.length-1]=fileLength;
for(int i=0;i<startPos.length;i++)
{
info="线程:"+i+"下载范围:"+startPos[i]+"--"+endPos[i];
System.out.println(info);
textArea.append("\n"+info);
}
downFileSplitter=new DownFileSplitter[startPos.length];
//启动一组子线程

for(int i=0;i<startPos.length;i++)
{
downFileSplitter[i]=new DownFileSplitter(dlURL,saveFileAs,
startPos[i],endPos[i], i,textArea);
info="线程 "+i+"启动";
textArea.append("\n"+info);
downFileSplitter[i].start();
System.out.println(info);
}

boolean breakWhile=true;
while(breakWhile)
{
Thread.sleep(500);
breakWhile=false;
for(int i=0;i<downFileSplitter.length;i++)
{
if(!downFileSplitter[i].done)
{
breakWhile=true;
break;
}
}
}
System.out.println("文件传输结束!");//文件传输结束
textArea.append("\n 文件传输结束!");
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}

}
public long getFileSize()
{
int fileLength=-1;
try
{
URL url=new URL(dlURL);
HttpURLConnection httpConnection=(HttpURLConnection) (url.openConnection());
int responseCode=httpConnection.getResponseCode();
if(responseCode>=400)
{
System.out.println("Web服务器响应错误");
return -2;//Web服务器响应错误
}
String sHeader;
for(int i=1;;i++)//查找标识文件长度的文件头,获取文件长度
{
sHeader=httpConnection.getHeaderFieldKey(i);
if(sHeader!=null)
{
if(sHeader.equals("Content-Length"))
{
fileLength=Integer.parseInt(httpConnection.getHeaderField(sHeader));
break;
}
}
else
{
break;
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
return fileLength;

}
}

package org.kevin.ftp;

import javax.swing.JTextArea;
import java.net.*;
import java.io.*;

public class DownFileSplitter extends Thread
{
String dlURL;
long startPos;
long endPos;
int threadID;
JTextArea textArea=new JTextArea();
boolean done=false;
boolean stop=false;
RandomAccessFile file;

public DownFileSplitter(String dlURL,String saveAs,long nStart,long nEnd,
int id,JTextArea textArea) throws IOException
{
this.dlURL=dlURL;
this.startPos=nStart;
this.endPos=nEnd;
this.threadID=id;
this.textArea=textArea;
file=new RandomAccessFile(saveAs,"rw");
file.seek(startPos);
}

public void run()
{
try
{
URL url=new URL(dlURL);
HttpURLConnection httpConnection=(HttpURLConnection)url.openConnection();
String sProperty="bytes="+startPos+"-";
httpConnection.setRequestProperty("RANGE",sProperty);
System.out.println("线程"+threadID+"下载文件、、、请等待");
textArea.append("\n 线程"+threadID+"下载文件、、、请等待");
InputStream input=httpConnection.getInputStream();
byte[] buf=new byte[1024];
int offset;
offset=(int)endPos-(int)startPos;
if(offset>1024)
offset=1024;
while(input.read(buf,0,offset)>0 && startPos<endPos)
{
offset = (int) endPos - (int) startPos;
if (offset > 1024)
offset = 1024;
System.out.println("threadID: "+threadID+" started: "+startPos+" offset: "+offset);
file.write(buf,0,offset);
startPos+=offset;
}
System.out.println("线程"+threadID+"下载完毕!!");

textArea.append("\n 线程"+threadID+"下载完毕!!");
file.close();
input.close();
done=true;
}
catch (Exception ex)
{
ex.printStackTrace();
}

}
}

热心网友 时间:2023-07-21 19:11

 
 
 
你可以调用 java.net.URLConnection.getContentLength( )。
中文文档:http://shorterlink.com/?WSX10K

import java.net.URL;

class C {
    public static void main( String[ ] args ) throws Exception {
        URL url = new URL( "http://www.python.org/ftp/python/2.5/python-2.5.msi" );
        int size = url.openConnection( ).getContentLength( );
        System.out.printf( "%s is only %.1f MB in size.", url, size / Math.pow( 2, 20 ) );
    }
}
 
 
 

热心网友 时间:2023-07-21 19:11

SmartUpload用这个包里的功能,网上有好多api资料,看看挺好用的

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com