/**
* @Description:
* @param request
* @param nameParam:要获取的表单的file名
* @Return: String
*/
public static HashMap<String,Object> uploadFileWidthLimit(HttpServletRequest request, String nameParam){
String fileUrl = "";
boolean isFail= false;
String errMsg = "";
try{
//获取上传文件的类型img,flash,file
String type = request.getParameter("type");
CommonsMultipartResolver commonsMultipartResolver =
new CommonsMultipartResolver(request.getSession().getServletContext());
//根据type类型获取限制上传的大小,单位字节
long limit = UtilConfig.getLong(type+".limit", 1024*1024);
commonsMultipartResolver.setMaxUploadSize(limit);
MultipartHttpServletRequest multipartRequest = commonsMultipartResolver.resolveMultipart(request);
//文件存储路径
String currentPath = Constants.NEWS_UPLOADPATH+"/"+type;
String currentDirPath = request.getServletContext().getRealPath(currentPath);
// 将路径前加上web应用名
currentPath = request.getContextPath()+"/" + currentPath;
logger.debug("currentPath>>>>>>{}",currentPath);
logger.debug("currentDirPath>>>{}",currentDirPath);
MultipartFile file = multipartRequest.getFile(nameParam);
String fileName = file.getOriginalFilename();
logger.debug("fileName>>>>{}",fileName);
// 获取文件扩展名
String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
//检测扩展名是否合法
ArrayList allowList = (ArrayList) allowedExtensions.get(type);
if(allowList.contains(ext)){
fileName = UtilDate.formatDateTime2()
+ RandomStringUtil.getRandomNum(3)
+ fileName.substring(fileName.lastIndexOf("."));
File pathToSave = new File(currentDirPath,fileName);
if(!pathToSave.exists())
pathToSave.mkdirs();
fileUrl = currentPath + "/" + fileName;
file.transferTo(pathToSave);
}else {
isFail = true;
errMsg="无效的文件类型: ";
}
}catch(Exception ex){
ex.printStackTrace();
isFail = true;
errMsg="未知错误!";
}
HashMap<String,Object> map = new HashMap();
map.put("isFail", isFail);
map.put("errMsg", errMsg);
map.put("fileUrl", fileUrl);
logger.debug("isFail>>>{}",isFail);
logger.debug("errMsg>>>>{}",errMsg);
logger.debug("fileUrl>>>{}",fileUrl);
return map;
}
分享到:
相关推荐
Spring CommonsMultipartResolver 上传文件,源码中包括spring的注解和视图交互及数据应用,上传文件等
使用springcloud的负载均衡 以及网关 实现文件的上传处理参看链接 https://blog.csdn.net/zhaoyu_nb/article/details/80579918
spring 支持@RequestBody注解依赖包 、 spring 支持@RequestBody注解依赖包 spring 支持@RequestBody注解依赖包
简单实现了spring上传文件的功能简单实现了spring上传文件的功能简单实现了spring上传文件的功能简单实现了spring上传文件的功能简单实现了spring上传文件的功能简单实现了spring上传文件的功能简单实现了spring上传...
spring配置文件实例
Spring5 文件上传所需的jar包:如果需要使用Spring中CommonsMultipartResolver解析器,解析multipart文件上传,需要导入这两个依赖包
spring mvc文件上传,通过监听器实现进度条
java spring 文件上传实例,包含了完整的 spring 文件上传配置
spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件spring配置文件...
Spring MVC文件上传下载,是http://blog.csdn.net/geloin/article/details/7537425#comments的配套资源。
Spring配置文件集合Spring配置文件集合Spring配置文件集合Spring配置文件集合Spring配置文件集合Spring配置文件集合Spring配置文件集合
使用Spring配置文件实现AOP
spring 配置文件 归类 IOC AOPspring 配置文件 归类 IOC AOP
基于SpringBoot的文件上传系统,前后端分离,单文件上传,多文件上传,大文件上传,断点续传,文件秒传,图片上传 项目经过严格测试,确保可以运行! 采用前后端分离的方式进行开发,实现了几种常用的文件上传功能...
Spring 文件上传
Spring MVC 文件上传下载 后端 - Java.zip
spring配置文件详解,Spring的applicationContext.xml文件代码:
bean文件 springbean文件
Spring Cloud Gateway的全局异常处理 Spring Cloud Gateway中的全局异常处理不能直接用@ControllerAdvice来处理,通过跟踪异常信息的抛出,找到对应的源码,自定义一些处理逻辑来符合业务的需求。 网关都是给接口做...