[英]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,并且不想在上面进行大量投资,则可以将程序分成几部分:
copy
utility if nothing more has to be done or can easily written in Java copy
实用程序 sc
and srvany
according to this answer on ServerFault sc
和srvany
轻松地将服务器程序作为服务安装在服务器计算机上 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.