ITPub博客

首页 > Linux操作系统 > Linux操作系统 > java程序之TCP传输文件程序

java程序之TCP传输文件程序

原创 Linux操作系统 作者:rain0903 时间:2019-03-30 15:18:06 0 删除 编辑
代码: [ 复制到剪贴板 ]
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 ");
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+" "+"文件长度"+lenString+" ");

//读取文件剩余数据
while((amount=in.read(b))!=-1){

fout.write(b, 0, amount);
}

fout.close();
in.close();
mySocket.close();
}
catch(IOException e){
inMessage.append("接收文件出错;"+e+" ");

}



}

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+"传输完毕 ");
} catch (IOException e1) {
// TODO 自动生成 catch 块
inMessage.append("发送文件出错:"+e1);
}


}
else if(e.getSource()==save)
{
setwrite(text1.getText(),text2.getText(),text3.getText());
inMessage.append("设置以保存 ");
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("网络传送");


}

}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7178747/viewspace-160918/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2006-04-07

  • 博文量
    265
  • 访问量
    184264