黑马程序员技术交流社区

标题: swing上传文件,求大神运行,修改下, 这里不知道怎么错了 [打印本页]

作者: ノtrack    时间: 2014-5-8 12:33
标题: swing上传文件,求大神运行,修改下, 这里不知道怎么错了
  1. import java.net.ServerSocket;
  2. import java.net.Socket;

  3. /**
  4. * 9、 使用TCP协议写一个可以上传文件的服务器和客户端。
  5. *
  6. * @author 刘阳文
  7. * */
  8. public class Test9
  9. {
  10.         public static void main(String[] args) throws Exception
  11.         {
  12.                 ServerSocket ss = new ServerSocket(8888);
  13.                 while(true)
  14.                 {
  15.                         Socket socket = ss.accept();
  16.                         new Thread(new TcpServer(socket)).start();
  17.                 }
  18.         }
  19. }
复制代码

  1. import java.io.BufferedInputStream;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.net.Socket;

  8. /**
  9. * Tcp文件上传  服务端
  10. *
  11. * @author 刘阳文
  12. * */
  13. public class TcpServer implements Runnable
  14. {
  15.         private Socket socket;
  16.         public TcpServer(Socket socket)
  17.         {
  18.                 this.socket = socket;
  19.         }
  20.         public void run()
  21.         {
  22.                 OutputStream  outputStream  = null;
  23.                 try
  24.                 {
  25.                         InputStream inputStream = socket.getInputStream();
  26.                         BufferedInputStream bis = new BufferedInputStream(inputStream);
  27.                         bis.mark(0);
  28.                         byte[] buf = new byte[1024];
  29.                         int len = 0;
  30.                         len = bis.read(buf);
  31.                         String str = new String(buf,0,len);
  32.                         String[] strs = str.split("----");
  33.                         bis.reset();
  34.                         bis.skip((strs[0].length()+"----".length())+(int)8);
  35.                         String[] fileNames = strs[0].substring(strs[0].lastIndexOf(File.separatorChar)+1).split("\\.");
  36.                         int count = 0;
  37.                         String fileName = fileNames[0]+"("+(++count)+")."+fileNames[1];
  38.                         File file = new File(fileName);
  39.                         if(file.exists())
  40.                                 file = new File(fileName);
  41.                         outputStream = new FileOutputStream(file);
  42.                         while((len = bis.read(buf)) > 0)
  43.                         {
  44.                                 outputStream.write(buf, 0, len);
  45.                         }
  46.                        
  47.                 } catch (IOException e)
  48.                 {
  49.                         e.printStackTrace();
  50.                 }
  51.                 finally
  52.                 {
  53.                         try
  54.                         {
  55.                                 if(outputStream != null)
  56.                                         outputStream.close();
  57.                         } catch (Exception e2)
  58.                         {
  59.                                 System.out.println("文件写入流关闭异常");
  60.                         }
  61.                         try
  62.                         {
  63.                                 if(socket != null)
  64.                                         socket.close();
  65.                         } catch (Exception e2)
  66.                         {
  67.                                 System.out.println("socket关闭异常");
  68.                         }
  69.                 }
  70.                
  71.         }
  72. }
