package com.springmvc.controller;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
@Controller
@RequestMapping("/uploadFile/*")
public class uploadFileController {
/**
* 跳转到上传页面
* @return
*/
@RequestMapping("goUploadFilePage.do")
public String goUploadFilePage(HttpServletRequest req, HttpServletResponse res, ModelMap model) throws Exception{
return "uploadFile/uploadFile";
}
/**
* 页面上传按钮
* @param req
* @param res
* @return
* @throws Exception
*/
@RequestMapping("addUploadFile.do")
@ResponseBody
public String addUploadFile(HttpServletRequest req, HttpServletResponse res) throws Exception{
return uploadImage(req);
}
/**
* 上传图片方法
* @param req
* @return
* @throws Exception
*/
public String uploadImage(HttpServletRequest req) throws Exception{
Map<String, Object> map = new HashMap<String, Object>();
String path = req.getSession().getServletContext().getRealPath("/");//本项目的根目录
String fileChannel = req.getParameter("fileChannel");
System.out.println("------图片存储路径------fileChannel = "+fileChannel);
String[] channels = null;
if(fileChannel != null && !fileChannel.equals("")){
channels = fileChannel.split("/");
}
//转型为MultipartHttpRequest(重点的所在)
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req;
//获得第1张图片(根据前台的name名称得到上传的文件)
MultipartFile imgFile1 = multipartRequest.getFile("imgFile");
//定义一个数组,用于保存可上传的文件类型
List fileTypes = new ArrayList();
fileTypes.add("jpg");
fileTypes.add("jpeg");
fileTypes.add("bmp");
fileTypes.add("gif");
//保存第一张图片
if(!(imgFile1.getOriginalFilename() ==null || "".equals(imgFile1.getOriginalFilename()))) {
map = this.getFile(imgFile1, channels, path, fileTypes);
}
fileChannel = fileChannel + "/" + map.get("fileName").toString();
System.out.println("------fileName="+map.get("fileName").toString());
System.out.println("------fileChannel="+fileChannel);
return fileChannel;
}
/**
* 这个方法,主要是用来检测上传的文件是否属于允许上传的类型范围内,及根据传入的路径名是否存在。
* 不存在自动创建文件夹和文件名,返回的File文件和创建的新的文件名。
* @param imgFile 从页面中读取到的文件
* @param channels 各级目录组成的数组
* @param fileTypes 允许的文件扩展名集合
* @return
*/
private Map<String, Object> getFile(MultipartFile imgFile, String[] channels, String path, List fileTypes) {
Map<String, Object> map = new HashMap<String, Object>();
String fileName = imgFile.getOriginalFilename();
Date date= new Date();
long time = date.getTime();
System.out.println("-------time="+time);
//获取上传文件类型的扩展名,先得到.的位置,再截取从.的下一个位置到文件的最后,最后得到扩展名
String ext = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
//对扩展名进行小写转换
ext = ext.toLowerCase();
File file = null;
File folder = null;
String channelStr = "";
String newFileName = time + "." + ext;
if(fileTypes.contains(ext)) {//如果扩展名属于允许上传的类型,则创建文件
if(channels != null){
for(int i = 0; i < channels.length; i++){
channels[i] = channels[i].replaceAll(" ", "").replaceAll(" ", "");//替换半角空格,替换全角空格
folder = new File(path + channelStr, channels[i]);//每级文件夹
System.out.println("path = "+path + channelStr);
if(!folder.exists()){//如果这一级不存在,则创建这一级文件夹
folder.mkdir();
}
channelStr = channelStr + channels[i] + "\\";
}
file = new File(folder,newFileName); //创建完所有文件夹后,再合建文件
}
try {
imgFile.transferTo(file);//保存上传的文件
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
map.put("file", file);
map.put("fileName", newFileName);
return map;
}
}