How to insert a file into the project using Java - java

I am trying to figure out if it is possible to insert a file into the project using code.
I have a jsp. page which has a filechooser for the user to choose a file from, and the file is then saved into a database table as a byte[]. I also save the name of the file (in form thisIsAFile.png) into the same table.
What I want to know is if it is possible to insert the file into the project resources?
I have created a file into the project where I would like to save the file into using code.
I am aware that right clicking the project and doing an import is a way to add a file into the designated file manually, but I do not want that, instead I need a way for the file (in this case images only) to be saved into the project automatically, every time a user chooses a file to be saved into the database from the .jsp page. The path I am trying to insert the files into is gotten from a .properties file as /images/banners/{0} Then I only need a way to get the name (which contains the actual name + . + mime type of the file) from the database into the {0} and save it in the path as the byte[] from the corresponding row of the database.
Any help is much appreciated.

Save the file directly to the filesystem when it is received by the servlet. You can save other data about the file to DB but instead of saving the file to the DB create a FileOutStream and save it to a File
see http://www.mkyong.com/java/how-to-convert-array-of-bytes-into-file/
After than you can set the img src tag to the physical location of the file, or to a servlet which will return the response as a outputstream

Related

What is the best format for transferring RTFs from a server to a local RTF editor?

I know that I can use a BLOB or an XML file, but I was wondering if there was a better way to do this, or if either of these are the correct way of doing this. The user should be able to edit their file online and then save it onto the server, and at any time get the file back into the editor and continue editing that file (similar to that of Google Drive). Any ideas? We are using Spring Framework and controllers for our site. We are using a MongoDB for file storage (files are stored on the server, file paths are stored on the MongoDB).

Android Print Manager - return saved pdf file path

Using Android PrintManager API user can save content as PDF into device's internal/external directory.
I need to get that PDF file's path as soon as it is created. Is there any way to do this directly without having to use a file chooser?
Any ideas are appreciated.
Using Android PrintManager API we can save content as PDF into device's internal/external directory.
No. The user can elect to send your print job to a PDF file that is stored locally. The user could elect to do something else:
Send it to an actual printer
Send it to something else (e.g., Cloud Print)
Abandon the print job
Is there any way to do this directly without having to use a file chooser?
No, simply because there is no requirement for the user to save the content to a local PDF file in a place that you can access. In addition to all the above non-PDF alternatives, the user could save it as a PDF to removable storage, in a directory that your app cannot access.
I need to get that PDF file's path as soon as it is created
Then the PrintManager API is not suitable. Generate a PDF some other way (e.g., iText).

java open a file(pdf,xsl,etc...) from mongo db

I have files(pdf,doc,txt,xsl,etc..) stored in my mongo *db*. I want to retrieve and open them. I know to extract. Also I found out that Desktop.getDesktop().open(FileName); will open the file with its respective application(Acrobat reader, office suite etc). But can anyone please tell me if only the file would be enough or we should give the full path for the file.
In the latter case, can anyone plesae tell me how I could path for the file which is being retrieved from mongodb?
You'll have to extract the file from Mongodb if you want to open it using Desktop.getDesktop().open(FileName);
You can create a temp file using File.createTempFile and write in it the content fetched from mongodb, with the correct file extension, and then run Desktop.getDesktop().open() to open it.

Any Java plugin/code to manage uploaded files?

According to current requirement,user will upload files with large size,which he may like to download later. I cannot store the uploaded files in DB because the size of files is large and performance will be impacted if I store uploaded files in DB.
Any one knows any java plugin which provide efficient file management on webserver and maintains the link to file so that the file can be downloaded when the link is requested. Also the code will make sure that user will be able to download only those files which is uploaded by them,they cannot download any file just by modifying the download link etc. I am using spring3 as the framework.
Please suggest how to solve this problem?
if you have write access to the file system why not just save them there ?
you then generate an unique ID and save the hash/file relation in db, you then need to supply the ID to get the file feed from a servlet
Store the file content on a part of filesystem out of web application so you cannot reach it changing the link.
Then you can store on db the path for that file, and return them only if the user has the permissions to read it.
Pay attention, do not store all the file on the same folder, or the number of files could grow too much. So find a way to store them with more folder levels.

How to check whether the uploaded file is XLS file or not?

I have a file uploader module. The UI is created using simple HTML and Javascript.
Server side I am using Java code. I want to check the type of the uploaded file.
Files don't really have types. The best you can do would be to attempt to load the file as an XLS.
If the load succeeds, the file is most likely an XLS.
If the load fails, the file is either an invalid/malformed XLS, or not an XLS at all.
You can also look at the uploaded file name or the content type, but neither of these is definitive; i.e. they might say that the file is an XLS when it is something completely different.

Categories

Resources