springboot 实现接收前端发来的图片和视频以及在页面上展示图片和视频
springboot 实现接收前端发来的图片和视频以及在页面上展示图片和视频
一、效果:
1、上传图片
2、显示上传的图片
3、上传的视频
4、显示上传的视频
二、代码
没依赖特殊的包,引入springweb就行
1、配置文件:
# 应用名称
spring.application.name=demo
# 应用服务 WEB 访问端口
server.port=8080
#设置上传文件大小,默认是1M
spring.servlet.multipart.max-request-size=200MB
spring.servlet.multipart.max-file-size=200MB
2、controller类:
package com.example.demo; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; /** * <p>描述: 图片和视频上传和查看。</p> * <p>文件名称: Controller.java</p> * <p>创建时间: 2022-07-08 </p> * @author 万笑佛 * @since 2022-07-08 */ @RestController @RequestMapping("/editor") public class Controller { /** * 图片上传。 * * @param fileUpload * @return JSONObject */ @PostMapping(value = "/image/upload" ) @ResponseBody public void imageUpload(@RequestParam("file") MultipartFile fileUpload) { //获取文件名 String fileName = fileUpload.getOriginalFilename(); String tmpFilePath = "D://test//image//" ; //没有路径就创建路径 File tmp = new File(tmpFilePath); if (!tmp.exists()) { tmp.mkdirs(); } String resourcesPath = tmpFilePath + "//" + fileName; File upFile = new File(resourcesPath); try { fileUpload.transferTo(upFile); } catch (IOException e) { e.printStackTrace(); } } /** * 图片查看。 * @return String */ @GetMapping("/image/look") public String imageLook (HttpServletResponse response) { File file = new File("D:\\test\\image\\1.png"); byte[] bytes = new byte[1024]; try (OutputStream os = response.getOutputStream(); FileInputStream fis = new FileInputStream(file)){ while ((fis.read(bytes)) != -1) { os.write(bytes); os.flush(); } } catch (IOException e) { e.printStackTrace(); } return "success"; } /** * 视频上传。 * * @param fileUpload * @return JSONObject */ @PostMapping(value = "/video/upload" ) @ResponseBody public void videoUpload(@RequestParam("file") MultipartFile fileUpload) { //获取文件名 String fileName = fileUpload.getOriginalFilename(); String tmpFilePath = "D://test//video//" ; //没有路径就创建路径 File tmp = new File(tmpFilePath); if (!tmp.exists()) { tmp.mkdirs(); } String resourcesPath = tmpFilePath + "//" + fileName; File upFile = new File(resourcesPath); try { fileUpload.transferTo(upFile); } catch (IOException e) { e.printStackTrace(); } } /** * 视频查看。 * @return String */ @GetMapping("/video/look") public String videoLook (HttpServletResponse response) { File file = new File("D:\\test\\video\\1.mp4"); byte[] bytes = new byte[1024]; try (OutputStream os = response.getOutputStream(); FileInputStream fis = new FileInputStream(file)){ while ((fis.read(bytes)) != -1) { os.write(bytes); os.flush(); } } catch (IOException e) { e.printStackTrace(); } return "success"; } }
资源丰富的的网盘资源:网盘资源大全! 推荐一个适合零基础学习SQL的网站:不用安装数据库,在线轻松学习SQL!
分类:
springboot
标签:
springboot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)