如题,本篇我们介绍下springmvc文件上传、下载。
application.properties
#multipart upload 文件上传 #限制一次上传的单个文件的大小 spring.http.multipart.maxFileSize=10Mb #限制一次上传的所有文件的总大小 spring.http.multipart.maxRequestSize=10Mbupload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'upload.jsp' starting page</title> <meta charset="utf-8"/> </head> <body> <form action="${pageContext.request.contextPath }/file_upload" method="post" enctype="multipart/form-data" target="_blank"> fileType:<input type="text" name="fileType" id="fileType" /> <br/> file:<input type="file" name="file" id="file"> <button type="submit">提交上传</button> </form> </body> </html>FileController.java
package com.tingcream.springWeb.controller; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.util.Map; 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.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import com.tingcream.springWeb.util.AjaxUtil; @Controller public class FileController { /** * 文件上传 * @param fileType 文件类型 * @return */ @ResponseBody @RequestMapping("/file_upload") public Object file_upload(String fileType,HttpServletRequest request,HttpServletResponse response){ MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request; Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); MultipartFile mulFile = fileMap.get("file"); if(mulFile == null ||mulFile.getSize()<=0 ){ // -1 return AjaxUtil.messageMap(400, "抱歉,上传文件不能为空"); } String orgFileName= mulFile.getOriginalFilename(); long fileSize =mulFile.getSize(); String name = mulFile.getName();//file input域的参数名 , file System.out.println("文件参数名:"+name); System.out.println("原始文件名:"+orgFileName); System.out.println("文件大小:"+fileSize); System.out.println("fileType(业务参数):"+fileType); String suffix=orgFileName.substring(orgFileName.lastIndexOf("."));//文件后缀 String newFileName = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase()+suffix; String destPath="f:/13/"+newFileName; try { mulFile.transferTo(new File(destPath) ); return AjaxUtil.messageMap(200, "上传文件成功","destPath",destPath); } catch (Exception e) { e.printStackTrace(); } return AjaxUtil.messageMap(400, "抱歉,文件上传失败"); } /** * 文件下载 * @throws Exception */ @ResponseBody @RequestMapping("/file_download") public void file_download(HttpServletRequest request,HttpServletResponse response) throws Exception{ //String filePath ="f:/13/postman-4.1.2.rar";//ok String filePath ="f:/13/postman-安装包.rar";//中文名称文件 ok File file =new File(filePath); //response.setContentType(""); response.setHeader("Content-Disposition","filename=" + new String(file.getName().getBytes("gb2312"), "iso8859-1")); BufferedInputStream bufin=new BufferedInputStream(new FileInputStream(file)); int len=0; byte[] buf=new byte[1024*4]; while( (len=bufin.read(buf))>0){ response.getOutputStream().write(buf, 0, len); } bufin.close(); } }AjaxUtil.java
package com.tingcream.springWeb.util; import java.util.HashMap; import java.util.Map; public class AjaxUtil { //得到一个map public static Map<String,Object> getMap(){ return new HashMap<String,Object>(); } //返回json数据 状态 public static Map<String,Object> messageMap(int status){ Map<String,Object> map=new HashMap<String,Object>(); map.put("status", status); return map; } //返回json数据 状态、消息 public static Map<String,Object> messageMap(int status,String message){ Map<String,Object> map=new HashMap<String,Object>(); map.put("status", status); map.put("message", message); return map; } //返回json数据 状态、消息 和一个参数 public static Map<String,Object> messageMap(int status,String message, String paramName,Object paramValue){ Map<String,Object> map=new HashMap<String,Object>(); map.put("status", status); map.put("message", message); map.put(paramName, paramValue); return map; } //返回json数据 状态、消息 和多个参数 public static Map<String,Object> messageMap(int status,String message, String[] paramNames,Object[] paramValues){ Map<String,Object> map=new HashMap<String,Object>(); map.put("status", status); map.put("message", message); if(paramNames!=null&¶mNames.length>0){ for(int i=0;i<paramNames.length;i++){ map.put(paramNames[i], paramValues[i]); } } return map; } }
download.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'download.jsp' starting page</title> <meta charset="utf-8"/> </head> <body> <a href="${pageContext.request.contextPath }/file_download">下载文件</a> </body> </html>
FileController.java 、AjaxUtil.java 同上(文件上传中的) 。
ok!!!
Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1