文件上传 mvc

实例

Spring管理文件上传解析器

  • bean的Id必须为multipartResolver
  • 定义最大上传量;
  • 可能会定义上传的字符集编码。
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="10485760"/>
		<property name="defaultEncoding" value="UTF-8"/>
	</bean>

实现文件上传

	@RequestMapping("/file")
	public String imageFile(MultipartFile image) throws IllegalStateException, IOException{
		
		//1.定义文件上传的目录  E:\jt-upload
		File imageFile = new File("E:/jt-upload");
		
		//2.判断文件是否存在
		if(!imageFile.exists()){
			//创建文件夹
			imageFile.mkdirs();
		}
		//3.获取文件名称
		String fileName  = image.getOriginalFilename();
		
		//4.实现文件上传 文件路径/文件名称
		image.transferTo(new File("E:/jt-upload/"+fileName));
		System.out.println("文件上传实现成功!!!!!");
		
		//跳转到index页面
		return "index";
	}

项目

编辑vo对象

  • 格式
{"error":0,"url":"图片的保存路径","width":图片的宽度,"height":图片的高度}

参数说明: 0代表是一张图片,如果是0,前台才可以解析并显示。1代表不是图片,
不显示如果不设置宽度和高度,则默认用图片原来的大小,所以不用设置

public class PicUploadResult{
	private Integer error = 0;
	private String url;
	private String width;
	private String height;
}

Controller

	@RequestMapping("/pic/upload")
	@ResponseBody
	public PicUploadResult fileUpload(MultipartFile uploadFile){
		return fileService.fileUpload(uploadFile);
	}

Service

@Service
public class FileServiceImpl implements FileService {
	
	private String localPath = "E:/file-upload/";
	private String urlPath = "http://image.test.com/";
	
	
	/**
	 * 1.判断图片的类型 jpg|png|git
	 * 2.判断是否为恶意程序
	 * 3.为了提高检索效率.将文件分文件存储.
	 * 		UUID:hash随机算法(当前毫秒数+算法+hash)=32位hash值 2^32=21亿
	 * 		3.1 aaaaaaaa-bbbbbbbb-cccccccc-dddddddd/1.jpg  
	 * 			优点:几乎永远不需要修改代码
	 * 		3.2 yyyy/MM/dd
	 * 
	 * 4.如何杜绝文件重名现象    
	 *	 uuid+随机数(100).jpg
	 * 5.实现文件上传
	 */
	@Override
	public PicUploadResult fileUpload(MultipartFile uploadFile) {
		PicUploadResult uploadResult = new PicUploadResult();
		
		//1.判断是否为图片类型
		String fileName = uploadFile.getOriginalFilename();//abc.jpg
		fileName = fileName.toLowerCase(); //将字符全部转化为小写
		if(!fileName.matches("^.*\\.(jpg|png|gif)$")){
			uploadResult.setError(1);
			return uploadResult;  //文件不是图片
		}
		
		//2.判断是否为恶意程序
		try {
			BufferedImage bufferedImage = 
					ImageIO.read(uploadFile.getInputStream());
			int height = bufferedImage.getHeight();	//获取图片的高度
			int width = bufferedImage.getWidth();	//获取图片的宽度
			if(height == 0 || width == 0){
				//表示不是图片
				uploadResult.setError(1);
				return uploadResult;
			}
			//程序执行到这里 证明是一张图片
			//为文件进行分文件存储   yyyy/MM/dd
			String datePath = 
			new SimpleDateFormat("yyyy/MM/dd").format(new Date());
			
			//定义文件保存的路径  E:/jt-upload/2018/12/12
			String dirPath = localPath + datePath;
			
			//判断文件夹是否存在
			File dirFile = new File(dirPath);
			if(!dirFile.exists()){
				//文件不存在时,应该创建文件夹
				dirFile.mkdirs();
			}
			
			//4.动态生成文件名称 UUID+三位随机数   ss-aa-cc  ssaacc
			String uuid = UUID.randomUUID().toString().replace("-", "");
			int randomNum = new Random().nextInt(1000);
			String imageFileType = 
			fileName.substring(fileName.lastIndexOf("."));  //.jpg
			String imageFileName = uuid + randomNum + imageFileType;
			
			//5.实现文件上传 E:/jt-upload/2018/12/12/ssdfsd.jpg
			String imageLocalPath = dirPath +"/" + imageFileName;
			uploadFile.transferTo(new File(imageLocalPath));
			
			//6.封装参数
			uploadResult.setHeight(height+"");
			uploadResult.setWidth(width+"");
			
			//7.准备虚拟路径    "http://image.jt.com/yyyy/MM/dd/abc.jpg";
			String imageUrlPath = urlPath + datePath + "/" + imageFileName;
			uploadResult.setUrl(imageUrlPath);//添加虚拟路径
		} catch (Exception e) {
			e.printStackTrace();
			uploadResult.setError(1);
			return uploadResult;
		}

		return uploadResult;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值