• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

spring boot实现上传图片并在页面上显示及遇到的问题小结

java 搞代码 4年前 (2022-01-05) 27次浏览 已收录 0个评论

最近在使用spring boot搭建网站的过程之中遇到了有点小问题,最终解决方案是在main目录下新建了一个webapp文件夹,并且对其路径进行了配置,本文重点给大家介绍spring boot实现上传图片并在页面上显示功能,需要的朋友参考下吧

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

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

注册界面:

   <title>Title</title>  <label>姓名</label><label>密码</label><label>上传图片</label>

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 2017/8/9. */ @Controller public 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-600")));//保存图片到目录下 out.write(file.getBytes()); out.flush(); out.close(); String filename="f:\\旗杯\\demo5\\src\\main\\webapp\\"+name+".jpg-600"; 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>Title</title> <p>用户名:</p><p>图片:</p>

对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 2017/8/9. */ @Configuration public 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 2017/8/9. */ @Entity public 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 2017/8/9. */ public interface UserRepository extends JpaRepository{ }

最后运行如下:

注册上传头像:

 

个人中心:

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

附上传的这个小demo的地址:

http://xiazai.gaodaima.com/201712/yuanma/demo5(gaodaima.com)来源gao@!dai!ma.com搞$$代^@码!网.rar

总结

以上所述是小编给大家介绍的spring boot实现上传图片并在页面上显示及遇到的问题小结,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对gaodaima搞代码网网站的支持!

以上就是spring boot实现上传图片并在页面上显示及遇到的问题小结的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:spring boot实现上传图片并在页面上显示及遇到的问题小结

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址