上传文件公共方法:
/**
* 通过;spring 的方法解析所有方法
* @param multipartRequest 请求
* @param keys 获取参数key 对应的文件 key的值必须和前台页面的name值一样
* @return
*/
public static String getFileURL(MultipartHttpServletRequest multipartRequest, String keys){
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
return getPathValue(fileMap.get(keys),StaticUrl.getUrl());
}
protected static String getPathValue(MultipartFile file,String systemPath){
Long time=new Date().getTime();
if (!file.isEmpty()) {
try {
String fileName=file.getOriginalFilename();
if(StringUtils.isNotBlank(fileName)){
String[] str=fileName.split("\\.");
if(str.length==1){
systemPath=systemPath+time+str[0]+".JPEG";
}else{
systemPath=systemPath+time+"."+str[str.length-1];
}
}
// 文件保存路径
File fl=new File(systemPath);
if(!fl.getParentFile().exists()){
fl.getParentFile().mkdirs();
}
// 转存文件
file.transferTo(fl);
} catch (Exception e) {
e.printStackTrace();
}
}else{
return null;
}
return time+".jpg";
}
controller
/**
* 修改记录
* @param meeting
* @return
*/
@RequestMapping("updateById")
public String updateById(Meeting meeting, HttpServletRequest request){
return operationService.updateById(meeting,request);
}
service
/**
* 修改记录
* @param meeting
* @return
*/
@Override
public String updateById(Meeting meeting, HttpServletRequest request) {
MultipartHttpServletRequest mr = (MultipartHttpServletRequest) request;
if (null!=mr.getMultiFileMap().get("img")) {//判断是否有文件
String path=UploadFile.getFileURL(mr,"img");
}else{
meeting.setImagePath(" ");
}
return operationDao.updateById(meeting)+"";
}
获取路径
public static String getUrl(){
// String systemPath =null;
// try {
// systemPath = ResourceUtils.getURL("classpath:").getPath().replace("%20", " ").replace('/', '\\').substring(1);//从路径字符串中取出工程路径
// systemPath=systemPath.substring(0,systemPath.lastIndexOf("Minutes"));
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
//
// int result=systemPath.indexOf("\\");
// if(-1!=result){
// systemPath=systemPath.substring(result).replace("/","\\");
//
// }
return /*systemPath+*/"E:\\IDEA\\image\\";
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有