How to implement folder upload in a Spring web app? - java

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.

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?

Does anyone know how to upload multiple files using Blobstore in Java?

I am submitting a form which has a upload filed and other fields , I writing other fields to database using j-query ajax but having issue with the upload..looks like I will have to separate the the file upload from rest of the form.. Is there a way to use Blob store upload without redirection ?
This is the functionality i want to achieve with Blobstore and Java App Engine
Allows the user to select multiple files.
Ajax send the files to the GAE server.
Save the files in GAE server.
Return the links to the file.
Add the uploaded files to the current page.
It Would be nice if any Google Developer post a Java reference code

Java Application on Amazon Web Services with Mobile and Web Clients

Our new start-up company is trying to build a mobile app with an accompanied website. We are trying to setup our application on Amazon Web Services.
We have Java code running in an EC2 instance, which will store data in S3. We want clients (iOS and Web for now) to communicate with the Java Backend via a REST API. Ideally the website would be hosted under the same AWS account.
The Java Code and REST API are already set up in a very basic form, but the setup of the Website is unclear, since this is new to us all. We also would like to evaluate beforehand if such a setup is even feasible.
Since I am in charge of the Website i have already spend hours researching this specific setup, but i simply lack experience in cloud/backend development to come to a conclusion.
Here are some questions we would like to answer:
Where would the HTML files and accompanied JavaScript etc. files be stored?
How can data (images etc.) that is stored within S3 by the JAVA code be accessed from the Website directly?
How could something like bootstrapping of data within HTML files be achieved (in JSON format preferably)?
How could the server be set up to compress certain files like CSS or JavaScript?
Please point me into the right direction, any comment is appreciated.
Where would the HTML files and accompanied JavaScript etc. files be
stored?
Either on the same AWS EC2 box or a different one, just give it a static IP and link that IP to the domain you want, done. Just remember to have port 80 open as a firewall rule.
How can data (images etc.) that is stored within S3 by the JAVA code
be accessed from the Website directly?
The files will have some url that you can link to directly in your html so it's essentially just a url.
How could something like bootstrapping of data within HTML files be
achieved (in JSON format preferably)?
You have a number of choices here. You could potentially create some JSP files to generate the HTML and load the JSP files on access and cache them so they load up super fast. You could argue however, this is overkill and in some ways, the REST endpoint should be robust enough to handle the requests.
Part of me thinks you should endeavor to use the REST API for this information so you just have to manage one endpoint, why make an extra endpoint or over engineered solution for the HTML that you then have to maintain? Build once and reuse.
How could the server be set up to compress certain files like CSS or
JavaScript?
During the build process, run the files through a minify process. This is built into maven so you could do it automatically or by hand using something like jscompress. This Minify plugin shows how to automatically minify your resources. Consider you'll need to be using Maven though as your build tool.

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.

Java : Display list of directories and files on the server through a web page

I want to implement something as follows:
The user uploads different types of files on the server.
My requirement is to display the list of all the files and directories on the server using a web page.
Is it possible to display the list of files / directories on the server using a web page.
If yes, then would it be a client side script or server side script.
Any sample code link, if possible, would be great help.
Thanks.
web page can display everything
if you wish to create list of file on the server you need server side code
You should write recursive method that traverses over tree of directories and files using java.io.File API.
Examples:
http://www.javapractices.com/topic/TopicAction.do?Id=68
Recursively list files in Java
etc, etc
A server-side script. Or even just use Apache's directory index.

Categories