我的图片资源文件都放在了网站目录的外面如何访问?
比如:网站是tomcat/webroot/myweb/
而图片文件在E:\\pictures
我在客户端如何请求到?
客户端请求http://localhost://myweb/2.jpg
则指向E:\\pictures\2.jpg
比如:网站是tomcat/webroot/myweb/
而图片文件在E:\\pictures
我在客户端如何请求到?
客户端请求http://localhost://myweb/2.jpg
则指向E:\\pictures\2.jpg
conf/server.xml中加上
<Context path="/myweb" docBase="E:\\pictures" debug="0" reloadable="false" />
在程序里使用
<img src="/images/12112.gif"> 就能访问了!
下边我说一下他们的含义吧
Host
Host表示一个虚拟主机,一个引擎可以包含多个Host。用户通常不需要创建自定义的Host,因为Tomcat给出的Host接口的实现(类StandardHost)提供了重要的附加功能。
Context
一个Contex表示了一个Web应用程序,运行在特定的虚拟主机中.一个Host可以包含多个Context(代表Web应用程序),每一个Context都有一个惟一的路径。
path="/images"代表访问路径
docBase="E:\\pictures" 代表文件在计算机中的路径
reloadable="false" 表示是否自动加载
用流的方式输出,
在 html 中 <img src = "Picture?fileName=" width = "" height = "">
别忘了提供参数 fileName 这个是你服务器上的绝对路径 可以写在数据库里
这样就可以了 你甚至可以在图片上加水印的文字
下面是源代码
package com.miao.pub.action;import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;public class Picture extends HttpServlet { /**
*
*/
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) {
doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response) {
String fileName = null; fileName = request.getParameter("fileName");
if (fileName == null || fileName.length() == 0)
return; response.setContentType("image/jpeg");
response
.setHeader("Content-Disposition", "inline;filename=" + fileName); try {
JPEGCodec.createJPEGEncoder(
execute(response.getOutputStream(), fileName, new Integer(
800), new Integer(600), 0.8F));
} catch (ImageFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} private OutputStream execute(OutputStream os, String filePath,
Integer picWidth, Integer picHeight, Float picPara)
throws IOException {
Image image = null;
BufferedImage bimage = null;
// ͼƬԭʼ³ߴ勊 int width = 0;
int height = 0; try {
image = ImageIO.read(new File(filePath));
width = image.getWidth(null);
height = image.getHeight(null);
width = width < picWidth ? width : picWidth;
height = height < picHeight ? height : picHeight;
bimage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
bimage.getGraphics().drawImage(image, 0, 0, width, height, null); Graphics g = bimage.getGraphics();
g.setColor(Color.white);
g.setFont(new Font("Monotype Corsiva", Font.PLAIN, 25));
g.drawString("Powered by Miao.Liu", width - 220, height - 10);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(picPara == null ? 1 : picPara.floatValue(), false);
encoder.encode(bimage);
return encoder.getOutputStream(); } catch (IOException e) {
e.printStackTrace();
throw e;
}
}
}
用流的方式输出,
在 html 中 <img src = "Picture?fileName=" width = "" height = "">
别忘了提供参数 fileName 这个是你服务器上的绝对路径 可以写在数据库里
这样就可以了 你甚至可以在图片上加水印的文字
下面是源代码
package com.miao.pub.action;import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;public class Picture extends HttpServlet { /**
*
*/
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) {
doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response) {
String fileName = null; fileName = request.getParameter("fileName");
if (fileName == null || fileName.length() == 0)
return; response.setContentType("image/jpeg");
response
.setHeader("Content-Disposition", "inline;filename=" + fileName); try {
JPEGCodec.createJPEGEncoder(
execute(response.getOutputStream(), fileName, new Integer(
800), new Integer(600), 0.8F));
} catch (ImageFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} private OutputStream execute(OutputStream os, String filePath,
Integer picWidth, Integer picHeight, Float picPara)
throws IOException {
Image image = null;
BufferedImage bimage = null;
// ͼƬԭʼ³ߴ勊 int width = 0;
int height = 0; try {
image = ImageIO.read(new File(filePath));
width = image.getWidth(null);
height = image.getHeight(null);
width = width < picWidth ? width : picWidth;
height = height < picHeight ? height : picHeight;
bimage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
bimage.getGraphics().drawImage(image, 0, 0, width, height, null); Graphics g = bimage.getGraphics();
g.setColor(Color.white);
g.setFont(new Font("Monotype Corsiva", Font.PLAIN, 25));
g.drawString("Powered by Miao.Liu", width - 220, height - 10);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(picPara == null ? 1 : picPara.floatValue(), false);
encoder.encode(bimage);
return encoder.getOutputStream(); } catch (IOException e) {
e.printStackTrace();
throw e;
}
}
}