上傳指用戶把本地文件傳到后端,下載指從后端數(shù)據(jù)庫等地方把文件呈現(xiàn)到前端,略微修改可下載到本地。以圖片為例,使用themyleaf模板引擎。
一.圖片上傳
1. pom.xml添加依賴
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version></dependency>
2. 前端上傳
<!--enctype參數(shù)一定要-->
<form enctype="multipart/form-data" method="post" action="/user/">
<!--多文件-->
<input type="file" name="file" multiple="multiple"/>
<input type="submit" value="提 交"/>
</form>
3. 后端保存
//@RequestParam 可選
public void g( MultipartFile[] file){
for(MultipartFile file1:file) {
System.out.println(file);
if (!file1.isEmpty()) {
//圖片尾綴,用于保存文件又要改文件名時(shí),保留格式
int dotPos = file1.getOriginalFilename().lastIndexOf(".");
String fileExt = file1.getOriginalFilename().substring(dotPos + 1).toLowerCase();
try {
//這里將上傳得到的文件保存至根目錄下的src目錄下
FileUtils.copyInputStreamToFile(file1.getInputStream(), new File("./src/", "我的圖片" + i++ + "." + fileExt));
} catch (IOException e) {
e.printStackTrace();
}
} else{
System.out.println(" no picture");
}
}
}
二.圖片下載
1. 后端傳圖片路徑參數(shù)。圖片在src/main/resources/static/ 文件夾下
@RequestMapping(value = {"/user/"}, method = {RequestMethod.POST})
public String g(Model model){
model.addAttribute("url", "5.jpg");
return "Photo";
}
2. 前端展示
<!DOCTYPE html><html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8"/>
<title>Photo</title>
</head>
<body>
<!--展示只需img那句,現(xiàn)在這樣可以下載到本地,但是后端還要寫一點(diǎn)代碼-->
<!--請求參數(shù)形式-->
<!--<a th:href="@{/downFile/(url=${url})}">-->
<!--路徑參數(shù)形式-->
<a th:href="@{/downFile/{url}(url=${url})}">
<!--資源是/url,才回找到-->
 Download</a>
</body>
</html>
3. 后端處理下載請求
1. 路徑參數(shù)形式
@RequestMapping("/downFile/{url}")
public void downFile(HttpServletRequest request,HttpServletResponse response,@PathVariable("url") String url) {
// 得到要下載的文件名
String fileName = "./src/main/resources/static/"+url+".jpg";
try {
fileName = new String(fileName.getBytes("iso8859-1"), "UTF-8");
// 得到要下載的文件
File file = new File(fileName);
// 如果文件不存在
if (!file.exists()) {
request.setAttribute("message", "您要下載的資源已被刪除??!");
System.out.println("您要下載的資源已被刪除?。?);
return;
}
// 處理文件名,后端傳過來的要處理,這里直接設(shè)置的
String realname = url+".jpg";
// 設(shè)置響應(yīng)頭,控制瀏覽器下載該文件
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));
// 讀取要下載的文件,保存到文件輸入流
FileInputStream in = new FileInputStream(fileName);
// 創(chuàng)建輸出流
OutputStream out = response.getOutputStream();
// 創(chuàng)建緩沖區(qū)
byte buffer[] = new byte[1024];
int len = 0;
// 循環(huán)將輸入流中的內(nèi)容讀取到緩沖區(qū)當(dāng)中
while ((len = in.read(buffer)) > 0) {
// 輸出緩沖區(qū)的內(nèi)容到瀏覽器,實(shí)現(xiàn)文件下載
out.write(buffer, 0, len);
}
// 關(guān)閉文件輸入流
in.close();
// 關(guān)閉輸出流
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
2.請求參數(shù)形式
@RequestMapping("/downFile/")
public void downFile(HttpServletRequest request,
HttpServletResponse response,
@RequestParam String url) {
//以下兩處不一樣,路徑參數(shù)下,spring默認(rèn)把最后一個(gè).后面的文件格式省略,所以需要手動(dòng)添加文件格式才能找到資源,而請求參數(shù)形式不會(huì)發(fā)生,其余的數(shù)據(jù)流讀寫代碼一樣
String fileName = "./src/main/resources/static"+"/"+url;
String realname = url;
}