Java怎么实现按比例缩小图片
作者:SunnyJourney
时间:2023-05-01
浏览:4
使用spring注解上传文件@RequestParam(value="",required=true),required为true时不能传入空值,为false时反之;UploadFile.getOriginalFilename()获取上传的文件的文件名;System.nanoTime()返回当前时间的纳秒,用做文件名;FileUtils.writeByteArrayToFile()上传文件到本地目录;使用BufferedImage将图片加载到内存中,然后对图片进行修改如大小变换、图片变灰、设置透明等。效果
本文目录
使用spring注解上传文件@RequestParam(value="", required=true),required为true时不能传入空值,为false时反之;UploadFile.getOriginalFilename()获取上传的文件的文件名;System.nanoTime()返回当前时间的纳秒,用做文件名;FileUtils.writeByteArrayToFile()上传文件到本地目录;使用BufferedImage将图片加载到内存中,然后对图片进行修改如大小变换、图片变灰、设置透明等。
效果图:

HTML:
form表单提交到java:
@SuppressWarnings("unused")
@RequestMapping(value="/IntOpers")
private void IntOper(@RequestParam(value="UploadFile", required=true) MultipartFile UploadFile, PrintWriter pw, HttpServletRequest request) throws IOException {
String StrReturn = "error", FileName = "", ext = "";
long time = 0;
if (!UploadFile.isEmpty()) {
if (UploadFile != null) {
String fullName = UploadFile.getOriginalFilename();
ext = fullName.substring(fullName.lastIndexOf("."));//获取扩展名称
time = System.nanoTime();
FileName = time + ext;//设置文件保存的名称
//原图
String upDir = "F:/java/upload-ssm";//文件上传路径
FileUtils.writeByteArrayToFile(new File(upDir, FileName), UploadFile.getBytes());
//缩小图片
Image srcFile = ImageIO.read(new File(upDir + "/" + FileName));
BufferedImage tag = new BufferedImage(28, 40, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(srcFile, 0, 0, 28, 40, null);
String FileName_tab = time + "_tab" + ext;//设置文件保存的名称
FileOutputStream out = new FileOutputStream(upDir + "/" + FileName_tab);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
jep.setQuality(1f, true);
encoder.encode(tag, jep);
out.close();
StrReturn = "success";
}
}
pw.write(StrReturn);
}
作者最新文章
xshell端口ssh SSH隧道建立方法
2026-11-03 10:11
CorelDRAW是免费软件吗?详解试用、订阅与一次性购买收费模式
2026-10-30 19:32
PDF怎么免费转成Word并保留内容?转换前后检查要点
2026-10-30 17:50
ArchiveofOurOwn小说快速入口链接
2026-10-30 10:30
冰点还原怎么安装 初始配置步骤
2026-10-28 13:38
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多

































