日期:2014-05-20  浏览次数:20858 次

哪位大侠帮我看一下这个“文件传输”代码(文件长度穿不过去)
调了一下午了,长度死活穿不过去


服务器端:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Man1 extends JFrame {


JLabel lab = null;
JTextField jf = null;
JButton btn = null;
JButton btnSend = null;
File f1 = null;

FileInputStream fis = null;
BufferedInputStream bis = null;

BufferedOutputStream bos =null;

Socket sk = null;
ServerSocket ssk = null;


public Man1() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setBounds(100, 100, 500,80);
this.setTitle("简单的文件传输程序");
this.setResizable(false);
lab = new JLabel("路径:");
jf = new JTextField(20);
btn = new JButton ("选择要传送的文件");
btnSend = new JButton ("发送");
JPanel pan =(JPanel) this.getContentPane();
pan.setLayout(new FlowLayout());
pan.add(lab);
pan.add(jf);
pan.add(btn);
pan.add(btnSend);
this.setVisible(true);
try {
ssk = new ServerSocket(7778);
System.out.println("服务器正在监听....");
sk = ssk.accept();
System.out.println("有客户端连接上....");
} catch (IOException e1) {
e1.printStackTrace();
}
//消息处理
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser jfc = new JFileChooser();
int flag = jfc.showOpenDialog(null);
if(flag==jfc.APPROVE_OPTION){
f1 = jfc.getSelectedFile();
String path = f1.getAbsolutePath();
jf.setText(path);
}
}
});
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

//发送文件
sendFile();
}
});
}
public void sendFile(){
String path = f1.getAbsolutePath();
String name = f1.getName()+";";
String len = f1.length()+"";

try {

fis = new FileInputStream(new File(path));
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(sk.getOutputStream());

bos.write(name.getBytes());
bos.flush();
// ?????????????此处len穿不过去
bos.write((len+";").getBytes());
bos.flush();

byte [] length = new byte[Integer.parseInt(len)];
int i=0;
while((i=fis.read(length))!=-1){
bos.write(length);
}
bos.flush();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try{
if(sk!=null){
sk.close();
}
if(ssk!=null){
ssk.close();
}
if(fis!=null){
fis.close();
}
if(bos!=null){
bos.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new Man1();
}

}
============================================================================
============================================================================
客户端: