<img src="">中放绝对路径或者不在项目下图片的方法

做一个6张图的滚动图片,项目在F盘,图片在E盘,

1、用file协议放(file:///+绝对路径)测试只能放张,取多张时有问题或者有时只会取最后一张

2、使用tomcat文件夹下的conf里面的server.xml里面的host标签下 添加一个

<Context path="/file" docBase="E:\test" debug="0" reloadable="true"/>
3、使用io六读取图片,一般浏览百度什么都是图片在一个服务器,每次浏览都会到图片服务器读取出来输入到src中

package com.report.controller;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@Transactional
public class OutPutImage extends HttpServlet{


	@RequestMapping({"/getImage.d"})
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		String filePath = request.getParameter("filePath");
		try {
			response.setContentType("test/html;charset=UTF-8");
			response.setContentType("image/jpeg");
			FileInputStream fis = new FileInputStream(filePath);
			OutputStream out = response.getOutputStream();
			
			try {
				int count = 0;
				byte[] buffer = new byte[1024*1024];
				while((count = fis.read(buffer)) != -1) out.write(buffer, 0, count);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

这里的filepath就是图片的绝对路径,也就是图片的实际路径



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值