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

我是新手一个简单问题搞不懂了:(请各位大虾指教一二~
一个文件传输的发送端程序~提示E:\javasheji\Filesend.java:51: 非法的表达式开始
public void run()
^

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;
import java.net.Socket;
public class Filesend extends JFrame implements ActionListener,Runnable
{ JButton b1;
TextArea txt;
JFileChooser fc;
File file;
Thread thread;

Filesend()  
 { super("文件传输");
  this.setLocation(100,100);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  fc = new JFileChooser();
b1=new JButton("Send a File...",new ImageIcon("go.gif"));
  txt=new TextArea(10,40);
JScrollPane scrollpane = new JScrollPane(txt);
JPanel jp = new JPanel();
jp.add(b1);
getContentPane().add(jp, BorderLayout.NORTH);  
  getContentPane().add(scrollpane, BorderLayout.CENTER);
  b1.addActionListener(this);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  pack();
  setVisible(true);
 }

public void actionPerformed(ActionEvent e) 
{
  if (e.getSource() == b1) 
  {int returnVal = fc.showOpenDialog(this);
  if (returnVal == JFileChooser.APPROVE_OPTION)
  {file = fc.getSelectedFile();
  thread =new Thread(this);
  thread.start();
  txt.append("发送:"+file.getAbsolutePath()+"\n");
  } 
  else { System.out.println("发送取消");}
  }  
  //}

//class thread extends Thread 
//{  
  // DataInputStream in;
// DataOutputStream out;

public void run()
  {  
  try{
  Socket socket;
  socket=new Socket("localhost",4321);
  DataInputStream in = new DataInputStream(socket.getInputStream());;
DataOutputStream out;
  out = new DataOutputStream(socket.getOutputStream());
  out.writeUTF(file.getName());
  txt.append("发送文件名:" + file.getName()+"\n");
  boolean isAccepted = in.readBoolean();
 
  if(isAccepted){
  txt.append("开始发送"+"\n");
  FileInputStream fis = new FileInputStream(file);
  BufferedInputStream bin=new BufferedInputStream(fis);
  byte[] b = new byte[(int)file.length()];
  int num = bin.read(b);
  while(num!= -1) {out.write(b,0,num); out.flush(); } 
  bin.close();in.close();out.close(); socket.close();
  txt.append("Send file finished:" + file.toString());
  }
  } catch (IOException e1) { txt.append("发送失败"+"\n");}
 //finally{
  // try { //in.close();out.close();
  // } catch (IOException e) { e.printStackTrace(); }
  }
}
//}
   
public static void main(String args[])
  { new Filesend();
  }
}


------解决方案--------------------
49行那少个夸号 
被你多注释了
------解决方案--------------------
if (returnVal == JFileChooser.APPROVE_OPTION) 
{file = fc.getSelectedFile(); 
thread =new Thread(this); 
thread.start(); 
txt.append( "发送: "+file.getAbsolutePath()+ "\n "); 
}