java图片上传压缩

在工作中,会设计到文件上传等常用功能,但是有时候图片上传到服务器很很大,有的甚至有几十MB,我们前端加载图片就很慢了,甚至这些图片会占用服务器空间,积压服务器;

如何处理?

Thumbnailator 是一个优秀的图片处理的Google开源Java类库。处理效果远比Java API的好。从API提供现有的图像文件和图像对象的类中简化了处理过程,两三行代码就能够从现有图片生成处理后的图片,且允许微调图片的生成方式,同时保持了需要写入的最低限度的代码量。还支持对一个目录的所有图片进行批量处理操作

第一步:引入依赖包

  <!-- 图片缩略图 -->
            <dependency>
                <groupId>net.coobird</groupId>
                <artifactId>thumbnailator</artifactId>
                <version>0.4.8</version>
            </dependency>

第二步:在上传图片到Linux服务器成功之后我们再去处理图片,我这里以Linux系统为案例

加上这段代码即可: Thumbnails.of(new File[] { new File(UPLOAD_PATH + fileName) })
                                        .scale(1f).outputQuality(0.25f)
                                        .toFile(new File(UPLOAD_PATH + fileName));
//压缩之后重新覆盖图片

之前为20MB的压缩之后如果高度和宽度不进行压缩:压缩后图片大小为1.1MB

public static List<String> listFileNames(InputStream inputstream,String fileName) {
		List<String> list = new ArrayList<String>();
		ChannelSftp sftp = null;
		Channel channel = null;
		Session sshSession = null;
		try {
			JSch jsch = new JSch();
			jsch.getSession(username, url, port);
			sshSession = jsch.getSession(username, url, port);
			sshSession.setPassword(password);
			Properties sshConfig = new Properties();
			sshConfig.put("StrictHostKeyChecking", "no");
			sshSession.setConfig(sshConfig);
			sshSession.connect();
			channel = sshSession.openChannel("sftp");
			channel.connect();
			System.out.println("连接远程服务器成功");
			sftp = (ChannelSftp)channel;
		    sftp.cd(UPLOAD_PATH);
		    sftp.put(inputstream, fileName);//图片上传成功
		    
		    
           //new File[] { new File(UPLOAD_PATH + fileName) }:成功之后获取上传到服务器的图片,等比压缩,高度和宽度不变,清晰度也不变
		  //--------------图片等比压缩------------------
			//UPLOAD_PATH="/data/yby-img-file/";
		    Thumbnails.of(new File[] { new File(UPLOAD_PATH + fileName) })
	         .scale(1f).outputQuality(0.25f)
	         .toFile(new File(UPLOAD_PATH + fileName));
		    
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeChannel(sftp);
			closeChannel(channel);
			closeSession(sshSession);
		}
		return list;
	}

如果想按照大小进行压缩

		//不按比例,就按指定的大小进行缩放
		Thumbnails.of(fromPic).size(100, 100).keepAspectRatio(false).toFile(toPic);
		//或者Thumbnails.of(fromPic).forceSize(100,100).toFile(toPic);

 

其它更详细功能使用介绍地址:

http://blog.csdn.net/chenleixing/article/details/44685817

http://www.qzblog.net/blog/220

http://blog.csdn.net/wangpeng047/article/details/17610451

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java 图片上传压缩处理可以使用 Java 图像处理库 ImageIO 和 Java 压缩库 ZipOutputStream。 以下是一个简单的 Java 图片上传压缩处理实现示例: 1. 获取上传图片文件。 2. 使用 ImageIO 读取图片文件,将其转换为 BufferedImage 对象。 3. 使用 BufferedImage 的 getScaledInstance() 方法缩放图片。 4. 使用 ImageIO 将缩放后的 BufferedImage 对象写入临时文件。 5. 使用 ZipOutputStream 压缩临时文件。 6. 将压缩后的文件上传到服务器。 以下是示例代码: ```java import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import javax.imageio.ImageIO; public class ImageUploadCompress { public void uploadAndCompress(File imageFile, String zipFileName, int width, int height) throws IOException { // 读取图片文件 BufferedImage originalImage = ImageIO.read(new FileInputStream(imageFile)); // 缩放图片 BufferedImage scaledImage = originalImage.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH); // 写入临时文件 File tempFile = File.createTempFile("temp_", ".jpg"); ImageIO.write(scaledImage, "jpg", tempFile); // 压缩临时文件 ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(zipFileName)); zip.putNextEntry(new ZipEntry(tempFile.getName())); FileInputStream in = new FileInputStream(tempFile); byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) > 0) { zip.write(buffer, 0, len); } zip.closeEntry(); in.close(); zip.close(); // 将压缩后的文件上传到服务器 // ... } } ``` 此示例仅演示了基本的图片上传压缩处理,实际应用中还需要考虑文件上传安全和性能等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hexu_blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值