请问怎样通过socket通道传送完图片后,再显示在JTextPane上

解决方案 »

  1.   

    下面是我的代码,但是我不知道具体该怎样写,试了几次都不太行
    import java.awt.EventQueue;
    public class QQ<FontAttrib> extends JFrame { JFrame frame;
    private JScrollPane scrollPane_1 = null; // 滚动
    private JTextPane textPane_1 = null;  
     private StyledDocument doc = null; // 非常重要插入文字样式就靠它了
     private StyledDocument dic = null; // 非常重要插入文字样式就靠它了
     private JTextPane textPane = null;  
       Socket s=null;
           ObjectOutputStream oos=null;
           ServerSocket ss=null;
          
           FileOutputStream fos=null;
           ObjectInputStream ois=null;
           FileInputStream fis=null;
           Vector picVector = new Vector();
           private JTextField textField;
           int length=1024;
       byte[] sendBytes=new byte[1024];
       GregorianCalendar time=new GregorianCalendar();
    int a1=0;
    String sj=new String(""+time.get(GregorianCalendar.YEAR)+
            +time.get(GregorianCalendar.MONTH)+
            +time.get(GregorianCalendar.DATE)+
            +time.get(GregorianCalendar.HOUR)+
            +time.get(GregorianCalendar.MINUTE)+
            +time.get(GregorianCalendar.SECOND));
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {

    try {
    QQ window = new QQ();
    window.frame.setVisible(true);

    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    } /**
     * Create the application.
     */
    public QQ() {

    initialize();
    } /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 625, 550);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    scrollPane_1 = new JScrollPane(textPane_1);


    final JTextPane textPane = new JTextPane();
    textPane.setBounds(0, 0, 442, 291);
    frame.getContentPane().add(textPane);
     dic = textPane.getStyledDocument();

    final JTextPane textPane_1 = new JTextPane();
    textPane_1.setBounds(0, 301, 442, 146);
    frame.getContentPane().add(textPane_1);
     doc = textPane_1.getStyledDocument();
     
     textField = new JTextField();
    textField.setBounds(10, 485, 432, 21);
    frame.getContentPane().add(textField);
    textField.setColumns(10);
     
    JButton button = new JButton("\u56FE\u7247");
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
     JFileChooser f = new JFileChooser(); // 查找文件
      f.showOpenDialog(null);
      insertIcon(f.getSelectedFile()); 
      textPane_1.insertIcon(new ImageIcon(f.getSelectedFile().toString()));
     textField.setText(f.getSelectedFile()+"");

    } private void insertIcon(File file) {
    // TODO Auto-generated method stub

     textPane_1.setCaretPosition(doc.getLength()); // 设置插入位置
     textPane_1.insertIcon(new ImageIcon(file.getPath())); // 插入图片

     
    }
    });

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(440, 301, -444, 146);
    frame.getContentPane().add(scrollPane);
    button.setBounds(10, 457, 93, 23);
    frame.getContentPane().add(button);



    JButton button_1 = new JButton("\u6E05\u7A7A");
    button_1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    textPane_1.setText("");
    }
    });
    button_1.setBounds(246, 457, 93, 23);
    frame.getContentPane().add(button_1);

    JButton button_2 = new JButton("\u53D1\u9001");
     button_2.addActionListener(new ActionListener()  
      { // 插入文字的事件
      public void actionPerformed(ActionEvent e)  
      {
     
      } private void insert(Object fontAttrib) {
    // TODO Auto-generated method stub

    }
      });//
    button_2.setBounds(349, 457, 93, 23);
    frame.getContentPane().add(button_2);



    List list = new List();
    list.setBounds(445, 0, 172, 516);
    frame.getContentPane().add(list);

    waitfordata();

    JButton button_3 = new JButton("\u53D1\u9001\u56FE\u7247");
    button_3.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    sendatatupian();
    a1++;
    } private void sendatatupian() {
    // TODO Auto-generated method stub

      Socket s=null;
            ObjectOutputStream oos=null;
            try {
                s=new Socket("127.0.0.1",8888);
                oos=new ObjectOutputStream(s.getOutputStream());
                oos.writeObject(new File(textField.getText()));
                oos.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally{
                if(oos!=null)
                    try {
                        oos.close();
                    } catch (IOException e1) {
                    } 
               
           
                if(s!=null)try {s.close();} catch (IOException e) {}
                StyledDocument a1= textPane_1.getStyledDocument();         }
       
       
     a1++;


    }
    });
    button_3.setBounds(129, 457, 93, 23);
    frame.getContentPane().add(button_3);




    }
     private void insert(FontAttrib attrib) {

     }

    protected Object getFontAttrib() {
    // TODO Auto-generated method stub
    return null;

    } public void waitfordata()  {
    Image ia=null;
            ServerSocket ss=null;
            Socket s=null;
            FileOutputStream fos=null;
            ObjectInputStream ois=null;
            FileInputStream fis=null;
            String a2="f:\\ds"+a1+".jpg";
           
            try {
                ss=new ServerSocket(8888);
                
                while (true) {
                    s = ss.accept();
                    ois = new ObjectInputStream(s.getInputStream());
                    File a=(File)ois.readObject();
                    fos = new FileOutputStream(new File("f:\\ds"+a1+sj+".jpg"));
                    fis=new FileInputStream(a);
                    while(fis.available()>0){
                        fos.write(fis.read());
                          
                    }
                   textPane.insertIcon(new ImageIcon("f:\\ds"+a1+sj+".jpg".toString()));
            textPane.setCaretPosition(doc.getLength()); // 设置插入位置

                   
                  a1++;
                  textPane.insertIcon(new ImageIcon("f:\\ds"+a1+sj+".jpg")); // 插入图片

                }
              
            }
            catch (Exception e) {
            
                e.printStackTrace();
            }finally{
                if(fos!=null)try {fos.close();} catch (IOException e1) {}
                if(fis!=null)try{fis.close();}catch (IOException e) {}
                if(s!=null)try {s.close();} catch (IOException e) {}
                if(ss!=null)try {ss.close();} catch (IOException e) {}
            
            }
          
        } private ImageIcon createImageIcon(String string) {
    // TODO Auto-generated method stub
    return null;
    }

    }