<form id="uploadForm" enctype="multipart/form-data"> <span style="margin-right: 65px;">词典名称:</span> <select class="easyui-combobox" style="width: 180px;" id="batchAddPanelDictConditionSelect" name="dictId"> </select> <div style="margin-bottom: 20px"> <input class="easyui-filebox" label="Excel文件:" name="excelFile" id="excelFileId" labelPosition="left" data-options="prompt:'Choose a excel file...'" style="width: 100%"> </div> </form>
function commit() { var dictId = $('#batchAddPanelDictConditionSelect').combobox('getValue'); if (dictId == '') { showWarningMessage('请选择词典!'); return; } var excelFile = $('#excelFileId').textbox('getValue'); if (excelFile == '') { showWarningMessage('请选择需要上传的excel!'); return; } var regex = /^.+?(\.xlsx)|(\.xls)$/; if (!regex.test(excelFile)) { showWarningMessage('上传的文件必须是excel!'); return; } var formData = new FormData($('#uploadForm')[0]); var options = { url: 'entry/batchAddEntries', type: 'POST', dataType: 'json', data: formData, // 告诉jQuery不要去处理发送的数据 processData: false, // 告诉jQuery不要去设置Content-Type请求头 contentType: false, beforeSend: function () { console.log("正在进行,请稍候"); }, success: function (rs) { }, error: function (rs) { } }; $.ajax(options); };
@RestController @RequestMapping(value = { "/entry" }) public class EntryController { private static final Logger LOGGER = LoggerFactory.getLogger(EntryController.class); /** * excel文件后缀形式1 */ private static final String EXCEL_SUFFIX_01 = ".xls"; /** * excel文件后缀形式2 */ private static final String EXCEL_SUFFIX_02 = ".xlsx"; @RequestMapping(value = { "/batchAddEntries" }, method = { RequestMethod.POST }) public Result<Void> batchAddEntries(@RequestParam("dictId") Integer dictId, @RequestParam("excelFile") MultipartFile excelFile) { LOGGER.debug("start to batchAddEntries. dictId:{},excelFile:{}", dictId, excelFile); checkNotNull(dictId, "dictId is null"); checkNotNull(excelFile, "excelFile is null"); String lowcaseName = excelFile.getName().toLowerCase(); checkArgument(lowcaseName.endsWith(EXCEL_SUFFIX_01) || lowcaseName.endsWith(EXCEL_SUFFIX_02), "upload file is not a excel file"); // TODO upload excel return null; } @RequestMapping(value = { "/download" }, method = { RequestMethod.GET }) public ResponseEntity<byte[]> download(HttpServletRequest request, @RequestParam("fileName") String fileName) throws Exception { // 下载文件路径 String path = request.getServletContext().getRealPath("/static/template/"); File file = new File(path + File.separator + fileName); HttpHeaders headers = new HttpHeaders(); // 通知浏览器以attachment(下载方式)打开图片 headers.setContentDispositionFormData("attachment", fileName); // application/octet-stream : 二进制流数据(最常见的文件下载)。 headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); } }
参考地址: http://blog.csdn.net/qian_ch/article/details/69258465
参考代码:单文件上传、多文件上传
import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.util.List; @RestController @RequestMapping("/fileserver") public class FileServerResource { @RequestMapping(path = "/singlefileupload/", method = RequestMethod.POST) public ResponseEntity<String> processFile(@RequestParam("file") MultipartFile file) throws IOException { byte[] bytes = file.getBytes(); System.out.println("File Name: " + file.getOriginalFilename()); System.out.println("File Content Type: " + file.getContentType()); System.out.println("File Content:\n" + new String(bytes)); return (new ResponseEntity<>("Successful", null, HttpStatus.OK)); } @RequestMapping(path = "/multiplefileupload/", method = RequestMethod.POST) public ResponseEntity<String> processFile(@RequestParam("files") List<MultipartFile> files) throws IOException { for (MultipartFile file : files) { byte[] bytes = file.getBytes(); System.out.println("File Name: " + file.getOriginalFilename()); System.out.println("File Content Type: " + file.getContentType()); System.out.println("File Content:\n" + new String(bytes)); } return (new ResponseEntity<>("Successful", null, HttpStatus.OK)); } }
相关推荐
5. **文件下载**: 下载文件时,Controller中需要返回一个链接,该链接指向服务器上待下载的文件。可以使用`@ResponseBody`和`StreamingResponseBody`来实现动态生成HTTP响应流。 ```java @RequestMapping("/...
基于springmvc实现文件上传下载 基于AOP的日志功能基于springmvc实现文件上传下载 基于AOP的日志功能基于springmvc实现文件上传下载 基于AOP的日志功能基于springmvc实现文件上传下载 基于AOP的日志功能基于...
springMVC 上传文件方式springMVC 上传文件方式springMVC 上传文件方式
本文将详细讲解如何实现SpringMVC中的单文件上传、多文件上传、文件列表显示以及文件下载。 首先,我们需要理解SpringMVC处理文件上传的基本原理。在SpringMVC中,文件上传通常涉及到`CommonsMultipartResolver`...
至于文件下载,可以通过返回一个带有`Content-Disposition`头的HTTP响应来实现。在Controller中创建一个方法,根据文件名或ID从服务器获取文件,并设置响应头: ```java @GetMapping("/download/{filename}") ...
在Spring MVC框架中,文件上传是一项常见的功能,用于接收客户端发送的文件数据。Vue.js作为一个前端框架,可以很好地与Spring MVC结合,实现用户界面的交互和文件上传的处理。在这个项目中,我们将深入探讨如何使用...
在这个“SpringMVC文件上传,多文件上传实例”中,我们将深入探讨如何在SpringMVC环境中实现文件上传功能,包括单个文件上传以及多个文件的批量上传。 1. **文件上传原理**: 文件上传是通过HTTP协议的POST请求来...
文件下载通常涉及HTTP响应的设置。在控制器方法中,通过`HttpServletResponse`对象来设置响应头,如`Content-Disposition`用于指定文件名,`Content-Type`用于指定文件类型。然后,使用`response.getOutputStream()....
springMvc 文件上传,springMvc 支持单文件和多文件上传,
在本文中,我们将深入探讨如何在Spring MVC框架中实现AJAX文件上传,以及通过表单提交方式上传文件。这两种方法都是在Web应用中处理用户上传文件的常见方式,特别是当需要在后台处理文件且不刷新整个页面时,AJAX...
对于文件下载,无论是SpringMVC还是SSH,都需要考虑以下几点: 1. **安全控制**:确保只有授权的用户才能访问和下载特定文件。 2. **文件路径安全**:避免路径遍历攻击,只允许访问指定的文件夹。 3. **流式传输**:...
jar包包含: com.springsource.org.apache.commons.fileupload-1.2.0.jar com.springsource.org.apache.commons.io-1.4.0.jar
在使用springMVC进行系统实现时,springMVC默认的解析器里面是没有加入对文件上传的解析的,这可以方便我们实现自己的文件上传。但如果你想使用springMVC对文件上传的解析器来处理文件上传的时候就需要在spring的...
**二、SpringMVC文件下载** 文件下载主要是通过创建一个控制器方法,生成一个包含文件内容的HTTP响应。这里的关键是设置响应头的`Content-Disposition`属性,指示浏览器如何处理响应体。 1. **创建下载链接**:在...
这个"springMVC多文件上传demo"是一个实例,它展示了如何在Spring MVC应用中实现这个功能。下面将详细介绍相关知识点。 1. **Spring MVC概述** Spring MVC是Spring框架的一部分,它提供了一个用于构建Web应用程序...
在Spring MVC框架中,文件上传是一项常见的功能,用于接收客户端发送的文件数据。这里我们将详细介绍四种不同的文件上传方法,每种方法都有其适用场景和特点。 1. **使用`@RequestParam`注解** 这是最基础的文件...
SpringMVC 上传文件详解 SpringMVC 框架中上传文件是非常常见的操作,今天我们来详细讲解 SpringMVC 中的文件上传过程。 文件上传的必要条件 在 SpringMVC 中,文件上传需要满足以下几个条件: 1. 表单的 ...
3. **文件下载**: - Spring MVC提供`ResponseEntity`和`HttpHeaders`来创建一个HTTP响应,可以设置响应头`Content-Disposition`为`attachment`,触发浏览器下载行为。 - 服务器端获取文件后,可以通过`Response...
在SpringMVC框架中,文件的上传和下载是常见的功能需求,主要用于处理用户的数据交互,例如用户上传图片、文档等,或者系统提供文件下载服务。本文将深入探讨如何使用SpringMVC来实现这一功能。 首先,我们需要理解...