请问:不创建临时文件如何将内容为图片的InputStream显示到HTML页面中?

aistill 2002-03-09 10:50:53
读取了一张图片到InputStream中,不创建临时文件,如何将InputStream中的图片正确显示到HTML页面中呢?

...全文
235 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
aistill 2002-03-09
  • 打赏
  • 举报
回复
ch6,OK,我瞧瞧!谢谢!
cosmo 2002-03-09
  • 打赏
  • 举报
回复
oreilly 的java servlet programming 有提及, 但沒有書在手

看看CODE 吧, ch6, java servlet programming, oreilly

import java.awt.*;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.oreilly.servlet.ServletUtils;

import Acme.JPM.Encoders.GifEncoder;

public class GraphicalCounter extends HttpServlet {

public static final String DIR = "/images/odometer";
public static final String COUNT = "314159";

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
ServletOutputStream out = res.getOutputStream();

Frame frame = null;
Graphics g = null;

try {
// Get the count to display, must be sole value in the raw query string
// Or use the default
String count = (String)req.getQueryString();
if (count == null) count = COUNT;

int countlen = count.length();
Image images[] = new Image[countlen];

for (int i = 0; i < countlen; i++) {
URL imageSrc =
getServletContext().getResource(DIR + "/" + count.charAt(i) + ".GIF");
if (imageSrc == null) {
imageSrc = new URL("file:"); // placeholder, handle errors later
}
images[i] = Toolkit.getDefaultToolkit().getImage(imageSrc);
}

// Create an unshown Frame
frame = new Frame();
frame.addNotify();

// Load the images
MediaTracker mt = new MediaTracker(frame);
for (int i = 0; i < countlen; i++) {
mt.addImage(images[i], i);
}
try {
mt.waitForAll();
}
catch (InterruptedException e) {
res.sendError(res.SC_INTERNAL_SERVER_ERROR,
"Interrupted while loading image: " +
ServletUtils.getStackTraceAsString(e));
return;
}

// Check for problems loading the images
if (mt.isErrorAny()) {
// We had a problem, find which image(s)
StringBuffer problemChars = new StringBuffer();
for (int i = 0; i < countlen; i++) {
if (mt.isErrorID(i)) {
problemChars.append(count.charAt(i));
}
}
res.sendError(res.SC_INTERNAL_SERVER_ERROR,
"Could not load an image for these characters: " +
problemChars.toString());
return;
}

// Get the cumulative size of the images
int width = 0;
int height = 0;
for (int i = 0; i < countlen; i++) {
width += images[i].getWidth(frame);
height = Math.max(height, images[i].getHeight(frame));
}

// Get a graphics region to match, using the Frame
Image image = frame.createImage(width, height);
g = image.getGraphics();

// Draw the images
int xindex = 0;
for (int i = 0; i < countlen; i++) {
g.drawImage(images[i], xindex, 0, frame);
xindex += images[i].getWidth(frame);
}

// Encode and return the composite
res.setContentType("image/gif");
GifEncoder encoder = new GifEncoder(image, out);
encoder.encode();
}
finally {
// Clean up resources
if (g != null) g.dispose();
if (frame != null) frame.removeNotify();
}
}
}


aistill 2002-03-09
  • 打赏
  • 举报
回复
另外如果图片要作为背景,Applet也不行!
aistill 2002-03-09
  • 打赏
  • 举报
回复
是否可以用Servlet来完成?只是不知在HTML页面中的img src如何写?

各位可否说得更具体点呢?
cosmo 2002-03-09
  • 打赏
  • 举报
回复
可以用APPLET, 再加MULTIPART RESPONSE 試一試
留意, APPLET 要DECODE BASE64 --> BINARY
cosmo 2002-03-09
  • 打赏
  • 举报
回复
圖片有不同格式, BROWSER 不一定可以显示....

同時HTML 是HYPER TEXT<-- TEXT, 只是文字
cosmo 2002-03-09
  • 打赏
  • 举报
回复
圖片有不同格式, BROWSER 不一定可以显示....

同時HTML 是HYPER TEXT<-- TEXT, 只是文字

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