Map接受
代码语言:javascript复制 /**
* 通过;spring 的方法解析所有方法
* @param multipartRequest 请求
* @param keys 获取参数key 对应的文件 key的值必须和前台页面的name值一样
* @param systemPath 系统路径(活路径)
* @param path 自己目录的路径
* @return
*/
public static String[] getFileURL(MultipartHttpServletRequest multipartRequest, String[] keys,String systemPath,String path){
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
String[] vales=new String[keys.length];
for (int i=0;i<keys.length;i ) {
vales[i]=getPathValue(fileMap.get(keys[i]),systemPath,path);
}
return vales;
}
代码语言:javascript复制 protected static String getPathValue(MultipartFile file,String systemPath,String path){
if (!file.isEmpty()) {
try {
String fileName=file.getOriginalFilename();
if(StringUtils.isNotBlank(fileName)){
String[] str=fileName.split("\.");
if(str.length==1){
path=path new Date().getTime() str[0] ".JPEG";
}else{
path=path new Date().getTime() "." str[str.length-1];
}
}
// 文件保存路径
File fl=new File(systemPath path);
if(!fl.getParentFile().exists()){
fl.getParentFile().mkdirs();
}
// 转存文件
file.transferTo(fl);
} catch (Exception e) {
e.printStackTrace();
}
}else{
return null;
}
return path;
}
多文件一个文件名
代码语言:javascript复制 MultipartHttpServletRequest mr=(MultipartHttpServletRequest) request;
Iterator<String> ite=mr.getFileNames();
while (ite.hasNext()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String name = ite.next();
List<MultipartFile> file = mr.getFiles(name);
if (file != null || file.size() != 0) {
for (MultipartFile files : file) {
AppOpinionImgEntity opinionImgEntity = new AppOpinionImgEntity();
opinionImgEntity.setCreateBy("1");
opinionImgEntity.setCreateDate(DateTimeUtils.getDateTime());
opinionImgEntity.setUpdateBy("1");
opinionImgEntity.setUpdateDate(DateTimeUtils.getDateTime());
opinionImgEntity.setDelTag("0");
String sysPath = request.getSession().getServletContext().getRealPath("/");
String path = "upload/opinion/";
try {
String fileName = files.getOriginalFilename();
opinionImgEntity.setImgName(fileName);
opinionImgEntity.setOpinionId(opinionEntity.getId());
if (StringUtils.isNotBlank(fileName)) {
String[] str = fileName.split("\.");
if (str.length == 1) {
path = path new Date().getTime() str[0] ".JPEG";
opinionImgEntity.setImgType("JPEG");
} else {
path = path new Date().getTime() "." str[str.length - 1];
opinionImgEntity.setImgType(fileName.substring(fileName.lastIndexOf(".")));
}
}
// 文件保存路径
File fl = new File(sysPath path);
if (!fl.getParentFile().exists()) {
fl.getParentFile().mkdirs();
}
// 转存文件
files.transferTo(fl);
} catch (Exception e) {
e.printStackTrace();
return new HashMap<String, Object>();
}
opinionImgEntity.setImgUrl(path);
opinionImgDao.save(opinionImgEntity);
}
}
}