只能将支持 java.io.Serializable 接口的对象写入流中。
Serializable 是序列化接口。
未实现Serializable 接口会报NotSerializableException。例如:Image对象。
现将对象序列化,再写入流中。
将Image对像写入流中与servlet通信:
先将Image序列化:
ImageIcon imageIcon = new ImageIcon(image);
再写入流中
objStream.writeObject(imageIcon);
package com.liujy.web.servlet;
import java.awt.Image;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.ImageIcon;
import com.liujy.commons.GetImageFile;
public class TestCutServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
OutputStream out = response.getOutputStream();
ObjectOutputStream objStream = new ObjectOutputStream(out);
// 返回image对象,自己写
GetImageFile getImageFile = new GetImageFile();
getImageFile.init();
Image image = getImageFile.getImage();
System.out.println("************测试2*************");
//序列化BufferedImage对象
ImageIcon imageIcon = new ImageIcon(image);
objStream.writeObject(imageIcon);
objStream.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package com.liujy.web.applet;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.ImageIcon;
public class TestImage extends Applet {
Image image;
ImageIcon imageIcon;
public void init() {
super.init();
setVaule();
this.setContentPane(getTotleJPanel());
}
public void setVaule() {
URL url = null;
InputStream inStr = null;
ObjectInputStream objStream = null;
try {
url = new URL(getCodeBase(), "/appletimage/servlet/TestCutServlet");
URLConnection conn = url.openConnection();
conn.setUseCaches(true);
inStr = (InputStream) conn.getInputStream();
objStream = new ObjectInputStream(inStr);
imageIcon = (ImageIcon) objStream.readObject();
image = imageIcon.getImage();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void getTotleJPanel(){
JPanel p = new JPanel(){
private static final long serialVersionUID = 1L;
public void paint(Graphics g) {
//g.setColor(Color.red);
//g.drawLine(1, 1, 55, 55);
g.drawImage(image, 0, 0, null);
}
};
return p;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<
applet code="com.liujy.web.applet.TestImage.class" codebase="applet/"></applet>
</body>
</html>
在WebRoot中新建applet文件夹,将class文件拷贝到applet中。(当然也可以设置默认class文件存放路径)
基本的绘制图片到applet中,当要添加更复杂的图像操作时可是使用双缓冲技术,顺便封装JPanel类。在里不多讲了……