简体   繁体   中英

How to store file and reference it to java web application?

It is kind of basic thing to store file as it seems, but I stumbled upon certain problem. Here is what I understood about storing files on server while programming in JAVA.

  1. You can't store in mysql, because if the file is heavy, it is not recommended. So, I decided not to opt this.

  2. It is also not recommended to store files in containers, such as Tomcat or Wildfly, may be because of the fact that it needs to get deployed and something like that??

  3. You can definitely store file, Apache File server? I am confused in this. Can we do this, and store files in here, reference it to the database? Is this the similar way websites store their images or files on?

  4. I also came across some databases such as NoSql, but I didn't go much into depth, thinking it might be wrong at the end, and I would have invested my time in other stuff.

Saying so, what is the good way to store file in the server, reference it on JAVA web application, and record it in database?

We don't usually put our files in the database (NoSQL or RDBMS) because they are not file systems. If someone uploads a file, you store it in a file system and probably record the name and other metadata in the db for future use. You can technically put the contents of a file in a database and it has its own merits and drawbacks - https://softwareengineering.stackexchange.com/a/150787/156860

Uploaded files are not recommended to reside in web/app servers because you might have more than one server to handle the load and if you put your file in just one server, the other server might face problems in accessing the content. And if one server goes down, the files in that server are not available for other servers to use. You'd probably need a shared drive/disk which all the servers can connect to and read the file.

JavaEE handles this by providing a Resource Adapter which abstracts how the application interacts with 'resources' in general (could be a file or some other resource as well). But without having more detail on what kind of files, how big, and how are they being used, it'll be hard to get to 'the' solution for the question.

It really depends on what your use-case is. If you give more insight into what you're trying to solve for you should get better responses.

I think generally speaking that cloud solutions like Amazon's S3 are very common, good, and easy to work with these days. You also get the benefit of your servers not necessarily having to serve the files themselves which means more bandwidth for non-file based requests.

You can't store in mysql, because if the file is heavy, it is not recommended. So, I decided not to opt this.

It really depends on what you're doing, if you have high traffic and large files then this isn't going to scale well. If you have low traffic and small files, then there's no reason this isn't a viable solution.

It is also not recommended to store files in containers, such as Tomcat or Wildfly, may be because of the fact that it needs to get deployed and something like that??

Not quite sure what you mean by "in" containers, do you mean in your war file? Or on the local file system? It's generally not recommended but again, it really depends on traffic, file sizes, etc.. The local file system can work, but you should use something like NFS so if the server goes down the files don't go with it.

I've seen people use the local file system and git to store text files that are saved through the server. This gave them redundancy and versions.

You can definitely store file, Apache File server? I am confused in this. Can we do this, and store files in here, reference it to the database? Is this the similar way websites store their images or files on?

I've never personally used Apache File Server, but what's confusing you about it? After doing a simple Google it looks like it's basically just an Apache server, which means you make HTTP requests to store and retrieve files. This would be similar to how S3 works and so I'd say it's probably a good general solution.

I also came across some databases such as NoSql, but I didn't go much into depth, thinking it might be wrong at the end, and I would have invested my time in other stuff.

The actual NoSql solution you choose will have it's own requirements so I can't speak to how good the solution will be, but again, it really depends on your traffic and file sizes. The NoSql solutions I've used all have HTTP interfaces so working with them won't be all that different than Apache or S3. The biggest difference is that you have to query for the files like you would from a Database in a NoSql solution, but those queries are usually sent over HTTP so, not really that different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM