mkdir() doesn't work in Dynamic Web Project - servlets

I'm trying to upload files using a servlet, which creates a new directory and insert the file into that. Now I'm trying to create the directory, but the problem is that the path doesn't match. Can you help me?
String path="imageProdotto"+File.separator+"test";
String finalPath =getServletContext().getRealPath("")+ path;
File uploadDir = new File(finalPath);
if(uploadDir.mkdir())System.out.println("ok");
else System.out.println("no");
So, when Servlet is called, mkdir() return false.

Related

ASP.NET6 Blazor: Can a directory be mapped as a virtual subdirectory under wwwroot?

Can I map an arbitrary directory as a subdirectory of wwwroot? That is, the directory is not under wwwroot in the file system, but within my app, it is treated like so.
For example, I have created an ASP.NET 6.0 Blzor Server project. The programme dll path is in /app/proj1/BlazorApp1.dll. If there is an image at /app/proj1/wwwroot/images/dog.jpg, it can be rendered using <img src="images/dog.jpg"/> in my page. But what if I do not want to have this images directory actually under the wwwroot directory on the file system? Can the directory be somewhere else like /data/images, and I map that path to wwwroot/images, so that /data/images/dog.jpg can be rendered with <img src="images/dog.jpg"/> within my app?
There are 3 ways to do this that i know of, Firstly adjusting the settings, which is clearly documented on msdn, under the 'Serve files outside of web root' header;
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(builder.Environment.ContentRootPath, "MyStaticFiles")),
RequestPath = "/StaticFiles"
});
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-6.0#serve-files-outside-of-web-root
For your case, since you want both wwwroot and another file, see 'Serve files from multiple locations';
var webRootProvider = new PhysicalFileProvider(builder.Environment.WebRootPath);
var newPathProvider = new PhysicalFileProvider(
Path.Combine(builder.Environment.ContentRootPath, "MyStaticFiles"));
var compositeProvider = new CompositeFileProvider(webRootProvider,
newPathProvider);
// Update the default provider.
app.Environment.WebRootFileProvider = compositeProvider;
app.UseStaticFiles();
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-6.0#serve-files-from-multiple-locations
Another way would be to use MSBuild to copy the files from another directory into the wwwroot folder, see for example the following SO;
Copy files to output directory using csproj dotnetcore
Lastly, and this is not as portable, you could use symlinks, see:
A symbolic link is a file-system object that points to another file system object. The object being pointed to is called the target.
Symbolic links are transparent to users; the links appear as normal files or directories, and can be acted upon by the user or application in exactly the same manner.
https://learn.microsoft.com/en-us/windows/win32/fileio/symbolic-links
https://en.wikipedia.org/wiki/Symbolic_link
https://superuser.com/questions/1020821/how-can-i-create-a-symbolic-link-on-windows-10
The first one should satisfy your needs.
You could try as below to update the default provider with a composite fileprovider :
var webRootProvider = new PhysicalFileProvider(builder.Environment.WebRootPath);
var newPathProvider = new PhysicalFileProvider(
Path.Combine(builder.Environment.ContentRootPath, "data"));
var compositeProvider = new CompositeFileProvider(webRootProvider,
newPathProvider);
app.Environment.WebRootFileProvider = compositeProvider;
app.UseStaticFiles();
Result:

JavaFX jar doesnt read .properties file

I have developed a javafx application that reads the database configuration properties from a ".properties file".When i run the app in eclipse everything works fine.The problem is when running the app from the generated executable jar,it throws me a NullPointerException because it cant read the ".properties file".
The code is :
FileInputStream fis=new FileInputStream("resources/META-INF/db/db.properties");
Properties properties = new Properties();
properties.load(fis);
I searched about this and i saw some examples of using InputStream :
Properties pp = new Properties();
InputStream is = getClass().getClassLoader().getResourceAsStream("errors.properties");
pp.load(is);
But still doesnt work.Any idea?strong text
Try creating a folder that you always have with you Jar. The folder name should be resources. This folder should have a folder named META-INF. META-INF should have a folder named db. Finally, db should have the db.properties file in it.

