简体   繁体   English

使用身份验证将文件复制到Java中的本地网络中的计算机上

[英]copy files to a machine in local network in java with authentification

I've used Commons IO to write a program that copy files and other things. 我用Commons IO编写了一个复制文件和其他东西的程序。 But I want to copy a file to a local ip address \\\\10.xxx , but the user doesn't have rights to do the copy, so I need to put an ID and password to access it. 但是我想将文件复制到本地IP地址\\\\10.xxx ,但是用户无权进行复制,因此我需要输入ID和密码才能访问它。 However I cannot find a way I can do that. 但是,我找不到能够做到这一点的方法。

To move file I use : 要移动文件,我使用:

FileUtils.moveFileToDirectory(fichier, destDir,true);

But my directory is something like \\\\10.xxx\\files and only a few users can write in that directory so I have an ID & password that let you move files there. 但是我的目录类似于\\\\10.xxx\\files ,只有少数用户可以在该目录中写入数据,因此我有一个ID和密码,可以将文件移动到该目录中。 I want that even if the users don't have rights to move files to that directory my program can do it. 我希望即使用户没有权限将文件移动到该目录,我的程序也可以做到。

It is not really the way Windows security works. 并不是 Windows安全性真正起作用的方式。 If you really want to do it that way, you will have to use Java Native Interface or Java Native Access, and manage to call the WNetAddConnection function from Mpr.dll (and do not forget to call WNetCancelConnection when done). 如果您真的想这样做,则必须使用Java本机接口或Java本机访问,并设法从Mpr.dll调用WNetAddConnection函数(并且不要忘记在完成后调用WNetCancelConnection )。

But you would have to store a password in your program, which is poor security practice. 但是您将必须在程序中存储一个密码,这是不安全的做法。

The standard way to do that would be to start a service that would run under a user that has access to the desired directory, and have your program to communicate with it using whatever you want, the simplest way being probably TCP/IP. 执行此操作的标准方法是启动一个服务,该服务将在有权访问所需目录的用户下运行,并让您的程序使用您想要的任何内容与之进行通信,最简单的方法可能是TCP / IP。 But unless you have special requirement for that I would not recommend to use Jave for those kinds of program. 但是除非您有特殊要求,否则我不建议您将Jave用于此类程序。

A more Java alternative would be to start a Tomcat service on server machine running under a user having access to the directory. Java的另一种选择是在具有访问目录权限的用户下运行的服务器计算机上启动Tomcat服务。 That way you just have to develop a standard Java Web Application able to upload files that would save the files to the proper directory. 这样,您只需要开发一个标准的Java Web应用程序即可上传文件,该文件会将文件保存到正确的目录中。 But it would be a traditionnal and portable Java application with no need for JNI nor JNA. 但这将是不需要JNI或JNA的传统的可移植Java应用程序。

If cannot use a Tomcat and do not want to invest to much on it, you could split the program in pieces : 如果无法使用Tomcat,并且不想在上面进行大量投资,则可以将程序分成几部分:

  • one client program that copies files on a directory (on server machine) with File creation rights for everybody - can decays to the copy utility if nothing more has to be done or can easily written in Java 一个具有在每个服务器上对每个人都具有文件创建权限的目录文件(在服务器计算机上)进行复制的客户端程序,如果无需执行其他操作,或者可以轻松地用Java编写,则该程序可以降级到copy实用程序
  • one server program that will run on server machine under a user that has full write permissions on target directory. 一个服务器程序,它将在具有对目标目录的完全写权限的用户的情况下在服务器计算机上运行。 This one can also be easily written in Java 这个也可以用Java轻松编写
  • you can easily install the server program as a service on the server machine with sc and srvany according to this answer on ServerFault 您可以根据ServerFault上的此答案使用scsrvany轻松地将服务器程序作为服务安装在服务器计算机上

If you use a client program, you could easily add a digital signature file with each copied file, but as I said above, it is poor security practice and add little if any security. 如果使用客户端程序,则可以轻松地在每个复制的文件中添加一个数字签名文件,但是正如我上面所说,这是不安全的做法,并且几乎没有任何安全性。 At least the program should be executable and not readable, and the sources should be kept hidden. 至少程序应该是可执行的并且不可读,并且源应该保持隐藏。 It is better to log the users that copied the file and ask them what happened is you find a problem. 最好记录复制文件的用户,并询问他们发现问题后发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Java将文件从本地窗口机器复制到远程Windows机器 - Copy files from local window machine to remote windows machine using java 在Java中将保存文件存储在本地计算机上的位置 - Location to store save files on local machine in java 如何通过网络将文件复制到Java中的LocalDrive - How to copy files over Network to LocalDrive in Java 如何获取java中local.network上机器的MAC地址 - How to get the MAC address of machine on local network in java 在Windows机器上使用Java Code将本地文件复制到Unix Server? - Copy local file to Unix Server using java Code on windows machine? Java:如何使用凭据从网络复制到本地文件夹 - Java: How to copy from network using credentials to local folder 使用 RCP 或 FTP 将文件从远程 unix 机器复制到本地 windows 机器上 - Using RCP or FTP to copy files from a remote unix machine onto a local windows machine 批处理文件可以从本地计算机执行到本地计算机的(本地)Java代码吗? - Can batch files execute (local) Java code from a local machine to a remote machine 使用Java代码将目录的多媒体文件下载到我的本地计算机中 - Downloading multimedia files of a directory into my local machine using java code Weblogic Server:使用Java将文件复制到网络内的共享位置 - Weblogic Server: Copy files to shared location within network using Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM