How to specify path with ImageIO write in JavaFX? - javafx

I have a JavaFX program in which the user may choose a profile picture. This profile picture is applied and should then be saved to a resources folder at the same level as my bin and src folders. I have a few questions:
How do you define the file path? This is my current attempt. I get a NullPointerException, but I think that has something to do with the output stream. It's fixable, so I am mostly worried about the path. This works when accessing an image, so why doesn't it work when saving one?
// Store chosen profile picture
File outputFile = new File("/resources/profilePicture.png");
ImageIO.write(SwingFXUtils.fromFXImage(croppedProfilePicture, null), "png", outputFile);
When I go to deploy the application, will I get issues with saving to a resource folder at the same level as bin and src? Do bin and src even exist when you deploy an application?
Thanks. I can supply any more information if needed.

Related

QT Creator Disable displaying file path in file browser

I move source and header files in include and src dirrectories. Then i define this dirrectories in VPATH and include files without specifying any directory.
After all of this, I want to see files in the File Browser panel as included directly (without any path), but I see relative paths anyway!
Is there some way to disable relative paths in File Browser panel?
P. S. I doesn't like "Simplify Tree" option, coz it place all headers and sources in one folder. This is uncomfortabel.
The screenshots are all taken from "Projects" view, not from file view. The project view is meant to visualize the build system. Your build system has the files stored in a subfolder, so Creator will display that. Since the Projects view is used to add files into the project this is important: Which folder should a new file go into if that folder was not displayed?
It does not matter how you write the filenames into your .pro-file: Creator will get a list of absolute paths for all your sources -- as long as qmake/make can build your project that is:-)

Server.MapPath does not find the path on Azure

I have deployed my project to Azure. In my project I have "App_Data\Images" folder.
Now I'm trying to do the following:
String filename = GLOBAL_IMAGES_VALS.GET_FILE_PREFIX(imageType) + "-" + User.Identity.GetUserId<int>().ToString() + Path.GetExtension(image.FileName);
String origPath = Server.MapPath("~\\App_Data")+"\\Images\\" + filename;
But then upon trying:
image.SaveAs(origPath);
I get this error message:
Could not find a part of the path
'D:\home\site\wwwroot\App_Data\Images\logo-10003065.jpg'.
How can I save my file to "App_Data\Images\"?
The actual problem was that the sub-folder 'Images' did not exist. I can't remember why the publish process did not create this sub-folder, however I added it manually and then everything worked fine.
EDIT:
As others wrote here (#Spectarion). I'll put here the important remark that explain why the folder was not created:
Just for the future readers, folder won't be created if it's empty.
Folder is empty even if there are files that are not included in
project.
Just put some 'fake.txt' file into any folder you want to make sure that it will be created, and of course don't forget to add it to the project. Good luck.
Since you don't have any file in the particular folder, while publishing Web deploy ignores the empty folder.
Quick fix: Add any file to the folder before publishing will fix this issue.
if (!Directory.Exists(Server.MapPath("~/Images")))
{
Directory.CreateDirectory(Server.MapPath("~/Images"));
}
The directory might be missing in the folder. Create the directory and use it in file path
Maybe this :
System.Web.Hosting.HostingEnvironment.MapPath("~\\App_Data")+"\\Images\\" + filename )
Maybe the images folder doesn't exist and you need to create it first? Although I wouldn't recommend saving images in your app like this if it is designed for people uploading images. I would save them in Azure storage via blobs or the new Azure File storage. I would keep your app deployment files clean just related to your app and save any user generated content outside of it.
BTW, If you are using Azure Web Apps you can use the environment variable of "HOME" to always get the correct path (which should be D:\home)
string path = Environment.GetEnvironmentVariable("HOME") +
"\\site\\wwwroot\\App_Data\\images"
I assume your AppData folder is just under the wwwroot folder, which is usually the case.
Try this:
HttpContext.Current.Server.MapPath(Path.Combine("~/AppData/Images/", filename));
I just had this problem on VS15. I first followed the advice in this question in order to generate the error you've got. I'm guessing this follows in part dsb's answer, but dsb hasnt given any description of the actual process of fixing this.
I then went to https://<mywebsite>.scm.azurewebsites.net/DebugConsole to look through the directory and found that App_Data had not been published
Which was why the error was throwing. So, I then solved this by simply going to the solution explorer, right clicking App_Dataand selecting to "Publish App_Data".
However, my website was a short-term academic effort for a project - I think there is probably a lot to be said for considering Matt Watsons answer above about whether or not allowing users to upload to the deployment area is a good idea

