本案例采用两种方式进行对比,一种为读取图片然后在页面上显示图片,一种为读取图片后,处理图片尺寸之后在页面显示
主要代码如下:
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)