需要引入文件上傳的依賴
<!--引入文件上傳依賴-->
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
然後在springmvc的配置文件中加入文件上傳的配置
<!--配置文件上傳-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!--上傳時的編碼-->
<property name="defaultEncoding" value="UTF-8"></property><!--上傳時的文件大小-->
<property name="maxUploadSize" value="2097152"></property>
</bean>
文件上傳
頁面上
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>文件上傳</title>
</head>
<body>
<h3>文件上傳</h3>
<!--必須是post的提交-->
<!--設置enctype的格式-->
<form action="/filesupload.action" method="post" enctype="multipart/form-data">
<input type="file" name="multipartFile">
<input type="submit" value="文件上傳">
</form>
</body>
</html>
控制器中Controller
//文件上傳
@RequestMapping("filesupload")
public String filesupload(MultipartFile multipartFile,HttpServletRequest request){
//MultipartFile是通過apache的文件上傳,用他接收文件
//獲取到文件的文件名稱
//System.out.println(multipartFile.getOriginalFilename());
//1.先獲取到upload文件夾的絕對路徑
String path=request.getRealPath("/upload");
//2.給獲取到的文件處理
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmssSSSS");
//通過時間處理文件的前綴
String fileName=sdf.format(new Date());
//獲取到後綴
String exit=multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
//3.創建一個文件對象
File file=new File(path+"/"+fileName+exit);
//文件的複製
try {
FileUtils.copyInputStreamToFile(multipartFile.getInputStream(),file);
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:/filedownload.action";
}
//查詢出upload文件夾中所有的文件
@RequestMapping("filedownload")
public String filedownload(HttpServletRequest request){
//獲取upload裏面所有的文件
String path=request.getRealPath("/upload");
File file=new File(path);
File[] files=file.listFiles();
for (File f : files) {
System.out.println(f.getName());
}
request.setAttribute("files",files);
System.out.println("成功");
return "FileDownload";
}
文件下載
頁面中
<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>文件下載</title>
</head>
<body>
<h3>文件下載</h3>
<!--獲取了upload文件下的所有文件-->
<c:forEach items="${files}" var="f">
${f.name}<a href="fileDownLoad.action?fileName=${f.name}">點擊下載</a><br/>
</c:forEach>
</body>
</html>
控制器中
//文件下載
@RequestMapping("fileDownLoad")
public ResponseEntity<byte[]> fileDownLoad(String fileName,HttpServletRequest request){//返回的是一個響應實體類,裏面的類型是字節數組
//內容
String path=request.getRealPath("/upload");
//獲取需要下載的文件
File file=new File(path+"/"+fileName);
//將要下載的文件轉換成字節數組
byte[] b=null;
try {
InputStream is=new FileInputStream(file);//創建字節流
b=new byte[is.available()];//獲取字節流中的字節數量
is.read(b);//讀取
} catch (Exception e) {
e.printStackTrace();
}
//頭部
//創建頭部的響應信息,告訴網頁需要做什麼操作
HttpHeaders headers=new HttpHeaders();
//設置要下載的文件顯示的名稱
headers.setContentDispositionFormData("attchement",file.getName());
//設置下載的時候刪除的數據類型(以二進制的字節流的形式輸出數據)
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//(內容,頭,狀態)
ResponseEntity<byte[]> entity=new ResponseEntity<>(b,headers, HttpStatus.OK);
System.out.println("OK");
return entity;
}