1. Overview
1.概述
In this quick tutorial, we’ll take a look at how to upload an image in a Java web application using Spring Boot and Thymeleaf.
在这个快速教程中,我们将看看如何使用Spring Boot和Thymeleaf在Java Web应用程序中上传图片。
2. Dependencies
2.依赖性
We’ll only need two dependencies — Spring Boot web and Thymeleaf:
我们只需要两个依赖项–Spring Boot web和Thymeleaf。
3. Spring Boot Controller
3.Spring Boot控制器
Our first step is to create a Spring Boot web controller to handle our requests:
我们的第一步是创建一个Spring Boot网络控制器来处理我们的请求。
We’ve defined two methods to handle HTTP GET requests. The displayUploadForm() method handles the GET request and returns the name of the Thymeleaf template to display to the user in order to let him import an image.
我们已经定义了两个方法来处理HTTP GET请求。displayUploadForm()方法处理GET请求,并返回要显示给用户的Thymeleaf模板的名称,以便让他导入图片。
The uploadImage() method handles the image upload. It accepts a multipart/form-data POST request on image upload and saves the image on the local file system. The MultipartFile interface is a special data structure that Spring Boot provides to represent an uploaded file in a multipart request.
idea上传图片
uploadImage()方法处理图片的上传。它接受图像上传的multipart/form-data POST请求,并将图像保存在本地文件系统中。MultipartFile接口是Spring Boot提供的一种特殊数据结构,用于表示多部分请求中的上传文件。
Finally, we created an upload folder that will store all the uploaded images. We also added a message, containing the name of the uploaded image, to display after the user submits the form.
最后,我们创建了一个上传文件夹,将存储所有上传的图片。我们还添加了一个消息,包含上传图片的名称,在用户提交表单后显示。
4. Thymeleaf Template
4.百里香树叶模板
The second step is to create a Thymeleaf template that we’ll call index.html in the path src/main/resources/templates. This template displays an HTML form to allow the user to select and upload an image. Furthermore, we use the accept=”image/*” attribute to allow users to import images only instead of importing any kind of files.
第二步是创建一个Thymeleaf模板,我们称之为index.html,路径为src/main/resources/templates。这个模板显示一个HTML表单,允许用户选择和上传图片。此外,我们使用accept=”image/*”属性,允许用户只导入图片,而不是导入任何类型的文件。
Let’s see the structure of our index.html file:
让我们看看我们的index.html文件的结构。
5. Custom File Size
5.自定义文件大小
If we try to upload a large file, a MaxUploadSizeExceededException exception will be thrown. However, we can tune the file upload limits through the properties spring.servlet.multipart.max-file-size and spring.servlet.multipart.max-request-size that we define in the application.properties file:
如果我们试图上传一个大文件,将抛出MaxUploadSizeExceededException异常。然而,我们可以通过spring.servlet.multipart.max-file-size和spring.servlet.multipart.max-request-size这两个属性来调整文件上传限制,我们在application.properties文件中定义了这些属性。
6. Conclusion
6 结语
In this quick article, we presented how to upload an image in a Java web application based on Spring Boot and Thymeleaf.
在这篇快速文章中,我们介绍了如何在一个基于Spring Boot和Thymeleaf的Java网络应用中上传图片。
As always, the complete source code for this article can be found over on GitHub.
一如既往,本文的完整源代码可以在GitHub上找到。
今天的文章
idea上传图片分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/141243.html