我迁移到java 1.7以使用nio包功能。
我正在创建一个文件,并使用nio包功能将文件的所有者设置为已登录用户。
File file = new File(System.getProperty("<file location is in system property>")+fileName);
outputStream = new FileOutputStream(file);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputstream.read(bytes)) != -1) { //getting the input stream through vcl of the file upload
outputStream.write(bytes, 0, read);
}
Path path = Paths.get(System.getProperty("<file location>")+fileName);
FacesContext faceCtx = FacesContext.getCurrentInstance();
ExternalContext extCtx = faceCtx.getExternalContext();
HttpServletRequest req = (HttpServletRequest)extCtx.getRequest();
UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
FileOwnerAttributeView ownerAttributeView = Files.getFileAttributeView(path,FileOwnerAttributeView.class);
String user=req.getHeader("<parameter to get the logged in user>")
UserPrincipal owner = lookupService.lookupPrincipalByName(user);
ownerAttributeView.setOwner(owner); //throwing operation not permitted exception
现在,即使我尝试通过unix框中的chown命令更改所有者,我也会遇到相同的错误(通过没有root权限的用户weblogic运行)。
似乎只有拥有root权限的用户才能更改文件的所有者,并且不建议使用root用户权限运行weblogic域。
所以问题是我有没有办法在创建文件时使用一些nio包API来设置所有者???