How to access the same set of images from 2 projects in asp.net

VS 2013, SQL Server 2012, Entity frame, Web API 2
Both project access the same database
Initially my solution has only one project. This project will read and write images. So I save image path in database and save the image itself in this project's folder "~/Images/".
Later I added another project into this solution. This project also need to read and write the same set of images. When I try to read an image saved by the first project (say "~/Images/xyz.jpg") by first getting image path from database and then access it, I was told the file doesn't exist. Then I realize that I was concatenating the first project's domain name and path "~/Images/xyz.jpg". But actually "~/Images/xyz.jpg" is a path in project 1. So apparently this doesn't exist, I even don't have a "~/Images" folder in the second project.
I am going to deploy these two projects to Azure. So I am not going to configure IIS.
How can I solve this problem? Thank you.
I would suggest to use absolute path for getting images. You can introduce a config parameter for your image path (put it in your in web.config) - for example c:\myprojectname\images. Then save only an image filename into the database. When you are retrieving image filename back, concatenate it with absolute path from config and you will be able to access it.
One things. Make sure to check and create the folder if it does not exist on app start.

Creating a new file without using a ServletContext

Assume I want to write to a new file created within the space of my webapp.
One way would be use getServletContext().getRealPath("/") and use that String to create a new file on the server. However, often I come across advice like not using getServletContext().getRealPath("/").
Can someone please let me know of another way to write a new file within my webapp?
Many thanks.
Have some configuration property containing the absolute path of a directory outside of the webapp and web server path, read this path from the configuration property, and write to this directory.
To serve files from this directory, write a servlet that takes the name or ID of this file as parameter, reads the file from the directory, and sends its content to the response.
This will
work even if the app is deployed as a war file and never unzipped to the file system
allow you to redeploy the next version of the app or server without deleting all the uploaded/created files
allow you to add whatever control you want on the uploaded/created files, instead of making them available to everyone
In short, treat these files as data, stored in a database which happens to be the file system instead of a SQL database.

move dynamically uploaded files in asp

I am having a application that uploads files according to the user input..i just want to move that particular file to moved to another folder..
Now i am able to move the files to the destination folder..but it moves all the files which is already there in the folder..
i am creating a folder dynamically while uploading the files..and i need that file to be moved to that created folder...
I need to move only the uploaded file during run time...
E:\Export Documents - Copy\Uploads this path to C:\inetpub\wwwroot
this is my code
Set oFS = Server.CreateObject("Scripting.FileSystemObject")
strDir = "c:\inetpub\wwwroot\" & fldr
oFS.CopyFile "E:\Export Documents - Copy\Uploads\*.jpg", "c:\inetpub\wwwroot\" & fldr
What you probably want to do is identify the file you have just uploaded and then move that. If you are already creating a folder to store the files in in inetpub, you might as well do the same in Uploads, and then just move that folder.
You will probably find it helpful to use fileSystemObject.MoveFolder instead of .CopyFile so that your Uploads folder does not fill up.
If you want some help with how to do that, post the code you are using to upload your file.
For working with the FileSystemObject, this is a useful reference
On another note, is it really wise to move files from Uploads into wwwroot? A malicious user could do some damage in there.

Resources