package com.wkrj.business.ntpd.until;
import java.io.File;
import java.text.SimpleDateFormat;
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 org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
public class imgUploadUntil {
public static List<Map<String, String>> lkh_uploadFile(HttpServletRequest request,String dirname){
ArrayList<Map<String, String>> newlist=new ArrayList<Map<String,String>>();
//文件上传
MultipartHttpServletRequest multipartRequest=(MultipartHttpServletRequest) request;
try {
multipartRequest.setCharacterEncoding("UTF-8");
//文件路径
String path = dirname;
String realPath = multipartRequest.getSession().getServletContext().getRealPath("/") + "/" + path;
File file = new File(realPath);
if (!file.exists()) {//创建目录
file.mkdirs();
}
String fileName = "";
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
String fileComponentName=entity.getKey();//获取组件名
List<MultipartFile> list2 = multipartRequest.getFiles(fileComponentName);
for (MultipartFile mf : list2) {
fileName = mf.getOriginalFilename();// 获取文件名
if(fileName==null||"".equals(fileName)||""==fileName){//空文件,直接返回
return null;
}
String onlyfileName=fileName.substring(0,fileName.lastIndexOf("."));//只取名字,去掉扩展名
String extend = fileName.substring(fileName.lastIndexOf(".")+1);// 获取文件扩展名
long noextfilename= System.currentTimeMillis();//获取时间戳 这种速度更快一些
String extra = "wkrj";//由于样式不能是数字所以加上wkrj来区分;
String myfilename=extra+noextfilename+"."+extend;//自定义文件名称
//文件全路径
String savePath = realPath + myfilename;// 文件保存全路径
String fileurl=path+myfilename;
File savefile = new File(savePath);
// 文件拷贝到指定硬盘目录
FileCopyUtils.copy(mf.getBytes(), savefile);
HashMap<String, String> map=new HashMap<String,String>();
map.put("fileextend", extend);//文件扩展名
map.put("fileurl", fileurl);//文件相对路径
map.put("filename", onlyfileName);//文件名
map.put("fileComponentName", fileComponentName);//前台文件组件的name值
newlist.add(map);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newlist;
}
}