Java 代码如何 读取本地路径的 图片 文件 显示到浏览器 img src 路劲中

由于图片是保存在本地,浏览器不能直接访问本地的资源,所以需要一个 类 去把本地的资源读到服务器中 浏览器才可以访问服务器中读出的本地图片。

前台代码:

//C:\files_system\123.png 是本地图片路径
//${ctx}/getpicture/toFindImg?imgUrl= 是请求后台的地址
<img src='${ctx}/getpicture/toFindImg?imgUrl=C:\files_system\123.png';>

后台代码:

@RestController
@RequestMapping("getpicture")
public class GetPicture {
    /**
     * @param imgUrl 图片在本地磁盘的位置 如:E:/teacherCompetition/1/images/1.jpg
     * @param request
     * @param response
     */
    @RequestMapping("/toFindImg")
    public void picToJSP(@RequestParam("imgUrl") String imgUrl, HttpServletRequest request, HttpServletResponse response){
        FileInputStream in;
        response.setContentType("application/octet-stream;charset=UTF-8");
        try {
            //图片读取路径
            in=new FileInputStream(imgUrl);
            int i=in.available();
            byte[]data=new byte[i];
            in.read(data);
            in.close();
            //写图片
            OutputStream outputStream=new BufferedOutputStream(response.getOutputStream());
            outputStream.write(data);
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OOObject

你的鼓励是我创作的最大源泉

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值