pom.xml依赖
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
jsp页面
<%@ page contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%><html>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″>
<%@taglib uri="http://java.sun/jsp/jstl/core" prefix="c" %>
<body>
<h2>Hello</h2>
<!--enctype="multipart/form-data"支持把部分文件上传包裹图片text文档,excel等-->
<form method="post" action="fileUpload.do" name="fileupload" enctype="multipart/form-data">
姓名: <input type="file" name="fileupload"></br>
姓名: <input type="file" name="fileupload"></br>
<button type="submit">提交</button>
</form>
<a href="download?fileName=工作簿1.xlsx">下载</a>
<button onclick="download()">下载附件</button>
<script>
function download() {## 标题
window.location.href="download?fileName=工作簿1.xlsx"
}
</script>
Controller类
package com.wcf.controller;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by ChaoFeng Cotter on 2020/8/3.
*/
@Controller
public class FileUploadController extends HttpServlet {
Logger logger= LoggerFactory.getLogger(FileUploadController.class);
//文件上传
@RequestMapping("/fileUpload.do")
//MultipartFile 变量名必须与jsp页面input标签的name属性之相同才可以
public void fileUpload(HttpServletRequest request,MultipartFile fileupload) throws Exception {
SimpleDateFormat dateFormat=new SimpleDateFormat("yyyyMMddHHmm");
String now=dateFormat.format(new Date());
//String path=request.getSession().getServletContext().getRealPath("WEB-INF/upload/");
String path="D:";
String fileName=fileupload.getOriginalFilename();
String name="seilver"+now+fileName.substring(fileName.lastIndexOf("."));
File file1=new File(path,name);
if (!file1.exists()){
file1.mkdir();
}
fileupload.transferTo(file1);
}
@RequestMapping("download")
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, UnsupportedEncodingException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.addHeader("content-Type", "application/octet-stream");//设置下载的文件的MIME类型
String fileName = request.getParameter("fileName");
String path = "D:";//设置要下载的文件在服务器中的目录
/**
*解析浏览器类型,设置不同的“content-Disposition”消息头来解决中文乱码问题
*/
String agent = request.getHeader("User-Agent").toLowerCase();
String contentDisposition = "attachement;filename="+fileName;
System.out.println(agent);
if(agent.contains("edge")) {
System.out.println("edge");
contentDisposition = "attachment;filename="+URLEncoder.encode(fileName,"UTF-8");
}else if(agent.contains("firefox")||agent.contains("opr")||agent.contains("chrome")) {
contentDisposition = "attachment;filename==?UTF-8?B?"+new String(Base64.encodeBase64(fileName.getBytes("UTF-8")))+"?=";
}
response.addHeader("Content-Disposition",contentDisposition);
//InputStream in = getServletContext().getResourceAsStream(path+fileName);//此种方式只能下载项目根目录中的文件
/**
* 通过IO操作将文件输出给用户
*/
InputStream in = new FileInputStream(path+fileName);
ServletOutputStream out = response.getOutputStream();
/*byte[] bs = new byte[10];
int len = -1;
while((len=in.read(bs))!=-1) {
out.write(bs,0,bs.length);
}
out.close();
in.close();*/
IOUtils.copy(in,out);
}
}
更多推荐
maven项目使用commonsfileupload实现文件的上传和下载
发布评论