springboot与php通讯,Springboot第二篇:与前端fetch通信(关于传输数据上传文件等前后端的处理)...
本章讲述的是关于前后端通信关于对应性,前端为react的View,会分传递不同值有不同的处理情况。
首先关于Springboot内的代码变更都是在IndexController.java内,以下是代码:
packagemaven.example.controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/index")public classIndexController {
@RequestMapping("/home")publicString home() {return "Hello World!";
}
}
View Code
1:传递普通类型的数据,如string
前端:
fetch(‘http://localhost:8080/index/name‘, {
method:‘post‘,
headers: {"Content-Type": "application/x-www-form-urlencoded;charset=utf-8"},
body:"firstName=zhu&lastName=yitian",
}).then(response=> response.text()).then(data =>{
alert(data)
}).catch(function(e){
alert("error:" +e);
})
后台:
@RequestMapping("name")
public String getName(@RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName) {
return firstName + lastName;
}
@RequestParam:用于访问 Servlet 请求参数。参数值转换为已声明的方法参数类型。
2:传递Json类型的数据,接收方为类
前端:
let temp ={};
temp.lastName= ‘yitian‘;
temp.firstName= ‘zhu‘;
fetch(‘http://localhost:8080/index/userName‘, {
method:‘post‘,
headers: {‘Content-Type‘: ‘application/json‘},
body:JSON.stringify(temp),
}).then(response=> response.text()).then(data =>{
alert(data)
}).catch(function(e){
alert("error:" +e);
})
后台:
IndexController.java
@RequestMapping("userName")
public String getUserName(@RequestBody User user) {
return user.getFirstName() + user.getLastName();
}
User.java
packagemaven.example.entity;public classUser {privateString lastName;privateString firstName;publicString getLastName(){returnlastName;
}public voidsetLastName(String lastName){this.lastName =lastName;
}publicString getFirstName(){returnfirstName;
}public voidsetFirstName(String firstName){this.firstName =firstName;
}
}
3:传递Json类型的数据, 接收方为map
前端:
let temp ={};
temp.lastName= ‘yitian‘;
temp.firstName= ‘zhu‘;
fetch(‘http://localhost:8080/index/mapName‘, {
method:‘post‘,
headers: {‘Content-Type‘: ‘application/json‘},
body:JSON.stringify(temp),
}).then(response=> response.text()).then(data =>{
alert(data)
}).catch(function(e){
alert("error:" +e);
})
后台:
@RequestMapping("mapName")public String getMapName(@RequestBody Mapmap) {return map.get("firstName") + map.get("lastName");
}
4. 上传单个文件或图片
前端:
上传图片
handleFile(){
let picture= document.getElementById("picture").files;
let formData= newFormData();
formData.append(‘file‘, picture[0]);
fetch(‘http://localhost:8080/index/getPicture‘, {
method:‘post‘,
body:formData,
}).then(response=> response.text()).then(data =>{
alert(data)
}).catch(function(e){
alert("error:" +e);
})
}
后台:
@RequestMapping("getPicture")public String handleFormUpload(@RequestParam("file") MultipartFile file) {try{if (!file.isEmpty()) {byte[] bytes =file.getBytes();
File picture= new File("temp.png");//这里指明上传文件保存的地址
FileOutputStream fos= newFileOutputStream(picture);
BufferedOutputStream bos= newBufferedOutputStream(fos);
bos.write(bytes);
bos.close();
fos.close();return "success";
}
}catch(IOException e){
System.out.println(e);
}return "failed";
}
5.上传多个文件或图片
前端:
上传图片
handleFile(){let picture= document.getElementById("picture").files;
let formData= newFormData();for (let i = 0; i < picture.length; ++i){
formData.append(‘file‘, picture[i]);
}
fetch(‘http://localhost:8080/index/getPictures‘, {
method:‘post‘,
body:formData,
}).then(response=> response.text()).then(data =>{
alert(data)
}).catch(function(e){
alert("error:" +e);
})
}
后台:
@RequestMapping("getPictures")publicString handleFormsUpload(HttpServletRequest request) {try{
Listfiles = ((MultipartHttpServletRequest) request).getFiles("file");
MultipartFile file= null;for(int i = 0; i < files.size(); ++i){
file=files.get(i);if (!file.isEmpty()) {byte[] bytes =file.getBytes();
File picture= new File("temp" + String.valueOf(i) + ".png");//这里指明上传文件保存的地址
FileOutputStream fos= newFileOutputStream(picture);
BufferedOutputStream bos= newBufferedOutputStream(fos);
bos.write(bytes);
bos.close();
fos.close();
}
}return "success";
}catch(IOException e){
System.out.println(e);
}return "failed";
}
springboot与php通讯,Springboot第二篇:与前端fetch通信(关于传输数据上传文件等前后端的处理)...相关推荐
- 微信小程序【网易云音乐实战】(第二篇 轮廓图、阿里巴巴的矢量图标、滚动条、前后端交互、列表渲染)
下面通过webStrom来写代码,通过微信开发者工具来调试! 本篇最终效果图: 这里写目录标题 一.轮播图 二. 五个小图标 1. 将阿里巴巴矢量图标转换为本地的 2. 项目使用图标 三.滚动条 sc ...
- SpringBoot+El-upload实现上传文件到通用上传接口并返回文件全路径(若依前后端分离版源码分析)
场景 SpringBoot+ElementUI实现通用文件下载请求(全流程图文详细教程): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...
- springboot界面上传文件和在页面上显示文件
做一个简单的上传图片和显示图片的测试 首先要有web-starter的包,Springboot中内置了DispatcherServlet也配置好了MultipartResolver. 关于上传文件的特 ...
- SpringBoot第十七篇:上传文件
这篇文章主要介绍,如何在springboot工程作为服务器,去接收通过http 上传的multi-file的文件. 构建工程 为例创建一个springmvc工程你需要spring-boot-start ...
- springboot接收文件上传_SpringBoot第十七篇:上传文件
这篇文章主要介绍,如何在springboot工程作为服务器,去接收通过http 上传的multi-file的文件. 构建工程 为例创建一个springmvc工程你需要spring-boot-start ...
- SpringBoot非官方教程 | 第十七篇:上传文件
转载请标明出处: http://blog.csdn.net/forezp/article/details/71023752 本文出自方志朋的博客 这篇文章主要介绍,如何在springboot工程作 ...
- 企业级 SpringBoot 教程 (十七)上传文件
这篇文章主要介绍,如何在springboot工程作为服务器,去接收通过http 上传的multi-file的文件. 构建工程 为例创建一个springmvc工程你需要spring-boot-start ...
- 在win10本地开发springboot项目能上传图片,并能通过URL直接从浏览器访问,但是部署到服务器上后能上传文件,但是通过浏览器无法访问图片
在win10本地开发springboot项目能上传图片,并能通过URL直接从浏览器访问,但是部署到服务器上后能上传文件,但是通过浏览器无法访问图片 1.首先springboot项目在Window和Li ...
- SpringBoot设置全局上传文件路径并上传文件
前言 在后端处理文件上传的时候,我们通上传文件的时候,一般这个路径不会随便写. 比如这篇文章中的路径:解决SpringBoot文件上传报错:org.apache.tomcat.util.http.fi ...
最新文章
- C#隐藏手机号中间四位为*
- Solaris 10 系统维护
- 批处理 bat 提取项目war包
- python3.6 在 windows10 下使用pycrypto
- 3dContactPointAnnotationTool开发日志(二二)
- Java Native Interface 二 JNI中对Java基本类型和引用类型的处理
- 音频处理四:(音频的分帧)
- 第49讲:实战上手,Scrapy-Redis 分布式实现
- C#中字典集合HashTable、Dictionary、ConcurrentDictionary三者区别
- Java Web-网页基础-HTML基础
- 苹果怎么给软件加密码_用了2年苹果手机,才知道有这几个功能,钱没花冤枉,好用!!...
- go模拟android浏览器,GO浏览器:界面酷炫操作顺畅Android浏览器
- mysql中哪一个储存逻辑型_《VisualFoxPro》2018秋华东年季学期在线作业(一)二三...
- [渝粤教育] 云南大学 大学生心理健康教育 参考 资料
- java类库详解_【Java系列-4】Java常用类库_详解
- python编写鸡兔同笼程序_鸡兔同笼问题的python实现
- 小程序运营主要做什么?如何推广比较好?
- QCC3040---earbudUi module
- 今天收到一封非常牛B的离职信
- 为什么电脑不能安全关闭计算机,电脑无法正常关机的原因以及解决办法