Create folder works but permission not inherited - java

I have a piece of Java that create folder(s) on a network mapped drive (Z:)
Script executes on server A (Windows 2008 R2, running as user account "serviceUser") and creates folder(s) on server B (Windows 2003)
The root folder on server B (which is mapped as Z:) has special permission and allows "serviceUser" to create, modify, delete, write, etc. Permissions are set to inherit to child object, so folder created in Z: should get the same permissions as Z: itself.
My code creates 2 folders inside Z: like:
File destination = new File("z:\\folder_1\\");
File destination = new File("z:\\folder_1\\subfolder_1\\");
Folder "folder_1" gets the correct permissions but "subfolder_1" does not.
After creating those folders, I need to create a file, but as the "subfolder_1" doesn't get permissions, console reports "Access is denied" when doing File fileName = new File("z:\\folder_1\\subfolder_1\\filename.png");
How can I fix this problem?

Have you tried the mkdirs command? It will delegate to the OS to create all needed directories in your overall path.
File destination = new File("z:\\folder_1\\subfolder_1");
destination.mkdirs();

Try to use: setReadable() and/or setWritable() on your folder2. This is the only portable pure java way to control file permissions.

The only successfull way I found to make this work was to set the user account as an administrator of Server B with full control.

Related

How to create local folder for program data on Windows (in %APPDATA%, without granting permissions, JAVA)

How to create local folder for local program data in Java on Windows (in %APPDATA%, without granting windows permissions, I use JAVA)
I tried this code but file.mkdirs() doesnt create folder which indicates that I dont have permission to this folder
new File("%APPDATA%/PackMe/some_folder");
Ok found solution:
System.getProperty("user.home") + "/PackMe/somefolder";
I think the appdata folder is this:
System.getProperty("user.home") + "\\PackMe\\somefolder";
or using the environment variable (which helped me when I encountered this problem too):
System.getenv("APPDATA");
Could you provide your full code and the stacktrace of the exception?

Why getClass().getProtectionDomain().getCodeSource().getLocation().getPath() doesn't work in final product after compiled?

Ok, Here is my Web project. I built it in eClipse with the following structure:
workspace3\MyProject\war\images\uploaded
workspace3\MyProject\war\WEB-INF\classes
Ok, I want to store the uploaded images into workspace3\MyProject\war\images\unloaded, so here is the code at service side & it works fine in Eclipse
String absolutePath = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
absolutePath=absolutePath.replace("WEB-INF/classes/", "images/uploaded");
File file = File.createTempFile("upload-", "."+extName, new File(absolutePath));
Ok, now I compiled my project & put it into VPS with Tomcat server and it has the following structure
tomcat7\webapps\ROOT\images\uploaded
tomcat7\webapps\ROOT\WEB-INF\classes
However, somehow when run the website via internet, it couldn't find the images\uploaded location.
Did i do anything wrong here?
Why getClass().getProtectionDomain().getCodeSource().getLocation().getPath() doesn't work in final product after compiled?
You should rather use ServletContext#getRealPath(...) to determine the file system path of your web application:
String absolutePath = request.getServletContext().getRealPath("/images/uploaded");
// File uploaded to this directory will be accessible via
// `http://<yourserver>/<web-app>/images/uploaded/`
But be careful! The servlet specification does not guarantee, that getRealPath will return a path to a writable directory. And it may return null in case the virtual path cannot be translated to a real path!
If you want to be sure, that the destination is a writable directory, and you just want to upload files into a temporary directory for processing, consider using the web application's private temp directory:
File tempDir = (File)request.getServletContext().getAttribute(ServletContext.TEMPDIR);
// Files uploaded to that directory will NOT be automatically published to WWW.
Note that this directory is temporary only and may not survive a server restart! So it is not thought for durable persistance.
The most sensible and durable solution is to write the file into a database, or any other repository (e.g. JCR like Jackrabbit), or into a file directory that is NOT controlled by your web server (and is specified from outside, e.g. via system property or in web.xml).

Create files in network shared drives

I want to create a file in Drive which is shared over the network.If I give normal path for file creation like :-
D:\\Folder\FileName.txt
this works but If I give
hostname\\Folder\Filename.txt
This gives me Access Denied Error.
Folder is shared to ALL/Everyone
I am using windows 7.

Create folders programmatically along with permissions using java to save content to that location

I have xampp installed in my windows system and lampp installed in my linux system. I want to create folder in the location "http://localhost/" using java. I have done the following :
dirName = new File("http://localhost/"+name);
if(!dirName.exists()) {
dirName.mkdir();
}
Is it possible to do? The idea is to download some files to that location programatically. Download is working but how do I create folders so that I can access it via http://example.com/name. This is required to keep track of the user related content. I have access to the apache web server with lampp already installed. How can I create folders and save the downloads to that folder with programmatically assigning the permissions to the folder and contents within it so that saved contents can be download from there using wget method.
Don't use the File API. It is ridden with misbehavior for serious filesystem work.
For instance, if a directory creation fails, the .mkdir() method returns... A boolean! No exception is thrown.
Use Files instead.
For instance, to create a directory:
// Throws exception on failure
Files.createDirectory(Paths.get("/the/path"),
PosixFilePermissions.asFileAttribute(
PosixFilePermissions.fromString("rwxr-x---")
));
Use Java Files with PosixPermission.
[Note- PosixPermission is not supported in Windows]
Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxrwxrwx");
Files.createDirectories(path, PosixFilePermissions.asFileAttribute(perms));
In Java you can create files in any writeable directory on your system by doing something like:
File file1 = new File("/var/www/newDirectory/");
file1.mkdirs();
Then to create a file in that directory you can do something like this:
File file2 = new File(file1.getAbsolutePath() + "newFile.txt"); // You may need to add a "File.seperator()" after the "file1.getAbsolutePath()" if the trailing "/" isn't included
if (file2.exists() == false) {
file2.createNewFile();
}
To ensure that your file is readable to the public you should add read permissions to the file:
file2.setReadable(true, false);
In Apache you can set up a virtual host that points to the directory where you would like to make files available from. By default on debian linux it is /var/www.

Creating a new file in the root of an ext2 file system

I am creating an application for Android which enables users to create encrypted LUKS partitions and then mount them to given directories on external memory.
For the partition to be usable I create an ext2 file system using the Busybox mkfs.ext2 command. The problem occurs once a user tries to create a file/directory at the root of the partition. For some reason it is impossible to create a file through Java as the "File.mkdirs()" method fails. However, it is possible to create this file through the command-line. And this error occurs only when at the root of the partition (i.e if I create a folder through the command line I am then able to create files within that folder through Java). Also, I am able to create a file if I create a vfat file system instead of ext2.
Any help would be greatly appreciated.
Harry
EDIT
Fixed. I was mounting the file system as root
My final solution to this problem was to create a vfat file system rather than ext2 as vfat doesn't have permissions etc... This worked for me as I did not need the extra security of permissions. However, if you need an ext2 file system which you need to mount as root but want it to be useable by other users I recomment looking into the mount ownmask option (man mount).

Categories

Resources