Struts2中图片的尺寸修改

本案例采用两种方式进行对比,一种为读取图片然后在页面上显示图片,一种为读取图片后,处理图片尺寸之后在页面显示

主要代码如下:

package cn.tedu.web;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.swing.filechooser.FileSystemView;

import org.apache.commons.io.FilenameUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Controller
@Scope("prototype")
public class PhotoAction extends BaseAction{
	
	private InputStream imageStream;
	public InputStream getImageStream() {
		return imageStream;
	}
	public void setImageStream(InputStream imageStream) {
		this.imageStream = imageStream;
	}
	
	public String originalPic() throws FileNotFoundException {
		//修改图片尺寸后显示
		//获取图片输入流--图片位于桌面
		FileSystemView fsv = FileSystemView.getFileSystemView();
		File com = fsv.getHomeDirectory();
		String filePath = com.getPath() + File.separator + "文件" + File.separator + "头像++--.jpg";//word文档位于桌面/文件/头像++--.jpg
		imageStream = new FileInputStream(filePath);
		return SUCCESS;
	}
	
	public String modifyPic() throws IOException{
		//修改图片尺寸后显示
		//获取图片输入流--图片位于桌面
		FileSystemView fsv = FileSystemView.getFileSystemView();
		File com = fsv.getHomeDirectory();
		String filePath = com.getPath() + File.separator + "文件" + File.separator + "头像++--.jpg";//word文档位于桌面/文件/头像++--.jpg
		File file = new File(filePath);
		imageStream = new FileInputStream(file);
		//获取文件后缀名
		String suffixName = suffixName(file);
		System.out.println("文件后缀名:" + suffixName);
		//设置图片宽度、高度为100px 
		int width = 100;
		int height = 100;
		//获得图片 
		Image src = ImageIO.read(imageStream); 
		//将设置好的图片追加到BufferedImage中
		BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = img.createGraphics();
		//重构图片
		g.drawImage(src, 0, 0, width, height, null);
		//将照片编码为jpg格式的数组
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ImageIO.write(img, "jpg", out);//三个参数  图片  类型  输出流
		byte[] jpg = out.toByteArray();
		out.close();
		//再将数组包装为InputStream
		imageStream = new ByteArrayInputStream(jpg);
		
		return SUCCESS;
	}
	
	//获取文件后缀名
	public String suffixName(File file){
		 String fileName = file.getName();
		 return fileName.substring(fileName.lastIndexOf(".") + 1);
	}
}

struts.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	<package namespace="/demo" name="demo" extends="struts-default,json-default">
		<!-- 显示原始图片 -->
		<action name="photo1" class="photoAction" method="originalPic">
			<result name="success" type="stream">
				<!-- 发送图片信息 -->
				<param name="contentType">image/jpeg</param>
				<!-- in为photoAction类中的一个bean属性,其类型必须是InputStream类型 -->
				<param name="inputName">imageStream</param>
			</result>
		</action>
		
		
		<!-- 显示修改后的图片 -->
		<action name="photo2" class="photoAction" method="modifyPic">
			<result name="success" type="stream">
				<!-- 发送图片信息 -->
				<param name="contentType">image/jpeg</param>
				<!-- in为photoAction类中的一个bean属性,其类型必须是InputStream类型 -->
				<param name="inputName">imageStream</param>
			</result>
		</action>	
	</package>
</struts>

输入不同的请求地址

URL:http://localhost:8000/struts_day03/demo/photo1  显示原图(200x200)

URL:http://localhost:8000/struts_day03/demo/photo2  显示修改后的图片(100x100)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

荒--

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

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

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

打赏作者

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

抵扣说明:

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

余额充值