上傳的時候 保存屬性html
<%@ 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>
<style>
.expa{ width:120px; height:80px; border:1px solid #F00}
.expb{ width:200px; height:120px; border:1px solid #00F}
</style>
</head>
<body>
<h3>文件下載</h3>
<div>
<img alt="" src="${requestScope.user.filepath}" width="400px" height="300px">
</div>
<a href="${pageContext.request.contextPath }/download.action?filename=${requestScope.user.realname}">
${requestScope.user.image.originalFilename }
</a>
</body>
</html>java
/**
* 6. 下載文件
* @param request
* @param filename
* @param model
* @return
* @throws Exception
*/
@RequestMapping(value="/download.action")
public ResponseEntity<byte[]> download(HttpServletRequest request, @RequestParam("filename") String filename, Model model)throws Exception {
//下載文件路徑
// String path = request.getServletContext().getRealPath("/images/");
String temp =request.getServletContext().getContextPath();
String path = "D:/workspace"+temp+"/src/main/webapp/img";
File file = new File(path + File.separator + filename);
HttpHeaders headers = new HttpHeaders();
//下載顯示的文件名,解決中文名稱亂碼問題
String downloadFielName = new String(filename.getBytes("UTF-8"),"iso-8859-1");
//通知瀏覽器以attachment(下載方式)打開圖片
headers.setContentDispositionFormData("attachment", downloadFielName);
//application/octet-stream : 二進制流數據(最多見的文件下載)。
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);// MediaType 用spring框架的
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), // FileUtils 選擇 org.apache.commons.io.FileUtils
headers, HttpStatus.CREATED);
}web