Grab a network java file

Asked

Viewed 992 times

1

I managed to open a file and put it on a Jlabel on the machine where I am developing. But when I put the application on the network the machine that is on the network, it does not locate the file. Is it possible to get the file from a networked machine? My code is as follows and works perfectly on my machine that I am developing.

String figura = jtResultadoPesquisa.getValueAt(jtResultadoPesquisa.getSelectedRow(), 0).toString(); // quardo o nome do arquivo na figura

String caminho = new File("/home/Pictures/").getCanonicalPath(); // pego o diretorio onde esta o arquivo

ResultadoCaminhoFigura = caminho +"/"+ figura; // tenho o caminho com a figura

ImageIcon img = new ImageIcon (ResultadoCaminhoFigura); //crio uma instancia e coloco a figura e diretorio onde ela se localiza
img.setImage(img.getImage().getScaledInstance(300, 430, 100));

jlVisualisarimagem.setIcon(img); // e seto a figura em um jlabel para ser visualizada.
jlVisualisarimagem.setHorizontalAlignment(JLabel.CENTER);
jspVisualizarimagem.getViewport().add(jlVisualisarimagem);

I posted the comments just to facilitate understanding. When I run on my machine it works cool, but when I networked the application it does not bring the image. Could someone help me? Hugs to all.

1 answer

1

Although your question is wide, you should not be able to access the figure because this path is local (proper to your computer):

String caminho = new File("/home/Pictures/").getCanonicalPath();

When you run the program on other machines, they search their own directory, not your computer (where the required image is). One way they access your computer to search for the image is through the IP. For example:

String caminho = new File("172.168.1.50/home/Pictures/").getCanonicalPath();

Where 172.168.1.50 is the IP address of your computer. Of course you will need the necessary access permissions for reading the image, among other settings. Remembering that this is just an example of access for networked computers.

  • G Zullian, besides doing that you put I had to share the folder that is on the server. Since the server is I installed samba, I shared the folder and used the ip address you described to locate the shared folder on the server. Thanks. Thank you so much for your help.

  • Exactly, when I talked about permissions and other settings, that’s what it was all about. Glad you made it, I ask you to accept the answer to help other users. Hug!

Browser other questions tagged

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