Today is the 148th day for Liu Xiaoai to learn Java by herself.

Thank you for watching. Thank you.


The study plan is as follows:

  • I wrote the new brand business yesterday, and one of the parameters is the picture, but we did not upload the picture.
  • Today is mainly to complete the picture upload function.
  • Since uploading not only involves uploading pictures, but also other uploads, an independent micro-service is created for its universality.

Create an Upload microservice

There are three core steps to microservice creation: importing dependencies, configuration files, and startup class authoring.

Here are the points to note in the configuration file.


Port number for uploading microservices: 8082.

The name of the micro-service is upload-service

Since it is to upload pictures, so to set the size of the upload, can not let any size of pictures can be uploaded.

  • Max-file-size: specifies the size of the uploaded file. For example, the size of a file cannot exceed 5M.
  • Max-request-size: specifies the size of the requested data. In this example, a maximum of 10 mbit/s can be sent at a time.

Other configurations such as Eureka registration are not mentioned.

As for dependencies, what you need is just imported in the POM file, and the startup class is easy to write, which has been written many times before.

About the request

Yesterday in the analysis of the front page of the time explained:


① Request path/mode

The full path is the corresponding URL in the figure above. It is the gateway mapping path + the real path. The real path is image.

The request mode is POST.

② Request parameters

The parameter is the file, and its return value is a string, the url of the image file.

3 Configure a mapping path for the gateway

The Upload mapping path corresponds to the Upload microservice.

In other words, when the browser accesses the gateway mapping path, it is essentially performing the Upload microservice.

Second, background code writing

Because it does not operate on the database itself, it does not involve the code writing of the Mapper layer.

1 the controller layer


These are the same four things:

  • Request path: real path image, as analyzed above.
  • Request mode: POST request.
  • Request parameter: file, which is packaged as MultipleFile, as we learned in SpringMVC.
  • Return value: the url string corresponding to the image.

2 the service layer

Since it’s an image file, we need to check its type first, not a text file or a video file; Then check the content of the picture.


① Image type verification

MultipartFile has a getContentType method that gets the type of image to upload.

We store the types of images in a collection, and then compare the collection with the uploaded image types.

PNG files, JPG files are common image file types, and BMP files, which I forgot to mention above.

② Picture content verification

We will store the image in nginx, create a file to specify the corresponding HTML folder path.

In the past, we need to copy the file through IO stream to complete the uploading and saving of pictures.

MultipartFile provides a transferTo() method to copy files directly.

③ Return the image URL

Return a URL, the domain name of the image + the image name, which can also be unique using a UUID.

PS: Why do image addresses use separate urls?

If it is stored inside the server, there is an additional burden, which is clearly not appropriate.

If you do not use a separate domain name, access to static resources will carry unnecessary cookies to increase the amount of requested data.

So use a separate domain name, in which case we need to configure the domain name for the image.

Image domain name and testing

Since the domain name is specified in the return value, SwitchHosts and Nginx do the same for the domain name.


This is something we covered specifically in Nginx, the study note for day 142.

What, in short, can it achieve?

Enter the full path of the image in the browser to access the HTML file in nginx.

Again, of course, to emphasize:

The configuration is just good, know that there is a domain name this thing, because the domain name needs to buy very troublesome, so had to configure.

Domain name configuration is complete, code is written, do a test:


Use Insomnia to simulate uploading images:

Send a request to the Upload micro service, which accepts the request and saves the uploaded image to Nginx.

The response status code 200 indicates success.

We can see the saved image in the corresponding HTML folder. Because the domain name is configured, the image can be accessed directly by using the domain name in the browser.

The last

Do not look back on yourself, I am @Liu Xiaoai

I am a post-95 hupiao who works during the day and studies at night. I only want to learn self-discipline and do a good job of myself. I hope my daily punch card can bring you courage.