- springMvc文件上傳創建文件夾的位置在項目裏面會導致部署的時候不小心丟失圖片
- 爲了防止發生此類事情,我們應該把保存文件的文件夾放在webapps外面
一: 控制器層
/**
* 這是跳轉到文件上傳的頁面
*/
@RequestMapping("toIframeUpload")
public String toIframeUpload(){
return "iframeUpload";
}
/**
* 這是文件上傳的後臺的方法
*/
@RequestMapping(value="uploadIframe")
public String uploadIframe(MultipartFile aa,HttpServletRequest request,Model model) throws Exception{
/**
* request.getRealPath("/") 獲得當前項目的全路徑名稱 D:\apache-tomcat-7.0.42\webapps\springmvc\
* request.getContextPath() 獲得當前的項目名 /springmvc
* realPath 得到webapps的全路徑 D:\apache-tomcat-7.0.42\webapps\
* separator不區分linux下和window下的/
*/
String realPath = request.getRealPath("/").substring(0, request.getRealPath("/").lastIndexOf(request.getContextPath().replace("/", "")));
File file = new File(realPath+File.separator+"upload"+File.separator);
//創建文件上傳的位置
if(!file.exists() ){
file.mkdirs();
}else{
System.out.println("文件夾已經存在");
}
int indexOf = aa.getOriginalFilename().lastIndexOf(".");
//獲得文件的後綴的名稱
String imageType = aa.getOriginalFilename().substring(indexOf,aa.getOriginalFilename().length());
String fileName = UUID.randomUUID().toString()+imageType;
aa.transferTo(new File(file,fileName));
model.addAttribute("path", "/upload/"+fileName);
System.out.println("/upload/"+fileName);
return "ok";
}
二:這是文件上傳的頁面
<%@ page language="java" import="java.util.*" pageEncoding="Utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>這是我的測試頁面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript">
function test(path){
var filePath2 = document.getElementById("#filePath");
filePath.value=path;
}
</script>
</head>
<body>
<!--
iframe:好處頭像上傳
注意:必須讓from表單的target 屬性的名字與 iframe的name名字保持一致
iframe所在的網頁稱之爲parent網頁
iframe上傳所調用的controller返回的邏輯頁面稱之爲子頁面
ifarme會把上傳的結果頁面包含到iframe中
-->
<iframe width="500px" name="fileUpload" height="500px"></iframe>
<form action="${pageContext.request.contextPath}/upload/uploadIframe" target="fileUpload" method="post" enctype="multipart/form-data">
<!-- 真正項目根據實際情況存儲文件的路徑名稱 -->
文件路徑:<input type="text" name="path" id="filePath">
<input type="file" name="aa">
<input type="submit" value="上傳"/>
</form>
</body>
</html>
三:這是控制器返回的iframe頁面
<%@ page language="java" import="java.util.*" pageEncoding="Utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>iframe</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
parent.test('${path}');
</script>
</head>
<body>
<img alt="" src="${path}">
</body>
</html>