Skip to content

这是一个基于SpringMVC的文件上传封装,通过配置文件配置上传路径,上传文件只需save()一下,对图片文件可压缩,剪切等操作。

License

Notifications You must be signed in to change notification settings

ZiryLee/FileUpload2Spring

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 

Repository files navigation

FileUpload2Spring

这是一个基于SpringMVC的文件上传封装,通过配置文件配置上传路径,上传文件只需save()一下,对图片文件可压缩,剪切等操作。

基于spring mvc 的文件上传实体类

内部包含MultipartFile

示例:

@RequestMapping(value = "/testUpload.html", method = RequestMethod.POST)
public ModelAndView testUploadAction(
  @RequestParam(value = "photo", required = false) MultipartFile file, 
  ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
  if( !UploadUtil.isNull(file) ) {
    UploadEntity uploadEntity = new UploadEntityImp(file);

    try {
      //上传文件
      System.out.println(uploadEntity.savePhoto(true));
      //获取缩略图
      System.out.println(uploadEntity.getSmallPhoto(160, 160));
    } catch (Exception e) {
      e.printStackTrace();
      printLog(e);
    }
  }
  return createModelAndView("WeCharTest/testUpload", modelMap);
}

数组方式(批量上传):

  1. @RequestParam(value = "files", required = false) List files

  2. @RequestParam(value = "photos", required = false) MultipartFile[] files

  3. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    List files = multipartRequest.getFiles("files");


  @RequestMapping(value = "/testUpload.html", method = RequestMethod.POST)
  public ModelAndView testUploadAction(
  		@RequestParam(value = "photos", required = false) MultipartFile[] files, 
  		ModelMap modelMap,
  		HttpServletRequest request, HttpServletResponse response) {
  
  	try {
  		UploadEntity uploadEntity = null;
 		for (MultipartFile file : files) {
  			if (!UploadUtil.isNull(file)) {
  				uploadEntity = new UploadEntityImp(file);
				 //上传文件
  				System.out.println(uploadEntity.savePhoto(true));
				//获取缩略图
  				System.out.println(uploadEntity.getSmallPhoto(160, 160));
  			}
  		}
  	} catch (Exception e) {
  		e.printStackTrace();
  	}
  
  	return createModelAndView("WeCharTest/testUpload", modelMap);
  }

删除图片:

  1. UploadUtil.deleteFile( str );
  2. 兼容旧版:
    if( !UploadUtil.deleteFile( str ) ) {
    UploadUtil.deleteWebFile(request, str);
    }

WEB地址文件加载到本地保存

示例:

UploadEntity uploadEntity = new DownloadEntityImp(http://....;);  
System.out.println(uploadEntity.savePhoto(true));  
System.out.println(uploadEntity.getSmallPhoto(160, 160));  

About

这是一个基于SpringMVC的文件上传封装,通过配置文件配置上传路径,上传文件只需save()一下,对图片文件可压缩,剪切等操作。

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages