http://www.javashuo.com/article/p-yptxsioj-kt.htmljavascript
在项目中创建一个文件夹,用来存储上传的文件,通常存放的在项目静态文件的根目录下html
在pom.xml文件中添加以下代码:java
<!-- 文件上传下载 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency>
在applicationContext.xml文件中添加以下代码:jquery
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8"></property> <property name="maxUploadSize" value="5242440"></property> <!--设置最大支持的上传文件大小 B--> </bean>
#index.jspweb
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>文件上传下载</title> </head> <body> <form action="${pageContext.request.contextPath }/file/upload.action" method="post" enctype="multipart/form-data"> 选择文件:<input type="file" name="file" width="120px" accept=".docx"> <!-- accept用来控制上传的文件类型,能够为.docx/.pdf/.doc/.jpg…… --> <input type="submit" value="上传"> </form> <form action="${pageContext.request.contextPath }/file/down.action" method="get"> <input type="submit" value="下载"> </form> </body> </html>
#index.jspajax
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <head> <base href="<%=basePath%>"> <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.min.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-form.js"></script> <title>My JSP 'index.jsp' starting page</title> <script type="text/javascript"> $(function(){ $("#submit").click(function(){ var file = $('#indicatorAddExcelForm')[0]; var formData = new FormData(file); alert(formData) $.ajax({ type:"post", url:"/Test/upload.action", data: formData, cache: false,//文件不设置缓存 processData: false,//数据不被转换为字符串 contentType: false,//上传文件时使用,避免 JQuery 对其操做 success:function(da){ alert(da); } }) }) }) </script> </head> <body> <form id="indicatorAddExcelForm" enctype="multipart/form-data"> <input type="file" class="easyui-validatebox" id="file" name="file"> <input type="button" id="submit"> </form> </body>
#FileController.javaspring
package com.ws.controller; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; /** * Maven环境下SSM框架实现上传与下载 * @author 文升 * */ @Controller @RequestMapping("file") public class FileController { @RequestMapping(value="upload",method=RequestMethod.POST) @ResponseBody public String upload(MultipartFile file,HttpServletRequest request) throws IOException{ String path = request.getSession().getServletContext().getRealPath("upload"); String fileName = file.getOriginalFilename(); File dir = new File(path,fileName); if(!dir.exists()){ dir.mkdirs(); } file.transferTo(dir); return fileName; } @RequestMapping("down") public void down(HttpServletRequest request,HttpServletResponse response) throws Exception{ String fileName = request.getSession().getServletContext().getRealPath("upload")+"/下载.docx"; InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName))); String filename = "下载.docx"; filename = URLEncoder.encode(filename,"UTF-8"); response.addHeader("Content-Disposition", "attachment;filename=" + filename); response.setContentType("multipart/form-data"); BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream()); int len = 0; while((len = bis.read()) != -1){ out.write(len); out.flush(); } out.close(); } }
import java.io.File; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import com.neu.util.ReadExcel; @Controller @RequestMapping("Test") public class TextController { @RequestMapping(value="upload",method=RequestMethod.POST) @ResponseBody public String upload(MultipartFile file,HttpServletRequest request) throws IOException{ String path = request.getSession().getServletContext().getRealPath("upload"); String fileName = file.getOriginalFilename(); File dir = new File(path,fileName); if(!dir.exists()){ dir.mkdirs(); } file.transferTo(dir); ReadExcel readExcel = new ReadExcel(); try { System.out.println(path+"\\"+fileName); readExcel.excel(path+"\\"+fileName); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return fileName; } }
连接:https://pan.baidu.com/s/1QaMk-_gGiYvuuVLJ672AmQ 提取码:5fw7