Read values from non-root web.config file

My site is structured in this way:
Root Directory
Arcade
default.aspx
web.config
default.aspx
web.config
I have a method Method1() which accesses:
System.Configuration.ConfigurationManager.AppSettings["Total_Unique_Plays_Required_For_High_Score_Board"]
This value exists in the root/Arcade/Web.config file but not in the root/web.config file.
When I execute Method1() from a page in the /arcade directory it works fine. However, when I execute this method as a timed event from global.asax it searches for the value in the root web.config file and throws a System.NullException.
Does anyone know how I can specify to search for the value in the root/arcade/web.config file and not the root/web.config file?
You can open the web.config file first.
So calling this will load your child file; notice you give the path to the folder containing the web.config, not the actual config file.
var config = WebConfigurationManager.OpenWebConfiguration("~/Arcade");
You can now get your values, like:
string MyValue = config.AppSettings.Settings["MySetting"].Value;
You can also get the list of app settings by calling:
KeyValueConfigurationCollection appSettings = config.AppSettings.Settings;

How to get the uploaded file path?

I am using input tag type="file" to browse the file in asp.net.
I browsed the file "Linq2sql.zip" from the location "c\Desktop\Karthik\Linq2sql.zip".
i can get the file name and path using
HttpPostedFileBase file;
var filePath = Path.GetFullPath(file.FileName);
But File path is like = C:\\Program Files (x86)\\Common Files\\Microsoft Shared\\DevServer\\10.0\\Linq2sql.zip
i have to get the original file path c\\Desktop\\Karthik\\Linq2sql.zip. How can i get?
You can not get the original path of the file on the client system; that information is not sent by the client.
The reason you get what you do with GetFullPath is because that forces a resolution with the simple file name alone with the current directory of the asp.net process. That info is utterly meaningless - and in fact incorrect - in this case.

How to find the working folder of a servlet based application in order to load resources

I write a Java servlet that I want to install on many instances of Tomcat on different servers. The servlet uses some static files that are packed with the war file under WEB-INF. This is the directory structure in a typical installation:
- tomcat
-- webapps
--- myapp
---- index.html
---- WEB-INF
----- web.xml
----- classes
------ src
------- .....
----- MY_STATIC_FOLDER
------ file1
------ file2
------ file3
How can I know the absolute path of MY_STATIC_FOLDER, so that I can read the static files?
I cannot rely on the "current folder" (what I get in a new File(".")) because it depends on where the Tomcat server was started from, which is different in every installation!
You could use ServletContext#getRealPath() to convert a relative web content path to an absolute disk file system path.
String relativeWebPath = "/WEB-INF/static/file1.ext";
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
File file = new File(absoluteDiskPath);
InputStream input = new FileInputStream(file);
// ...
However, if your sole intent is to get an InputStream out of it, better use ServletContext#getResourceAsStream() instead because getRealPath() may return null whenever the WAR is not expanded into local disk file system but instead into memory and/or a virtual disk:
String relativeWebPath = "/WEB-INF/static/file1.ext";
InputStream input = getServletContext().getResourceAsStream(relativeWebPath);
// ...
This is much more robust than the java.io.File approach. Moreover, using getRealPath() is considered bad practice.
See also:
getResourceAsStream() vs FileInputStream
What does servletcontext.getRealPath("/") mean and when should I use it
Where to place and how to read configuration resource files in servlet based application?
Recommended way to save uploaded files in a servlet application
You can try this
String relativeWebPath = "WEB-INF/static/file1.ext";
InputStream input = getServletContext().getResourceAsStream(relativeWebPath);
I would prefer to use the getResourceaAsStream option to load the resource based on the parent directory.
getClass().getClassLoader().getResourceAsStream("/MY_STATIC_FOLDER/file1");

Resources