我需要从Windows上运行的Java脚本中读取一堆二进制文件.
但是,文件所在的文件夹具有有限的权限.我(即我的Windows用户名)有权读取它们,但Java运行的用户(这是Web应用程序的一部分)却没有.如果我在运行时将自己的用户名和Windows网络密码传递给Java,有没有办法可以使用自己的权限而不是Web用户来读取这些文件?
(请注意,这不是通过Web发生的;这是在Web应用程序的上下文中运行的一次性导入脚本.)
最佳答案
您可以创建网络共享,然后通过jCIFS连接
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFileInputStream;
public class Example
{
public static void main(String[] args)
{
SmbFileInputStream fis = null;
try
{
fis = new SmbFileInputStream("smb://DOMAIN;USERNAME:PASSWORD@SERVER/SHARE/filename.txt");
// handle as you would a normal input stream... this example prints the contents of the file
int length;
byte[] buffer = new byte[1024];
while ((length = fis.read(buffer)) != -1)
{
for (int x = 0; x < length; x++)
{
System.out.print((char) buffer[x]);
}
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (SmbException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (fis != null)
{
try
{
fis.close();
}
catch (Exception ignore)
{
}
}
}
}
}
相关文章
总结
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250