How To Serve Uploaded Images From OpenShift DATA_DIR - java

My app runs on JBoss EAP and I want to upload images to the OPENSHIFT_DATA_DIR and then save their relative paths in the MySQL DB because I need to construct a path to the images and later serve them to the browser when needed.
If my OPENSHIFT_DATA_DIR path is /var/lib/openshift/5364c54ce0b8cd80180001f7/app-root/data/ and I want schoolpics/federaluniversity/uniben-1/ inside it to save a file logo.png such that the full path will be
/var/lib/openshift/5364c54ce0b8cd80180001f7/app-root/data/schoolpics/federaluniversity/uniben-1/logo.png
How to achieve this in term of storing in database, and opening using URL of image in a web page, since the OPENSHIFT_DATA_DIR seems to be outside the webroot of the app.

My solution to this issue was to create a servlet that would serve the images from the OPENSHIFT_DATA_DIR, the servlet also supplies a way to write the images to the OPENSHIFT_DATA_DIR also, you can check out this forum post for more information: https://www.openshift.com/forums/openshift/how-to-upload-and-serve-files-using-java-servlets-on-openshift

Related

How to use or load flash file exists on different server

I want to use flash file stored on another server or repository. I am using below code in xsl to add flash file.
https://www.***.com/docs/swf/Spreadsheet.swf
The problem is I am unabele to create swfObject because that flash file is not getting loaded properly on browser. My xslt application is on another server which is trying to access .swf file using above code. I guess there might be domain related issue. I read somewhere about cross domain.xml file.
Is it really required in above scenario? If yes then where to keep that cross domain.xml file? The flash file that I want to access is on another repository which is not on any web server. So can anyone provide me solutions on this?

Upload image in spring mvc permanently in other location

i want to upload image in spring mvc in file system. i can do it but when redeploy the project all image remove from directory. now my question is how upload image permanently?i want this in real application.
You probably uploaded your images under your project directories/war. Every time you deploy/build your project/war, the images got deleted.
You need to save the image files outside of your project/war.
For real application, I suggest you at least save/serve your uploaded images in a separate server. Amazon S3 is a pretty good one. You can just store the object/file name relative to S3 base url in your database. They have java APIs for you to upload the files too.
As mentioned, in order to persist these uploaded images it is best to save them outside of your project/.WAR file. The reason for this is as you've already experienced, each time you redeploy your application you will loose anything (i.e. your uploaded images) that had been written to the previous project/.WAR when it was deployed.
The provided solution of utilizing an Amazon S3 bucket to save these images is a good solution and you could definitely accomplish what you desire (having a URL of www.example.com/upload/ showing all these images). With springMVC within your controller you can set up a method and assign the #RequestMapping annotation like so:
#Controller
#RequestMapping("/") //this can map to your www.example.com
public class MainController(){
#RequestMapping("/upload") //this will then map to www.example.com/upload
public String showUploads(){
return "redirect:http://pathToAmazonS3Bucket"
}
}
In SpringMVC the redirect allows you to redirect to an absolute URL path. See docs
But as already mentioned you still have to host your images somewhere outside of your project path/WAR file. Amazon S3 works, but since you asked for another solution here is one.
You can save all the images to a file on your PC, then run Python3 or Node as HTTP servers. This solution though requires you to utilize your computer and your internet connection to host your images on the web. This assumes you are good with leaving your PC running non-stop or have an old one laying around that you will use as your webserver. It also assumes your ISP is okay with you hosting a webserver on your network. Lastly you can obtain your own unique URL from numerous services online (some free and some for small fees), this way people dont have to type in your IP address.
I am running a similar setup above on my network with a free domain name from No-IP.com.
Also, how do you plan to host your spring web application on the web? Will you be doing this via an online hosting service or hosting yourself? If hosting yourself, will you use Apache Tomcat or Glassfish or another container/application server?

How to get the bulks of images dynamically from database to server to jsp without using path

The problem with files is if i have bulk of users i should download images for every user in a certain folder and pass them to jsp.which is unfriendly.Is there any way.
Well, you can create a separate controller which serves images directly from the database, and then use the path of this controller in all of your JSPs. This way you will not need to "preload" the images into some local directory.
In case this happens to be slow, configure some kind of caching layer in front of the application in nginx or apache httpd.

How to implement folder upload in a Spring web app?

I'd like to implement in my web application a file/directory upload similar to Google Drive style (I think it's the best example to explain what I want).
So I would like to upload:
a single file
multiple selected files
a selected folder (all files contained in it)
On client side I suppose I have to use HTML5, am I wrong? But How to handle this on server side controller. I'm using Spring MVC 3.2.9
Can you suggest me the best approach?
The hard part is the client side upload of folders. According to this other answer on SO about Does HTML5 allow drag-drop upload of folders or a folder tree?, The HTML5 spec does NOT say that when selecting a folder for upload, the browser should upload all contained files recursively.
Of course it is possible, but HTML5 is not enough and you will have to use Javascript to (recursively) find all files in the folder.
As said by conFusl, you can find a nice example on viralpatel.net Spring MVC Multiple File Upload tutorial. Spring Multiple File upload example. The princips are :
on client side generate (via javascript) a form with one <input> tag per file to upload, and give them names like files[i]
on server side, you then get a form containing a List<MultipartFile> that you can process as usual.

Where should I keep files uploaded by users on my server?

On my website; users can upload their pictures. I am using tomcat with apache , hibernate, jpa.
I would prefer to keep these images at some location like /var/ImagesUploaded on my ubuntu box. Using Java I can refer these files in directory /var/ImagesUploaded using java.io but how will I show these images on HTML pages to user? On HTML files we need tags like <img src=''> and this src is relative to the webapp. So, is it that I have to keep the user uploaded images inside my webapp ONLY! Or is there a better solution?
How about reading the images on your application, and then serve them to your user as a base64 encoded stream? That way, you don't have to expose your image directory to the web, and can effectively prevent it from being crawled by bots.
Read the file as an InputStream inside your application using java.io
Convert it to Base64 using Apache Commons Codec. encodeBase64URLSafeString(IOUtils.toByteArray(yourImageAsInputStream);
In your response HTML, embed the image as <img src="data:image/jpeg;base64,encodedString">
This post on SO might be useful as well.
Either configure your webserver to server files from /var/imagesUploaded (for example, as /imguploads), or use a script that reads the image and outputs it to the user, with the correct headers.
You have several options:
If you are on Tomcat 7, you can use the "alias" feature in your <Context> element (http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Standard_Implementation)
You can attempt to map the DefaultServlet on some special path like "/uploads/*", but be aware that some versions of Tomcat have a DefaultServlet that basically does not work properly when it is not mapped to "/"
You can write your own servlet to serve the bits yourself, though you will end up duplicating a lot of the capabilities of the DefaultServlet and may fall short in the robustness category (e.g. implementing Range queries, etc.)
Note that if you write your own servlet, you have the option of performing user-based checks for authorized access to certain resources. Maybe you don't want your whole uploads directory to be accessible by, say, Google.

Categories