ssm上传文件获取路径_SSM实现图片上传下载功能
0. 基本步驟框架搭建
實現(xiàn)帶圖片上傳的注冊功能
實現(xiàn)下載功能
1. 框架搭建
Step1 創(chuàng)建開發(fā)項目
創(chuàng)建基礎MVC包結(jié)構(gòu):controller、service、mapper、pojo
Step2 導入jar包
導入SpringMVC的jar、Spring的jar、MyBatis的jar、上傳文件的jar
Step3 配置文件配置applicationcontext.xml文件
開啟service包注解掃描
配置數(shù)據(jù)庫信息properties文件
配置數(shù)據(jù)源bean
配置SqlSession工廠bean
配置mapper掃描bean,記得把工廠依賴設置為屬性注入
配置事務管理
配置springmvc.xml文件
配置controller包注解掃描
配置springmvc 注解驅(qū)動
配置靜態(tài)資源放行
配置文件上傳資源解析bean
配置web.xml文件
配置spring配置文件加載路徑
配置spring容器資源加載監(jiān)聽器
配置SpringMVC的DispatcherServlet
配置編碼過濾器
2. 實現(xiàn)帶圖片上傳的注冊功能
基本思路:
Step1 先實現(xiàn)圖片的異步上傳,反顯圖片,獲取圖片上傳后的存儲路徑,
Step2 將注冊的數(shù)據(jù)、頭像圖片的存儲路徑一并提交至后臺完成注冊。
2.1 JS實現(xiàn)圖片異步上傳
實現(xiàn)思路:
Step1 首先引入jQuery文件
Step2 添加注冊按鈕單擊事件
Step3 獲取上傳文件數(shù)據(jù)、利用FormData對象封裝數(shù)據(jù)
Step4 ajax方式提交后臺,注意processData設置為false、contentType設置為false
Step5 上傳成功后回顯圖片,將圖片路徑填至隱藏域
具體JS實現(xiàn)圖片上傳方法:$(function(
$("#uploadBtn").click(function(){
// 獲取上傳文件
var uploadPhoto = $("#upload")[0].files[0];
// 利用FormDate對象封裝文件數(shù)據(jù)
var formdata = new FormData();
formdata.append("uploadPhoto",uploadPhoto);
$.ajax({
url:'regUpload',
type:'post',
processData:false,
contentType:false,//設置false,將請求數(shù)據(jù)類型設置為multipart/form-data
data:formdata,
success:function(data){
if(data.result){
alert("上傳成功!");
$("#imageSpan").html("");
$("#imgPath").val(data.msg);
}else{
alert("上傳失敗!原因:"+data.msg);
}
}
});
});
));
2.2 后臺處理上傳的圖片
實現(xiàn)思路:
1、獲取文件在服務器存儲中的實際路徑
2、獲取文件原始文件名,隨機生成文件名
3、實際路徑拼接隨機文件名,將文件存儲至目標路徑
4、存儲文件的原始名、隨機名、文件類型至數(shù)據(jù)庫
5、返回上傳結(jié)果、存儲路徑+文件名
具體代碼如下:@RequestMapping("regUpload")
@ResponseBody
public Result regUpload(MultipartFile uploadPhoto, HttpServletRequest request) throws IOException {
// 業(yè)務處理
// 1、獲取文件在服務器存儲中的實際路徑
String realPath = request.getServletContext().getRealPath("/uploadImage/");
File pathFile = new File(realPath);
if(!pathFile.exists()){
pathFile.mkdirs();
}
// 2、獲取文件原始文件名,隨機生成文件名
String originalFilename = uploadPhoto.getOriginalFilename();
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
String fileName = UUID.randomUUID()+suffix;
// 3、實際路徑拼接隨機文件名,將文件存儲至目標路徑
uploadPhoto.transferTo(new File(realPath+fileName));
// 4、存儲文件的原始名、隨機名、文件類型至數(shù)據(jù)庫
String contentType = uploadPhoto.getContentType();
PhotoImage photoImage = new PhotoImage("/uploadImage/"+fileName, originalFilename, fileName, contentType );
System.out.println("============================");
int flag = 0 ;
try{
flag =photoImageService.insertImage(photoImage);
}catch (Exception e){
e.printStackTrace();
}
if(flag==1){
// 5、返回上傳結(jié)果、存儲路徑+文件名
Result result = new Result(true,"uploadImage/"+fileName);
return result;
}else{
return new Result(false,"圖片存儲失敗!");
}
}
2.3 html頁面實現(xiàn)
| 用戶名: | |
| 密碼: | |
| 上傳文件: | |
2.4 實現(xiàn)用戶信息注冊功能
實現(xiàn)思路:
Step1 用
標簽收集頁面所有數(shù)據(jù)Step2 submit按鈕提交至后臺
Step3 通過service、mapper存儲
具體實現(xiàn)代碼:@RequestMapping("regUser")
public String regUser(User user){
// 業(yè)務處理
int flag = userService.insertUser(user);
// 請求轉(zhuǎn)發(fā)
//return "forward:register.jsp";
return "register";
}
3. 實現(xiàn)圖片下載功能
實現(xiàn)思路:
Step1 前臺用超鏈接觸發(fā)下載功能,將要下載的文件名作為請求參數(shù)帶上
Step2 后臺接收請求,先設置響應頭,表明為下載請求:`response.setHeader("Content-Disposition", "attachment;filename="+filenName);`
Step3 獲取文件的在硬盤上的絕對路徑
Step4 利用FileUtils將文件轉(zhuǎn)成byte數(shù)組
Step5 從相應對象中獲取輸出流,將byte數(shù)組寫出
Step6 清除輸出流的緩存、關閉輸出流
具體代碼:@RequestMapping("downloadFile")
public void downloadFile(String filename,HttpServletRequest req, HttpServletResponse resp) throws IOException {
// Step2 后臺接收請求,先設置響應頭,表明為下載請求
resp.setHeader("Content-Disposition", "attachment;filename="+filename);
// Step3 獲取文件的在硬盤上的絕對路徑
String realPath = req.getServletContext().getRealPath("/uploadImage/");
// Step4 利用FileUtils將文件轉(zhuǎn)成byte數(shù)組
File file = new File(realPath,filename);
byte[] bytes = FileUtils.readFileToByteArray(file);
// Step5 從相應對象中獲取輸出流,將byte數(shù)組寫出
ServletOutputStream os = resp.getOutputStream();
os.write(bytes);
// Step6 清除輸出流的緩存、關閉輸出流
os.flush();
os.close();
}
總結(jié)
以上是生活随笔為你收集整理的ssm上传文件获取路径_SSM实现图片上传下载功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机二级access模考软件_计算机二
- 下一篇: isnull mysql_mysql i