java spring formdata,Spring中formdata方式提交json对象和file之二(改进版)

Spring中formdata方式提交json对象和file之二(改进版)

Spring中formdata方式提交json对象和file之二(改进版)

为什么80%的码农都做不了架构师?>>>

2617e3bda9430ed2aaf35105046eee19.png

问题

想使用最最最原生的表单提交上传多个文件,而且,这些上传多个文件的name是个变量。在之前《Spring中formdata方式提交json对象和file之一》的基础上面进行进一步改进。具体如下图:

dc898b69963e0b13373c1b17f0224e77.png

解决

vue动态添加file

spring获取MultipartFile

vue动态添加file

JavaScript

var app = new Vue({

el: '#app',

data: {

form : form,

files: []

},

// 在 `methods` 对象中定义方法

methods: {

processFile: function(event, index) {

var file = {};

file.index = index;

file.file = event.target.files[0]

Vue.set(this.files, this.files.length, file);

},

submit: function (event) {

// 可以提交

var formData = new FormData();

formData.append("form", new Blob([JSON.stringify(this.form)], {type: "application/json"}))

var length = this.files.length;

for(var i = 0; i < length; i++){

var file = this.files[i];

formData.append(file.index, file.file);

}

this.$http.post("../formdata/submit", formData, {

headers: {

'Content-Type': 'multipart/form-data'

}

}).then(function(response) {

}, function(response) {

// error callback

});

}

}

});

HTML

**Note:**这里把input绑定到了processFile方法,这里的index是Vue中的form对象中items的循环索引。

f8d707e1ad422f9d5dee91e67c1d659b.png

spring获取MultipartFile

Java

@RequestMapping(value = "/submit",

method = RequestMethod.POST,

consumes = {"multipart/form-data"})

@ResponseBody

public ResponseEntity submit(StandardMultipartHttpServletRequest request,@RequestPart("form") Form form) {

List nowItemsList = form.getItems();

Iterator iterator = nowItemsList.iterator();

int index = 0;

while (iterator.hasNext()) {

LineItems nowLine = iterator.next();

String viewType = nowLine.getViewType();

// 文件上传

MultiValueMap map = request.getMultiFileMap();

if (!map.isEmpty()) {

// 这里的index与前端循环索引是一致的

List multipartFileList = map.get(String.valueOf(index));

if (multipartFileList != null) {

for (MultipartFile multipartFile : multipartFileList) {

// 这里就获取到了前端的文件对象:multipartFile

}

}

}

index = index + 1;

}

}

**Note:**MultipartFile和StandardMultipartHttpServletRequest类都是Spring中类。

感受

这样就通过数组的索引,找到对应提交的文件了,在也不用担心动态多文件提交的问题了。

参考:

Vuejs2 file input

多文件上传以及java后台接受

angular, spring MVC and multipart/form-data

StandardMultipartHttpServletRequest

How to get Form data as a Map in Spring MVC controller?

转载于:https://my.oschina.net/fxtxz2/blog/1828907

Spring中formdata方式提交json对象和file之二(改进版)相关教程

Spring boot中使用Swagger2

Spring boot中使用Swagger2 为什么80%的码农都做不了架构师? 问题 最近需要做接口开发,给客户端们调用,但是我又不想写文档,听说REST风格的接口都在用Swagger做IDL(Interface description language),中文就是接口描述语言,简单的说就是给调用方的开发

Spring 2.0.4中使用OAuth2.0认证

Spring 2.0.4中使用OAuth2.0认证 为什么80%的码农都做不了架构师? 问题 最近有个需求是要给系统里面的所有REST请求,弄一个token,然后,那着这个访问token,去掉接口。阮一峰写了两遍文章,值得我们一看: RESTful API 设计指南 理解OAuth 2.0 这里假设我们

spring-retry使用以及源码

spring-retry使用以及源码 spring retry是从spring batch独立出来的一个能功能,主要实现了重试和熔断,对于重试是有场景限制的,不是什么场景都适合重试, 比如参数校验不合法、写操作等(要考虑写是否幂等)都不适合重试。 比如外部 RPC 调用,或者数据入库

Spring boot文件上传

Spring boot文件上传 为什么80%的码农都做不了架构师? Spring boot import org.apache.commons.lang3.RandomStringUtils;@Controllerpublic class FileUploadController {@PostMapping(/updateFile) public ResponseEntity updateFile(HttpServletRequest se

nginx解决vuejs与springboot跨域问题

nginx解决vuejs与springboot跨域问题 为什么80%的码农都做不了架构师? 问题 在实施前后端分离的时候,vuejs与springboot通常不在同一台服务器,这个时候,vuejs调用springboot的时候通常会出现跨域问题。 解决思路 这里的解决方案,主要是通过nginx搭建一个

JUnit 5 测试 Spring 引擎的时候提示 junit-vintage 错误

JUnit 5 测试 Spring 引擎的时候提示 junit-vintage 错误 在 Spring 项目中运行测试的时候,得到错误: TestEngine with ID 'junit-vintage' failed to discover tests” with Spring 这个错误的原因是 JUnit 的引擎,使用了 junit-vintage 引擎。 junit-vint

Selenium Docker的Grid方式进行自动化测试

Selenium Docker的Grid方式进行自动化测试 为什么80%的码农都做不了架构师? 问题 最新想通过界面批量输入数据到系统里面。 思路 使用自动化测试工具解决这个批量操作到问题。 Selenium selenium/hub selenium/node-chrome他们两者之间的关系,就是hub是管理

Spring boot中Get请求中Date参数的传递

Spring boot中Get请求中Date参数的传递 为什么80%的码农都做不了架构师? 问题 想要在Get请求中,传递Date参数,如下: @GetMapping(/findByPeriodOrderByTimeDesc)public ResponseEntityListMessage getMessagesPeriodByTimeDesc(@RequestParam @DateTimeFor

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值