


This article mainly introduces the use of FormData objects and Spring MVC to implement the Ajax file download function. Friends in need can refer to
Ajax file download
Using the FormData object and Spring MVC to implement the Ajax file upload function:
Steps
1. Import components and prepare Static script
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> <h1 id="Ajax-nbsp-文件上载">Ajax 文件上载</h1> <input type="file" id="file1"> <br> <input type="file" id="file2"> <br> <input type="button" id="upload" value="上载" > <p id="result"></p>
1. Bind events to buttons
$("upload").click(ajaxUpload);
2. Get the file
var file1 = $("#file1")[0].files[0]; var file2 = $("#file2")[0].files[0];
3. Create a form object in memory and add to Data transmitted by the server
//创建内存中的表单对象 var form = new FormData(); //向其中添加要传输的数据 form.append("userfile1", file1); form.append("userfile2", file2);
4.ajax() upload object
$.ajax({ url:'user/upload.do',//请求地址 data: form, //请求参数 type: 'POST', //请求类型 dataType: 'json',//服务器返回的数据类型 contentType: false,//没有设置任何内容类型头信息 processData: false, //见jQuery_api详解 success: function(obj){ //成功时回调函数,obj表示服务器返回的数据 if(obj.state==0){ $('#result').html("成功!"); } } });
5.Spring-MVC presentation layer
@RequestMapping("/upload.do") @ResponseBody public JsonResult upload( MultipartFile userfile1, MultipartFile userfile2) throws Exception{ //Spring MVC 中可以利用 MultipartFile //接收 上载的文件! 文件中的一切数据 //都可以从 MultipartFile 对象中找到 //获取上再是原始文件名 String file1 = userfile1.getOriginalFilename(); String file2 = userfile2.getOriginalFilename(); System.out.println(file1); System.out.println(file2); //保存文件的3种方法: //1. transferTo(目标文件) // 将文件直接保存到目标文件, 可以处理大文件 //2. userfile1.getBytes() 获取文件的全部数据 // 将文件全部读取到内存, 适合处理小文件!! //3. userfile1.getInputStream() // 获取上载文件的流, 适合处理大文件 //保存的目标文件夹: /home/soft01/demo File dir = new File("D:/demo"); dir.mkdir(); File f1 = new File(dir, file1); File f2 = new File(dir, file2); //第一种保存文件 //userfile1.transferTo(f1); //userfile2.transferTo(f2); //第三种 利用流复制数据 InputStream in1 = userfile1.getInputStream(); FileOutputStream out1 = new FileOutputStream(f1); int b; while((b=in1.read())!=-1){ out1.write(b); } in1.close(); out1.close(); InputStream in2 = userfile2.getInputStream(); FileOutputStream out2= new FileOutputStream(f2); byte[] buf= new byte[8*1024]; int n; while((n=in2.read(buf))!=-1){ out2.write(buf, 0, n); } in2.close(); out2.close(); return new JsonResult(true); }
The above is what I compiled for everyone, I hope it will be useful to everyone in the future helpful.
Related articles:
Based on apicloudAJAX request code collection (absolutely detailed)
Native JS implements Ajax cross-domain request flask Response content (graphic tutorial)
Ajax detection method for website hijacking
The above is the detailed content of Using the FormData object and Spring MVC to implement the Ajax file download function (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!

java实现定时任务Jdk自带的库中,有两种方式可以实现定时任务,一种是Timer,另一种是ScheduledThreadPoolExecutor。Timer+TimerTask创建一个Timer就创建了一个线程,可以用来调度TimerTask任务Timer有四个构造方法,可以指定Timer线程的名字以及是否设置为为守护线程。默认名字Timer-编号,默认不是守护线程。主要有三个比较重要的方法:cancel():终止任务调度,取消当前调度的所有任务,正在运行的任务不受影响purge():从任务队

一、@RequestParam注解对应的axios传参方法以下面的这段Springjava代码为例,接口使用POST协议,需要接受的参数分别是tsCode、indexCols、table。针对这个Spring的HTTP接口,axios该如何传参?有几种方法?我们来一一介绍。@PostMapping("/line")publicList

SpringBoot和SpringCloud都是SpringFramework的扩展,它们可以帮助开发人员更快地构建和部署微服务应用程序,但它们各自有不同的用途和功能。SpringBoot是一个快速构建Java应用的框架,使得开发人员可以更快地创建和部署基于Spring的应用程序。它提供了一个简单、易于理解的方式来构建独立的、可执行的Spring应用

随着技术的更新迭代,Java5.0开始支持注解。而作为java中的领军框架spring,自从更新了2.5版本之后也开始慢慢舍弃xml配置,更多使用注解来控制spring框架。

作为一名Java开发者,学习和使用Spring框架已经是一项必不可少的技能。而随着云计算和微服务的盛行,学习和使用SpringCloud成为了另一个必须要掌握的技能。SpringCloud是一个基于SpringBoot的用于快速构建分布式系统的开发工具集。它为开发者提供了一系列的组件,包括服务注册与发现、配置中心、负载均衡和断路器等,使得开发者在构建微

1.Spring项目的创建1.1创建Maven项目第一步,创建Maven项目,Spring也是基于Maven的。1.2添加spring依赖第二步,在Maven项目中添加Spring的支持(spring-context,spring-beans)在pom.xml文件添加依赖项。org.springframeworkspring-context5.2.3.RELEASEorg.springframeworkspring-beans5.2.3.RELEASE刷新等待加载完成。1.3创建启动类第三步,创

SpringBean的生命周期管理一、SpringBean的生命周期通过以下方式来指定Bean的初始化和销毁方法,当Bean为单例时,Bean归Spring容器管理,Spring容器关闭,就会调用Bean的销毁方法当Bean为多例时,Bean不归Spring容器管理,Spring容器关闭,不会调用Bean的销毁方法二、通过@Bean的参数(initMethod,destroyMethod)指定Bean的初始化和销毁方法1、项目结构2、PersonpublicclassPerson{publicP

spring设计模式有:1、依赖注入和控制反转;2、工厂模式;3、模板模式;4、观察者模式;5、装饰者模式;6、单例模式;7、策略模式和适配器模式等。详细介绍:1、依赖注入和控制反转: 这两个设计模式是Spring框架的核心。通过依赖注入,Spring负责管理和注入组件之间的依赖关系,降低了组件之间的耦合度。控制反转则是指将对象的创建和依赖关系的管理交给Spring容器等等。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
