使用java套接字请求文件并传输文件

时间:2016-01-29 15:56:14

标签: java sockets file-transfer lan

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         



    try{
            Socket socket = new Socket(jTextField1.getText(), 15123);
    OutputStream oStream = new BufferedOutputStream(socket.getOutputStream());
    String name = jTextField2.getText();
            DataOutputStream d = new DataOutputStream(oStream);
              d.writeUTF(name);

    oStream.flush();
    oStream.close();

}catch(IOException e){
}
}                
   public static void main(String args[]) throws IOException {

    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new testGUI().setVisible(true);


    try {

         ServerSocket serverSocket3 = new ServerSocket(15123);
        Socket socket3 = serverSocket3.accept();
        InputStream iStream3 = socket3.getInputStream();
        DataInputStream d3 = new DataInputStream(iStream3);
        InetAddress ip = socket3.getInetAddress();
        String t = d3.readUTF();

        Socket socket1 = new Socket(ip.toString(), 3333);
        OutputStream oStream1 = new BufferedOutputStream(socket1.getOutputStream());



        File folder = new File("C:");

        File[] listOfFiles = folder.listFiles();

    for(int i = 0; i < listOfFiles.length; i++){


        String filename = listOfFiles[i].getName();

                String fi = d3.toString();
        String[] fn = fi.split("\\.");

        if(filename.startsWith(fn[0]) && listOfFiles[i].getName().endsWith(fn[1])){

                     DataOutputStream d1 = new DataOutputStream(oStream1);
                     d1.writeUTF(d3.toString());

                     File file1 = new File(folder.toString()+"/"+filename);
                     InputStream iStream1 = new FileInputStream(file1);
                      byte[] buffer = new byte[(int) file1.length()];
        for (int readCount = iStream1.read(buffer); readCount != -1; readCount = iStream1.read(buffer)) {
            oStream1.write(buffer, 0, readCount);
        }
        oStream1.flush();
        oStream1.close();
        iStream1.close();

        }
    }


    } catch (IOException e) {

    }




        }


    });


    try {
        ServerSocket serverSocket1 = new ServerSocket(3333);
        Socket socket1 = serverSocket1.accept();
       InetAddress ip = socket1.getInetAddress();
        InputStream iStream1 = socket1.getInputStream();
        DataInputStream d = new DataInputStream(iStream1);

        FileOutputStream oStream1 = new FileOutputStream(d.readUTF());

        byte[] buffer = new byte[8192];

        int count;
        while ((count = iStream1.read(buffer)) > 0) {
            oStream1.write(buffer, 0, count);
        }

        oStream1.flush();
        oStream1.getFD().sync();
        oStream1.close();
        iStream1.close();

        JOptionPane.showMessageDialog(null, "File recived"+ip.toString(), "Notification", JOptionPane.INFORMATION_MESSAGE);

    } catch (IOException e) {

}
}

问题 - (netbean代码)

1)未加载GUI
2)不将请求文件发送到另一台电脑。

我的任务是;如果某人从某人请求文件,则搜索表单共享位置(按定义)并获取该文件并将其发送回请求者。

需要帮助

0 个答案:

没有答案