本文介绍了JSP:将文件上传到服务器的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用多部分表单,Apache FileUpload等上传文件。它工作正常。
但是,我想知道什么是最佳做法或常见做法保存在服务器中的文件,根据以下内容:
- 命名服务器中的文件(即:什么名称更好吗?生成一些UUID,当我插入文件相关的数据时,由db表生成的行ID)
- 服务器内的文件的最佳位置(有什么更好的?即在Linux服务器中我应该使用哪个文件夹或分区。我需要加密上传的文件吗?)
- 当我把一个链接从浏览器中存取文件时:是直接存取还是使用servlet? $ b如果你这样做(文件系统中的文件,数据库中的元数据),那么你可以使用这个方法来实现这个功能。对于文件名的行ID是不是一个坏主意(至少它确保唯一性)。不幸的是,你将不得不小心文件系统和数据库是同步的,所以需要仔细的编码。
- 如果您关心性能,可以将文件存储在单独的硬盘(或NAS)上。请注意,如果文件数量很大(数千),则不应将其全部放在一个文件夹中,而应将其分组放在子文件夹中,每个文件夹最多包含数百个文件。如果文件数量变大,它将确保访问时间短。使用加密应该取决于您的业务需求(文件是否包含机密数据?)。
- Servlet是更好的方法,因为它隐藏了客户端的真实存储细节,证明未来应用程序的变化。它还有一些其他的好处(例如,你可以实现你的访问控制,你可以在浏览器/代理开箱即可获得缓存等)。如果您使用加密,这是必须的。
- Naming the files in server (i.e.: What name is better? Some UUID generated, or the row ID generated by db table when I insert the file associated data)
- The best location for files inside the server (What is better? i.e. In linux server which folder or partition I should use. Do I have to encrypt the uploaded files?)
- When I put a link to access the files from browser: Is better a direct access, or using a servlet?
- If you do it this way (files in filesystem, metadata in DB) then row ID for filename is not a bad idea (at least it ensures uniqueness). Unfortunately you will have to take care that filesystem and database are in sync, so it will require careful coding.
- If you care for performance files can be stored on a separate HDD (or NAS). Note that if the number of files is going to be big (thousands) you should not put all of them in one folder, but instead group them in subfolders, each containing at most several hundreds of files. It will ensure low access time if the number of files gets big. The use of encryption should depend on your business needs (do the files contain confidential data?).
- Servlet is a better way, as it hides the real storage details from the client and it's more proof for future changes in the application. It has also some other benefits (eg. you can implement your access control, you get caching in browsers/proxies out-of-the-box, etc ). And it's a must if you use encryption.
I am uploading files using multipart form, Apache FileUpload, etc. It work fine.
But, I want to know what are the best practices or common practices when saving files in server, according to following:
解决方案
这篇关于JSP:将文件上传到服务器的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!