SpringBoot http post请求数据大小设置

背景:

使用http post请求方式的接口,使用request.getParameter("XXX");的方法获取参数的值,当数据量超过几百k的时候,接口接收不到数据或者接收为null。

    @RequestMapping(value = "/rcv",method = RequestMethod.POST)
    public ResInfo<String> pullApi(HttpServletRequest request) {
        String channel = request.getParameter("channel");
    }

 

在application.properties里添加:

spring.http.multipart.max-file-size=-1
spring.http.multipart.max-request-size=-1

默认值: private String maxFileSize = "1MB"; private String maxRequestSize = "10MB";

------------------------------------------------------------------------------------------------------------------------------------
server.tomcat.max-http-post-size=-1

这个设置是大小不限制,主要是这个设置。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
在端,可以使用 Element UI 的 Upload 组件来上传文件,同时将表单数据也一并提交到后端。具体代码如下: ```html <template> <el-form :model="form" label-width="120px"> <el-form-item label="姓名"> <el-input v-model="form.name"></el-input> </el-form-item> <el-form-item label="头像"> <el-upload class="avatar-uploader" action="/api/upload" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload"> <img v-if="imageUrl" :src="imageUrl" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm">提交</el-button> </el-form-item> </el-form> </template> <script> export default { data() { return { form: { name: '', avatar: '' }, imageUrl: '' } }, methods: { handleAvatarSuccess(res, file) { this.form.avatar = res.url this.imageUrl = URL.createObjectURL(file.raw) }, beforeAvatarUpload(file) { // 限制上传图片的大小和格式 const isJPG = file.type === 'image/jpeg' const isPNG = file.type === 'image/png' const isLt2M = file.size / 1024 / 1024 < 2 if (!isJPG && !isPNG) { this.$message.error('上传头像图片只能是 JPG/PNG 格式!') return false } if (!isLt2M) { this.$message.error('上传头像图片大小不能超过 2MB!') return false } return true }, submitForm() { this.$refs.form.validate(valid => { if (valid) { const formData = new FormData() formData.append('name', this.form.name) formData.append('avatar', this.form.avatar) // 将表单数据和文件一起提交到后端 this.$http.post('/api/user', formData).then(res => { this.$message.success('提交成功') }).catch(error => { this.$message.error('提交失败') }) } else { this.$message.error('表单验证失败') return false } }) } } } </script> ``` 在后端,使用 Spring Boot 的 MultipartFile 类型来接收上传的文件,并在 Controller 中处理表单数据和文件上传的逻辑。具体代码如下: ```java @RestController @RequestMapping("/api") public class UserController { @PostMapping("/user") public ResponseEntity<?> createUser(@RequestParam("name") String name, @RequestParam("avatar") MultipartFile avatar) { // 处理表单数据和文件上传的逻辑 // ... return ResponseEntity.ok().body("创建用户成功"); } } ``` 需要注意的是,由于使用了 FormData 对象来提交表单数据和文件,因此需要在前端使用 axios 或者其他 HTTP 请求库来发送 POST 请求。在上面的代码中,我使用了 Vue.js 的官方插件 vue-resource 来发送 POST 请求。如果您使用的是 axios,则可以将代码修改为: ```javascript import axios from 'axios' submitForm() { this.$refs.form.validate(valid => { if (valid) { const formData = new FormData() formData.append('name', this.form.name) formData.append('avatar', this.form.avatar) // 将表单数据和文件一起提交到后端 axios.post('/api/user', formData).then(res => { this.$message.success('提交成功') }).catch(error => { this.$message.error('提交失败') }) } else { this.$message.error('表单验证失败') return false } }) } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值