Save user profile photo when reloading to chat system using socket

Asked

Viewed 220 times

-1

I developed a Java chat using sockets where you have to choose a profile photo after logging in.

My question is: How would I save this photo chosen by the user and reuse it in a future login? In the same way that is Facebook when you put a photo, it appears there again in profile.

The code I put below is to take the photo, it takes the image and plays on a Jlabel:

foto.addMouseListener(new MouseListener() {

        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub
        }

        @Override
        public void mousePressed(MouseEvent e) {
            // TODO Auto-generated method stub
        }

        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            foto.setToolTipText("Altere sua foto.");
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            e.getComponent().setCursor(
                    Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            JFileChooser chooser = new JFileChooser();

            FileNameExtensionFilter filter = new FileNameExtensionFilter(
                    "png", "jpg", "gif");

            chooser.setFileFilter(filter);

            int returnVal = chooser.showOpenDialog(getParent());

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                BufferedImage image = null;
                try {
                    image = ImageIO.read(file);
                    foto.setIcon(new ImageIcon(image.getScaledInstance(130,
                            130, Image.SCALE_DEFAULT)));
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    });
  • Could you explain exactly where you are having a problem? Besides, how your code relates to your doubt?

  • Sorry for my lack of attention I edited the question, and I appreciate any help.

  • Hi, Jonathan, welcome to [en.so]. The site works differently from forums and social networks, the [Tour] is quick and explains well. I gave a simplified in your text to get straight to the point.

  • This question cannot be answered because the code provided has little relation to the given problem. The code just loads an image onto the disk and shows on the screen, and this has little to do with sockets. Furthermore, this question is abandoned, it has already been closed and reopened and I was already going to vote to close again.

1 answer

0

Persist this information on your chat server. In a very simplistic way, you can create a directory on the machine where your chat server is (using the user id as the photo name, for example). Then when the user reconnects to the server, you return the byte photo array to the socket and mount to the client.

  • Can you give an example?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.