Open a file with Java from a server that requires authentication

2

Good, I am trying to make an application in java, in which the program asks for the name of the document (in my case a pdf) and searches inside a shared folder remotely, if it exists, opens the pdf document . I am currently doing this as follows:

String NomFichero = "file://200.10.10.156/Carpeta/archivo";
if(java.awt.Desktop.isDesktopSupported()) {
   try {
      Desktop dk = Desktop.getDesktop();
      dk.browse(new URI(NomFichero+".pdf"));
   }
   catch(Exception e1) {
      try {
         Desktop dk = Desktop.getDesktop();
         dk.browse(new URI(NomFichero+".jpg"));
      }
      catch(Exception e2) {
         JOptionPane.showMessageDialog(null,"ERROR: "+ e2.getMessage());
      }          
   }    
}  

This works perfectly for me if I log in first from PC to PC, and I recognize the credential for the client team. But the idea is to already access the authentication to avoid this step.

I know of a jcifs library, it's a samba implementation, where if we can access shared folders remotely, and with the credentials in question, I use this library to access the folder at the moment edit the name of a document or delete it, but to open it frankly I can not find a method I can use.
I would appreciate much of your help. Successes!

    
asked by Carlos 20.03.2017 в 21:00
source

1 answer

2

I read in the comments that this is a FTP server , so you should use the aforementioned Apache library, specifically making use of the class FTPClient .

The solution I propose is to attack a Windows computer that hosts a shared folder (or failing that, a server SAMBA ), after authentication using the library JCIFS .

Please note that, at least in the example you propose, the IP address of the computer is public ( NAT must be done if it is not within the same subnet).

Solution:

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;
import jcifs.UniAddress;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbSession;

public class Pruebas {

    public static void main(String args[]) {
        // Autenticación
        try {
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
                "WORKGROUP;Usuario:Password"
            );
            UniAddress dc = UniAddress.getByName("200.10.10.156");
            SmbSession.logon(dc, auth);
        } catch (SmbException | UnknownHostException ex) {
        }

        // Tu código
        String NomFichero = "file://200.10.10.156/Carpeta/archivo";
        if (java.awt.Desktop.isDesktopSupported()) {
            try {
                Desktop dk = Desktop.getDesktop();
                dk.browse(new URI(NomFichero + ".pdf"));
            } catch (IOException | URISyntaxException e1) {
                try {
                    Desktop dk = Desktop.getDesktop();
                    dk.browse(new URI(NomFichero + ".jpg"));
                } catch (IOException | URISyntaxException e2) {
                    JOptionPane.showMessageDialog(null, "ERROR: " + e2.getMessage());
                }
            }
        }
    }
}
    
answered by 21.03.2017 / 16:13
source