Long ago the single application project upload pictures are very simple, after uploading pictures in the controller layer set the path and save to a path on the server line, the database storage path address, and finally set a virtual path in Tomcat, many years ago, most of them did so.
However, with the technology update iterative, SOA, microservices, such practices will be eliminated, if it is distributed deployment or cluster environment, upload files to their respective servers, is not able to achieve unity, then use the image server, before I mentioned Fastdfs, This is a very useful file server, not much to say here. Each project uploaded images are unified by the file server to manage, then in a distributed environment or cluster environment, according to the assigned path to access the image can be obtained.
1, the use of File stream processing, this result can be used by the browser, iOS and Android calls, from Multiparthttpservletrequest to get Multipartfile objects, and finally call the file server interface upload can
2. Upload using base64
After cutting the picture can produce a bunch of Base64 string, this string is you want to upload the picture, as long as the string to the background to decode processing, and finally in the use of Fastdfs to upload the line
So what's the difference between these two ways?
1, access to the file stream image requires 2 HTTP requests, and BASE64 only need one time
2, file stream images can be accessed in any browser, and base64 some browsers do not support
3, base64 picture cannot be cached (cache whole page is another matter)
4, base64 encoded picture is relatively large, so it is generally used to make small pictures, such as head
Use:
File stream is used to upload large images, such as some advertising pictures ah, product pictures and so on
Base64 pictures can be used to upload a user's avatar, or a small logo, or a small icon, etc.
Base64 can be saved to the database, without the need to upload, but I do not recommend this, the picture should be unified in the file server management, of course, this look at the demand design
?
Two forms of picture uploading