作用是向网页中添加图片,并且img标签有多个可用参数可以添加。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
spring.servlet.multipart.max-request-size=10MB spring.servlet.multipart.max-file-size=10MB
upload.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <body> <h1>Spring Boot file upload example</h1> <form method="POST" action="/upload" enctype="multipart/form-data"> <input type="file" name="file" /><br/><br/> <input type="submit" value="提交" /> </form> </body> </html>
resut.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <body> <div th:if="${message}"> <h2 th:text="${message}"/> </div> </body> </html>
@Controller public class SampleController { @GetMapping("/") public String upload() { return "upload"; } @RequestMapping("/result") public String result() { return "result"; } @PostMapping("/upload") public String singleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { if (file.isEmpty()) { redirectAttributes.addFlashAttribute("message", "Please select a file to upload"); return "redirect:result"; } try { // Get the file and save it somewhere byte[] bytes = file.getBytes(); Path path = Paths.get(uploadDirectory() + "/" + file.getOriginalFilename()); Files.write(path, bytes); redirectAttributes.addFlashAttribute("message", file.getOriginalFilename() + " upload success"); } catch (IOException e) { e.printStackTrace(); } return "redirect:/result"; } private String uploadDirectory() throws FileNotFoundException { //获取跟目录 File path = new File(ResourceUtils.getURL("classpath:").getPath()); if(!path.exists()) path = new File(""); System.out.println("path:"+path.getAbsolutePath()); //如果上传目录为/static/images/upload/,则可以如下获取: File upload = new File(path.getAbsolutePath(),"static/upload/"); if(!upload.exists()) upload.mkdirs(); System.out.println("upload url:"+upload.getAbsolutePath()); //在开发测试模式时,得到的地址为:{项目跟目录}/target/static/images/upload/ //在打包成jar正式发布时,得到的地址为:{发布jar包目录}/static/images/upload/ return upload.getAbsolutePath(); } }
选择上传文件进行上传
站文章怎么添加图片?在进行网站优化时,站内文章的质量很重要,其中除了需要撰写高质量的内容,注意关键词的密度布局这些,添加图片也具有一定的优化作用,有利于提升用户的体验。那么如何在文章中添加图片呢?
通常,在网站文章中添加图片有手动、半手动和自动这三种方法。
手动添加图片就是手动点击添加本地的图片,这种方式的效率较低,而且需要自己找寻合适的图片;半手动添加图片就是先处理好图片并命名,打包上传到服务器,然后利用工具批量插入图片在服务器中的地址,找到图片存储文件夹,记录其路径,这样文章发布时就避免手动查找图片,比纯手动添加效率高;自动添加图片就是利用插件来实现。有一些CMS程序中会有很多插件是可以使用的,如果没有图片添加的相关插件,可以找人开发一个。这个插件主要实现的功能是,按照指定的图片作为底部图层,而后在图片上自动生成文字,并且是以文章标题为准,这样这篇文章就有了专属图片。当然,进一步优化就是实现不同文章位置插入图片。这样做的好处是,如果底层图片没有版权,而且文字可以商用,也就避免了侵权的麻烦。
了解完如何在文章中添加图片,对于添加的图片还有一些问题是需要注意的。
在处理图片时,需要先确定图片的正确大小,等上传后发现尺寸不对再去修改是很浪费时间的,而图片太大会拖累网站的加载速度,这一点是需要注意的。另外,还需要考虑网站支持格式的问题,通常是jpg、png比较好。
搜索引擎是无法识别图片的,必须用html标签中的alt标签属性来识别,因此给图片添加Alt标签是必不可少的操作,这有利于蜘蛛抓取。
图文结合的文章能增加用户的阅读体验,但是太多的图片会有反效果,因此一篇文章中的一两张图片就足够了。另外要注意,图片太多也会拖累网站的加载速度,所以要控制图片的数量,不仅是和文字要有一个平衡点,还有和网站结构也要有一个平衡点。
在文章中添加图片当然不是随意添加的,应该为该页面配备符合主题的相关图片,要注意网站图片和网站内容的相关联性。
关于网站文章怎么添加图片及图片添加要注意的问题,我们就讨论到这里,以上内容仅供参考。对于网站图片优化的细节大家一定要多多注意,最好希望这篇能对大家进行网站优化有帮助。
*请认真填写需求信息,我们会在24小时内与您取得联系。