前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【java基础】压缩图片上传

【java基础】压缩图片上传

作者头像
用户5640963
发布2019-07-25 14:33:07
1.8K0
发布2019-07-25 14:33:07
举报
文章被收录于专栏:卯金刀GG卯金刀GG

1、pom.xml

<dependency>
    <groupId>net.coobird</groupId>
    <artifactId>thumbnailator</artifactId>
    <version>0.4.8</version>
</dependency>

2、代码

/**
	 * 上传图片
	 */

	@RequestMapping(method = RequestMethod.POST, value = "upload")
	public Object UploadFile(MultipartHttpServletRequest requestFile) throws IllegalStateException, IOException {
		log.info("*******************进入图片上传的方法*************************");
        ServletContext servletContext = WebContext.getServletContext();
        // 得到文件绝对路径
        String realPath = servletContext.getRealPath("/");
        System.out.println("realPath:"+realPath);
        
        InputStream is = null;
        FileOutputStream fs = null;

        
		DataState dataState = new DataState();
		HttpServletRequest request = WebContext.getServletRequest();
		UserEntity user = WebContext.getUserEntity();
		Iterator<String> itr = requestFile.getFileNames();
		requestFile.setCharacterEncoding("utf-8");
		MultipartFile file = requestFile.getFile(itr.next());

		// 转换成File
		CommonsMultipartFile cf = (CommonsMultipartFile) file;
		DiskFileItem fi = (DiskFileItem) cf.getFileItem();
		File f = fi.getStoreLocation();		
		
		
		String fileName = file.getOriginalFilename();
		System.out.println(fileName);
		// 自定义的文件名称
		String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName;
		String totrueFileName = String.valueOf(System.currentTimeMillis())+1 + fileName;
		String time = new SimpleDateFormat("yyyyMMdd").format(new Date());
		
		 /** 临时文件夹*/
		 String imgPath = "uploadImgTemp" + File.separator;
		 String tempPath = realPath +"\\"+ imgPath;
		 System.out.println("old-path-" + tempPath);
		 File oldFile = new File(tempPath);
		 if (!oldFile.exists()) {
		     oldFile.mkdirs();
		 }
		 /** 处理后文件夹*/
		 String newImaPath = "uploadImg" + File.separator;
		 String newPath = realPath +"\\" + newImaPath;
		 System.out.println("new-path-" + newPath);
		 File newFile = new File(newPath);
		 if (!newFile.exists()) {
		     newFile.mkdirs();
		 }
	
		/** 先存取源文件*/
		is = file.getInputStream();
		fs = new FileOutputStream(tempPath + trueFileName);
		//...
		if (file.getSize() > 0 && file.getSize()>1048576) {
		    byte[] buffer = new byte[1024 * 1024];
		    int bytesum = 0;
		    int byteread = 0;
		    while ((byteread = is.read(buffer)) != -1) {
		        bytesum += byteread;
		        fs.write(buffer, 0, byteread);
		        fs.flush();
		    }
		    fs.close();
		    is.close();
		}
		/** 处理源文件 ,进行压缩再放置到新的文件夹*/
		String oldPath = tempPath + trueFileName;
		String copyPath = newPath + totrueFileName;
		
		Thumbnails.of(oldPath).scale(0.2f).toFile(copyPath);//按比例缩小 
		File toFile = new File(copyPath);	
		log.info("*******************上传图片大小:"+toFile.length()+"*************************");
		
		//ftp上传
		String filePath = Common.getValue("FTP_REMOTE_FILEPATH", "com.ztb.common.util.properties.ftp") + time;
		
		boolean result = FtpUtil.upload(new File(copyPath), filePath, totrueFileName);
		if (result == false) {
			dataState.setStateCode(StateCode.OPT_ERROR);
			dataState.setData("");
			return DataEvent.wrap("uploadok", dataState);
		}

		String url = Common.getValue("RETURN_URL", "com.ztb.common.util.properties.ftp") + time + "/" + totrueFileName;
		WebContext.getSession().setAttribute("url", url);
		dataState.setStateCode(StateCode.OPT_OK);
		dataState.setData(url);
		return DataEvent.wrap("uploadok", dataState);

	}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档