失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > spring boot实现上传图片遇到的问题小结

spring boot实现上传图片遇到的问题小结

时间:2022-06-08 22:34:43

相关推荐

spring boot实现上传图片遇到的问题小结

后端开发|php教程

spring,boot,图片

后端开发-php教程

最近在使用spring boot搭建网站的过程之中遇到了这样一个问题:用户注册时需要上传一个属于自己的头像,注册成功之后跳转到个人中心,在个人中心中显示用户信息.其中在显示头像的时候遇到了问题:上传头像的时候,我把头像存放到了项目文件下的static文件夹中,将其地址存放到了数据库对应的用户中,并且在idea中添加了热部署,但是在注册跳转到个人中心后还是无法显示头像,只有在下一次启动项目进入到个人中心时才可以。

点击计数源码,ubuntu没有独立显卡,tomcat提示已经在使用,爬虫地址栏,php表达式核心代码,东台seo公司lzw

被这个问题困扰了许久,最后是这样的解决的:在main目录下新建了一个webapp文件夹,并且对其路径进行了配置。下面是一个解决方案的小demo,做的比较简单,请见谅~~核心代码如下:

互评系统源码,如何卸载vscode,ubuntu切换图形界面黑屏,tomcat 改变端口,安卓sqlite封装,dx 插件,前端可视化开发框架,北京多腿椭圆爬虫,php手机app,如皋优化seo,语言学习交流网站,html点击图片弹出网页代码,rfi模板,vb 简易写字板程序设计论文lzw

注册界面:

asp的项目管理系统源码,Ubuntu能日常使用,常见黑色爬虫种类,php 统计访问,go语言seolzw

Title

control如下:

package com.example.demo.control;import com.example.demo.dao.UserRepository;import com.example.demo.domain.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.multipart.MultipartFile;import java.io.*;/** * Created by 18274 on /8/9. */@Controllerpublic class Control { @Autowired UserRepository userRepository; @GetMapping(value="/zhuce") public String zhuce(){ return "zhuce"; } @PostMapping(value="/zhuce") public String tijiao(@RequestParam(value="name") String name, @RequestParam(value="password") String password, @RequestParam(value="file")MultipartFile file, Model model) { User user = new User(); user.setUsername(name); user.setPassword(password); if (!file.isEmpty()) { try { BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(new File("f:\\旗杯\\demo5\\src\\main\\webapp\\"+name+".jpg")));//保存图片到目录下 out.write(file.getBytes()); out.flush(); out.close(); String filename="f:\\旗杯\\demo5\\src\\main\\webapp\\"+name+".jpg"; user.setTupian(filename); userRepository.save(user);//增加用户 } catch (FileNotFoundException e) { e.printStackTrace(); return "上传失败," + e.getMessage(); } catch (IOException e) { e.printStackTrace(); return "上传失败," + e.getMessage(); } model.addAttribute(user); return "permanager"; } else { return "上传失败,因为文件是空的."; } }}

个人中心:

Title

用户名:

图片:

对webapp路径的配置

package com.example.demo.config;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;/** * Created by 18274 on /8/9. */@Configurationpublic class MyWebAppConfigurer extends WebMvcConfigurerAdapter{ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/src/main/webapp/**").addResourceLocations("classpath:/webapp/"); super.addResourceHandlers(registry); }}

对应的用户实体类:

package com.example.demo.domain;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;/** * Created by 18274 on /8/9. */@Entitypublic class User { @Id @GeneratedValue private Long id; private String username; private String password; private String tupian;//图片地址 public User(){} public Long getId() { return id; } public String getUsername() { return username; } public String getPassword() { return password; } public String getTupian() { return tupian; } public void setId(Long id) { this.id = id; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } public void setTupian(String tupian) { this.tupian = tupian; }}

用户实体类的接口:

package com.example.demo.dao;import com.example.demo.domain.User;import org.springframework.data.jpa.repository.JpaRepository;/** * Created by 18274 on /8/9. */public interface UserRepository extends JpaRepository{}

最后运行如下:

注册上传头像:

个人中心:

ps:如果在结合spring security的话,只需要从session.SPRING_SECURITY_CONTEXT.authentication.principal.XXX中取得信息即可。

以上内容就是spring boot实现上传图片遇到的问题小结,希望能帮助到大家。

php判断文件上传图片格式的方法介绍

3种上传图片的方法实例

推荐10款上传图片特效(收藏)

如果觉得《spring boot实现上传图片遇到的问题小结》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。