复制代码


  1. import java.awt.Button;
  2. import java.awt.FileDialog;
  3. import java.awt.FlowLayout;
  4. import java.awt.Frame;
  5. import java.awt.Label;
  6. import java.awt.TextArea;
  7. import java.awt.TextField;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.KeyAdapter;
  11. import java.awt.event.KeyEvent;
  12. import java.awt.event.WindowAdapter;
  13. import java.awt.event.WindowEvent;
  14. import java.io.File;
  15. import java.io.FileInputStream;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.OutputStream;
  19. import java.io.PrintWriter;
  20. import java.net.InetSocketAddress;
  21. import java.net.Socket;

  22. /**
  23. * Tcp文件上传  客户端
  24. * */
  25. @SuppressWarnings("serial")
  26. public class TcpClient extends Frame
  27. {
  28.         private TextField ipField ;
  29.         private TextField porField;
  30.         private Label iplLabel;
  31.         private Label portlLabel;
  32.         private TextField filePath ;
  33.         private Label fileLabel;
  34.         private FileDialog openFile;
  35.         private Button openButton;
  36.         private TextArea info;
  37.         private Button send;
  38.         private Socket socket;
  39.         private InetSocketAddress isa;
  40.      public TcpClient (Socket socket)
  41.         {
  42.              super("客户端    文件上传");
  43.              this.socket = socket;
  44.                 init();
  45.         }
  46.      private void init()
  47.      {
  48.              setBounds(300, 300, 500, 200);
  49.              setLayout(new FlowLayout());
  50.              
  51.              ipField = new TextField(30);
  52.              ipField.setText("127.0.0.1");
  53.              iplLabel = new Label("服务端IP地址:");
  54.              
  55.              porField = new TextField(5);
  56.              porField.setText("8888");
  57.              portlLabel = new Label("端口号:");
  58.              
  59.              fileLabel = new Label("文件路径:");
  60.              filePath = new TextField(35);
  61.              openButton = new Button("选择文件");
  62.              send = new Button("上传文件");
  63.              info = new TextArea(5,65);
  64.              add(iplLabel);
  65.              add(ipField);
  66.              add(portlLabel);
  67.              add(porField);
  68.              
  69.              add(fileLabel);
  70.              add(filePath);
  71.              add(openButton);
  72.              add(send);
  73.              add(info);
  74.              
  75.              actionEvent();
  76.              windowEvent();
  77.              keyEvent();
  78.              
  79.              setVisible(true);
  80.      }
  81.      public static void main(String[] args)
  82.         {
  83.              Socket socket = new Socket();
  84.              new TcpClient(socket);
  85.         }
  86.      private void actionEvent()
  87.      {
  88.              openButton.addActionListener(new ActionListener()
  89.                 {
  90.                        
  91.                         @Override
  92.                         public void actionPerformed(ActionEvent e)
  93.                         {
  94.                                 openFile = new FileDialog(TcpClient.this,"选择上传的文件",FileDialog.LOAD);
  95.                                 openFile.setVisible(true);
  96.                                 String path = openFile.getDirectory();
  97.                                 String file = openFile.getFile();
  98.                                 if(path != null && file != null){
  99.                                         filePath.setText(path+file);
  100.                                 }
  101.                         }
  102.                 });
  103.              send.addActionListener(new ActionListener()
  104.                 {
  105.                        
  106.                         @Override
  107.                         public void actionPerformed(ActionEvent e)
  108.                         {
  109.                                 int port=0;
  110.                                 InputStream inputStream = null;
  111.                                 try
  112.                                 {
  113.                                         System.out.println("上传文件"+filePath.getText());
  114.                                
  115.                                         port = Integer.parseInt(porField.getText());
  116.                                         isa =new InetSocketAddress(ipField.getText(),port);
  117.                                         if(!socket.isConnected()){
  118.                                                 socket.connect(isa);
  119.                                                 inputStream = new FileInputStream(new File(filePath.getText()));
  120.                                                
  121.                                                 OutputStream outputStream = socket.getOutputStream();
  122.                                                
  123.                                                 outputStream.write((filePath.getText()+"----").getBytes());
  124.                                                
  125.                                                 byte[] buf = new byte[1024];
  126.                                                 int len = 0;
  127.                                                 while((len = inputStream.read(buf)) > 0)
  128.                                                 {
  129.                                                         outputStream.write(buf, 0, len);
  130.                                                 }       
  131.                                                 System.out.println("上传成");
  132.                                         }
  133.                                         filePath.setText("");
  134.                                 } catch (Exception e2)
  135.                                 {
  136.                                         e2.printStackTrace();
  137.                                 }
  138.                                 finally
  139.                                 {
  140.                                         try
  141.                                         {
  142.                                                 if(inputStream != null )
  143.                                                         inputStream.close();
  144.                                         } catch (Exception e3)
  145.                                         {
  146.                                                 System.out.println("文件读取流关闭异常");
  147.                                         }
  148.                                 }
  149.                                
  150.                         }
  151.                 });
  152.      }
  153.      private void windowEvent()
  154.      {
  155.              //关闭窗口
  156.              this.addWindowListener(new WindowAdapter()
  157.              {
  158.                      @Override
  159.                      public void windowClosing(WindowEvent event)
  160.                      {
  161.                              System.exit(0);
  162.                      }
  163.              });
  164.      }
  165.      private void keyEvent()
  166.      {
  167.              ipField.addKeyListener(new KeyAdapter()
  168.                 {
  169.                     // 35065 35071 35073
  170.                      @Override
  171.                      public void keyPressed(KeyEvent event)
  172.                      {
  173.                              //ip地址只能输入. 0-9  数字键盘区
  174.                              if(!((event.getKeyCode()>=96 && event.getKeyCode()<=105) || event.getKeyCode()==110))
  175.                                      event.consume();
  176.                      }
  177.                 });
  178.              porField.addKeyListener(new KeyAdapter()
  179.                 {
  180.                      @Override
  181.                      public void keyPressed(KeyEvent event)
  182.                      {
  183.                              //端口只能输入地址
  184.                              if(!(event.getKeyCode()>=96 && event.getKeyCode()<=105))
  185.                                      event.consume();
  186.                      }
  187.                 });
  188.      }
  189. }
复制代码

作者: ノtrack    时间: 2014-5-8 12:34
帮忙运行 修改下 搞了半天




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2