How and where to place uploaded images on glassfish 4 - java

I'll try to be short with the description of my situation:
I'm making a restaurant recommendation web site. I want users to be able to add a new restaurant and upload 1 picture of the restaurant (restaurant's profile picture). That picture will later be displayed when users search for the restaurants. Each time a new restaurant is added I want to create new folder for that restaurant and place the uploaded picture there.
I can upload an image and place on my file system, but when I try to display it it doesn't show.
<img src="\C:\glassfish4\glassfish\domains\domain1\applications\__internal\WebAppName\profilePicture.jpg" />
This is where I was originally placing the images, but they wouldn't show.
If I understood correctly from here that is not a good practice to reference images in this manner because then the browser will be looking for that location in user's machine.
I tried to place the images in:
C:\glassfish4\glassfish\domains\domain1\eclipseApps\WebAppName
because I figured that this is where WebContent folder is (please, correct me if I am wrong), and that this location is accessible with:
<img src="http://localhost:8080/WebAppName/..."/>
but that worked only for a short time. As soon as I redeployed my app the folders in which the pictures were placed had gone away (and they were created).
So my question(s) are:
How and where to place these images, and what should my src attribute look like in an html document (should it be like C:\... or http://localhost/...)?
What are conventions, practices for this, and how is this generally done?
And does redeployment has anything to do with my pictures being gone?
I found this post, but it did not solve my problem.
Note: - I am using glassfish4, and Java Servlets, JSP, JSTL/EL, and generally Java.
Thanks in advance!

And does redeployment has anything to do with my pictures being gone?
It does. When you redeployed your application, GAS removed the application directory at ${GAS_INST_DIR}\domains\${YOUR_DOMAIN_NAME}\application{YOUR_APP_BUNDLE_NAME}. Once you decided to store you images there, they are gone after the redeployment.
How and where to place these images
The most straightforward way is to put your files somewhere outside application server folder could be a solution but I would say just half a solution. Let's assume you store your pictures in a local folder /var/application/data. Later you decided to cluster your application. Now you are again in trouble. Each instance has its own /var/application/data directory and as a rule you do not know what node will handle a request for storing an image.
What are conventions, practices for this, and how is this generally done?
I would say it is you who decides what way to go according to the needs of your application. I will list the ways that are the most obvious. All have their own strength and weaknesses.
You can put the images in a local folder. The strong side is simplicity. Once you decide to cluster, you would have to remake this approach. If you go this way the most general approach would be to create a servlet that loads your images and in this case your src= will point to the servlet. Did not find a good example right away, but I think this example will give you an idea how to do it. The only thing I would suggest using finally block or if you use jdk 1.7 the try-with-resources for closing stream. Another thing you will need to pass file name as a parameter to the servlet.
Store images in database. It could be RDBMS or noSql. Down side is that not all RDBMSs work efficiently with binary data. Again src could point to a servlet that loads images from the DB. Here you should design your DB accordingly so you can retrieve images effectively. I would not choose this approach, but this is just a personal opinion. Cannot say how efficient the noSql databases are for storing binary data. You should do it yourself
Consider Webdav. In this case your src attribute will be a link to a resource in webdav server. You can use it in clustered environment, relatively simple implementation.
and what should my src attribute
look like in an html document (should it be like C:... or
http://localhost/...)?
Depends on the approach you choose. See item 1-3.
Hope that helps.

Related

Java, Codename One: Exporting and -most importantly- importing files between users

Here the problem: my app will generate some files, and I want to give to the users the opportunity to exchange these files between them.
This requires 3 steps:
Saving the data: easily done in Storage implementing the
functions required by the Externalizable class;
Sharing the data: done (probably, right now it's impossible to check if the
result is correct because the missing step 3) with the sharing
methods offered by the framework, as soon as I understood I needed
to use as mimetype "application/octect-stream";
importing the downloaded data (shared by another user): this one I can't manage to
find a way to make it work. Loading the files from the app's Storage
is easy, but accessing to the folders out of the app's Storage is
something I can't manage to do.
I used FileSystemStorage in the hope of gaining access at least to the Download folder that (mostly) every phone has, but apparently I can't manage to accomplish the task
Using the FileSystemStorage on Android, for example, I have access to
/storage/emulated/0
/storage/emulated/legacy
file:///system
The first two being related to the Storage of the app.
Acceding to file:/// I obtain a long list of folders, a partial list including
media
logs
sdcard
Removable
...
root
...
But when I try to access some of these, they all appear to be empty. Either I make some mistake or the app can't see their content.
So I wonder if there is a way to accomplish the task, namely to have access to the files (probably in the Download folder) the user has downloaded, to import them.
Phone apps live in isolation within the phone. Android literally creates a separate Linux user for every app so they don't step on each other and damage the phone. iOS does similar tricks.
As a result apps can't just write a file to downloads and can't just list all the files there. This would violate the users privacy. So the native API to share a file is usually separate from the files API. We do have a share API in the CN class which lets you share images etc. to an arbitrary OS app. See isNativeShareSupported and share.
Ok, maybe I found a solution for reading the files from the Download folder in an extension of CodenameOne called FileChooser.
According to this blog post it should give access to, between the others, the Download folder (at least in Android).
I'm going to try it and, when everything is ready and tested, edit this reply to say how it worked out for me.

What should be my path when I want users to upload file on my website?

I have hosted my JavaEE website ImgEcho in which I have given functionality through which users can upload an image. When I implemented this website on localhost I directly gave the path where the uploaded image would be stored as "C:/ProjectName/images"
However, I cant figure out what should be my path on the hosting server.
P.S: I should ask this question to the customer support of my hosting server but they have trashy service.
Thanks in advance.
Depending on what you want, there are many different things that you can do in this situation.
If you want to have a single, shared image folder, in which you store the images of all users, than I recommend that you create an images directory within your projects root.
Otherwise, if you want to create a image storage that is per-user, than you can do the following:
-Project Root
|-User 1
|-images
|-account
|-User 2
|-images
|-account
This is a structure that contains every user in a separate folder, with the subfolders images, which contains the images for that user, and account, the
storage for that user's account details and other information. This is generally more structured of a setup, and will benefit you long-term, although more advanced.
For a more scalable approach, perhaps in the future if you are willing to scale up your service, I recommend getting a large external network hard drive, or NAS (Network-attached storage), and then using that for all of the image storage. This will be very advanced, and possibly pricey, which is why it would be a long-term decision, that could quite possibly benefit your company in the future, because of the scalability.
Overall, I would generally use an images directory, regardless of the rest of the folder structure, as it is the most logically correct decision for your current situation.
EDIT: To make this setup more secure, I recommend putting the images directory into a custom, unrecognised archive file, and then converting the data when you need to use it, and back again when a change is made. This ensures no sneaky users opening your images because it is stored in a plain folder layout.
Create a folder in your root named "images" and save them in there. You have to adjust the rest of the references.
You can store the images inside a path in your project itself by giving a relative path.
You should use a relative path to store it in the site. You would want something like /images/uploads for your project. The path should be at the root of your web documents. The reason to do so is that if you move the project, things would likely break. Using a relative path ensures that you can move the project. In addition, if this is shared hosting, you won't have to worry about your files getting mixed with others.
What I do is run this code in a jsp :
java.io.File f = new java.io.File ("./");
try{
out.println(" f curr " + f.getCanonicalPath());
}catch(Exception e){
out.println(" Err file :" + e + ", " + f);
}
This tells me the path to the current folder of my web app, on some clouds have seen its /xyz/tomcat/temp on others a longer path.
the path will be along the lines of /usr/public_html/images. Who is your hosting through?
Create two directory, One is for your site and another one is your storage puropose.
Advantages:
Each time an user send a server request to access his data, it will go to search a file or folder in the directory.
through chmod we can give different access permission two them.
if the storage folder is mess up with site, it would be a risque of long page load.
Since you have a JavaEE website, why not just make a controller method responsible for downloading the files and have the return value of the upload function be a path to the download method? That way you can store the image wherever your website has write/read access to. I can give more implementation details on request.

Static images in webpage from Google App Engine

I'm porting an old Java project to GAE. It has some servlets, which generate html pages with static images in them. In the original project these images are stored on the filesystem next to the servlets.
I'm trying to use GCS in the first place, I've uploaded my files and gave permissions on public read. In this case I can reach the files with their public link, I can embed these links into the HTML output. But I have a feeling that this isn't the right solution. The load time seems quite slow, like the images don't "travel internally", and I have to provide permission for every single image.
So my question is, how to get an "internal" URL for a file located on GCS in your GAE application?
I've found some Java examples, but in my case I don't think I need the image object in the source, I just need an URL to pass it on to the HTML source.
As far as I know I could just simple deploy the images with the source as resources, but there are quite many of them.
If there are other soultions, like Datastore, I'm open for that too, but I thought GCS would be the easiest.
Google Cloud Storage is as fast an option for loading images as any other. A browser reads a link and asks the server (in this case GCS) to deliver an image. There is no "internal" URL that can work faster - the speed reflects the bandwidth/distance between GCS and the browser which asked for an image.
You can speed it up by using a CDN, where your image is stored on local servers throughout the world. It only makes sense if you serve content to a very large number of users, and it is a critical part of how fast a page loads.
Another way to speed up page load time is to use image sprites instead of images. This way you cut the number of requests from a browser to a server (i.e. GCS). If you images do not change frequently, and most pages need the same "collection" of images (i.e. not shown dynamically), this is a very good solution.

Saves game level settings

I have been coding for about a month and I have found ways to adapt around ever problem but one. The problem as you can probably see by the title is how to make a way to make game saves. I am currently creating a very simple game that has about 5 classes of my code and maybe 2 of Java Swing GUI.
I know how I would like to go about the saving process but I have no idea how to do it in my code. How I would like to go about doing this is by making the code print a Number or Integer to a file to represent a Level. For example if you completed level 1 the number in the file would be 1. I have tried some templates for this but none of them work.
I understand how to write to a file but my problem is reading it from a jar or even creating a file then reading it from a place on the computer. I need to know how to find a file URL for different computers because some use Docs and Settings and other Users. Please could someone help.
Since the jar is read only, it can only contain the 'default settings'. See this answer for the general strategy to deal with such a embedded-resource.
Speaking of which (embedded resources) see the info. page for more details on how to access them.
Here is an example of storing and reading a Properties file from the 'current directory'.
As mentioned by #MadProgrammer though, it is safest to put the settings file into a (sub-directory) of user.home, as seen in this answer.
But a properties file is just one option. You might also serialize an object, or write the file in a custom format that your app. knows how to read, for the first two off the top of my head.
Besides 'serialize (in some form) in a File', there is also the Preferences API, or for desktop applications launched using Java Web Start, the PersistenceService. Here is a demo. of the service.
I need to know how to find a file url for different computers because
some use Docs and Settings and other Users
The System property user.home points to the user's home directory
File userHome = new File(System.getProperty("user.home"));

Problems with deployment, advice needed for a web-based java application

I have developed a command-line (read: no GUI) Java application which crunches through numbers based on a given dataset and a series of parameters; and spits out a series of HTML files as resultant reports. These reports hold a large amount of data in tables, so in order to give the users a easy and quick overview of the results, I utilized the JUNG2 library and created a nice graph.
Here's where it gets interesting; since I would like the graph to be interactive it should be deployed after the application has run and files are generated, whenever the user wants to view the reports. I decided to go with an applet based deployment, however I am not too happy with the current setup due to the following reasons:
I want to make the software as simple to use as possible (my users won't be tech-savvy, and even tech-intimidated in most cases). I would really like to distribute one JAR only, which forced me to put the applet with everything else it needs in a package in the same JAR as the main application.
The applet and the main application need to communicate the results, so I create a xML-based report which is used to hold information. As long as the files are on a local machine and are not moved around it all works fine. Unfortunately I also need the files to be moved around. A user should be able to take the "results" folder to a USB stick, go anywhere plug the stick to another computer and be able to use the report as he/she likes.
For the time being the applets are implemented with the following html code:
<applet code="package.myapp.visualization.GraphApplet.class"
codebase="file:/home/user/myApp"
archive="myApp-0.2.6-r28.jar"
width="750" height="750">
<param name=input value="results/test_name/results.fxml">
</applet>
As you can see this applet will not work if the parent folder is moved to another location.
As far as I know I have a couple of alternatives:
a) Change codebase to point to an URL on our webserver where I could put the jar file. This however creates the problem with permissions, as the applet will not be able to read the results file. Alternative is to upload the results file to the server when the user wants to visualize the graph, although I am not sure if that's a good option due to server security and also if it could be made so that upload happens automatically without bothering the user.
b) I can use a relative path on the codebase attribute, but then the whole folder hierarchy needs to be intact upon copy. This could be a last resort, if I cant come up with a better way to do it.
c) change the deployment method (would like to avoid this alternative to not spend more time on the development phase)
Any ideas? Am I missing something? How could I tackle this problem?
Thanks,
I'm not sure I entirely understand your use-case, but from what I do understand, I would suggest this:
Dump the applet for an application launched using Java Web Start. Have the JNLP file declare a file association for the fxml file type. When the user double clicks an fxml file, it will be passed as an argument to the main(String[]) of the JWS application.
A sand-boxed JWS application can gain access to resources on the local file system using the JNLP API. Here is my demo. of the JNLP API file services.

Categories

Resources