一个基于tcp协议的网络文件传输程序(java)
/ns/wz/comp/data/20070428180723.htm
大家好
我是一个新朋友热爱java。
这个程序只是一个范例,让大家了解象ftp类的文件传输程序的变成思路。
但我有一个问题:如何让程序在局域网内的ip限制啊。
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
/*网络传送带1.0 单线程,一次性*/
public class csd1 extends JFrame implements Runnable,ActionListener{
private static final long serialVersionUID = -5147144742761315168L;
JTextArea inMessage=new JTextArea(12,20),outMessage=new JTextArea(12,20);
JTextField text1,text2,text3;
JButton sure=new JButton("确定"),save=new JButton("保存");
Box box;
String str[]=new String[3];
csd1(String s)
{
super(s);
setBounds(320,200,400,400);
setVisible(true);
JPanel p1=new JPanel();
sure.addActionListener(this);
text1=new JTextField(16);
p1.add(new JLabel("输入目标地址"));
p1.add(text1);
p1.add(sure);
text2=new JTextField(18);
JPanel p2=new JPanel();
p2.add(new JLabel("文件路径"));
p2.add(text2);
JPanel p3=new JPanel();
text3=new JTextField(18);
save.addActionListener(this);
p3.add(new JLabel("接收 保存到:"));
p3.add(text3);
p3.add(save);
box=Box.createVerticalBox();
box.add(p1);
box.add(p2);
box.add(outMessage);
box.add(p3);
box.add(inMessage);
Container con=getContentPane();
con.add(box,BorderLayout.CENTER);
validate();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Thread Thread=new Thread (this);
str=setread();
text1.setText(str[0]);
text2.setText(str[1]);
text3.setText(str[2]);
Thread.start();
}
private String[] setread() {
File setfile=new File("d:\\setimfor");
try
{
FileReader in1=new FileReader(setfile);
BufferedReader in2=new BufferedReader(in1);
String str[]=new String[3];
for(int i=0;i<3;i++)
{
str[i]=in2.readLine();
}
return str;
}
catch(IOException e1)
{
System.out.println(e1);
return null;
}
}
public void run() {//线程run
str=setread();
byte b[]=new byte[1024];
String name=null,lenString=null;
RandomAccessFile fout=null;
try{
inMessage.append("正在监听断口1234\n");
ServerSocket server=new ServerSocket(1234);
Socket mySocket=server.accept();
InputStream in=mySocket.getInputStream();
int amount=0,i1=0,i2=0;
amount=in.read(b);//读取文件头
name=new String(b);
i1=name.indexOf(";");
i2=name.indexOf(";",i1+1);
lenString=name.substring(i1+1,i2);
name=name.substring(0, i1);
fout=new RandomAccessFile(str[2]+""+name,"rw");
inMessage.append("收到文件"+name+"\n"+"文件长度"+lenString+"\n");
//读取文件剩余数据
while((amount=in.read(b))!=-1){
fout.write(b, 0, amount);
}
fout.close();
in.close();
mySocket.close();
}
catch(IOException e){
inMessage.append("接收文件出错;"+e+"\n");
}
}
public void actionPerformed(ActionEvent e) {//动作监听
if(e.getSource()==sure)
{
str=setread();
int i=str[1].lastIndexOf("\\");
String name=str[1].substring(i+1);
byte b[]=new byte[1024];
try {
RandomAccessFile fin=new RandomAccessFile(str[1],"rw");
Socket sendSocket=new Socket(str[0],1234);
OutputStream out=sendSocket.getOutputStream();
out.write((name+";"+String.valueOf(fin.length())+";").getBytes());
i=0;
while((i=fin.read(b))!=-1){
out.write(b, 0, i);
}
fin.close();
out.close();
sendSocket.close();
outMessage.append(name+"传输完毕\n");
} catch (IOException e1) {
// TODO 自动生成 catch 块
inMessage.append("发送文件出错:"+e1);
}
}
else if(e.getSource()==save)
{
setwrite(text1.getText(),text2.getText(),text3.getText());
inMessage.append("设置以保存\n");
str=setread();
}
}
void setwrite(String s1, String s2, String s3) {
File setfile=new File("d:\\setimfor");
try
{
FileWriter out1=new FileWriter(setfile);
BufferedWriter out2=new BufferedWriter(out1);
out2.write(s1);
out2.newLine();
out2.write(s2);
out2.newLine();
out2.write(s3);
out2.flush();
out2.close();
out1.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
public static void main(String[] args) {
new csd1("网络传送带ptp版1.0(夏夏制作)");
}
}
===============================================
本文版权属20CN网络安全小组及其作者所有,如有转载,请保持文章完整性并注明出处
文章类型:原创 提交:catcher19 核查:NetDemon