头像获取

Servlet通过文件获取图片头像

public class Head_Portrait {
    File files;
    BufferedImage image;
    public Head_Portrait(String file){
        try {
            this.files=new File(file);
            //读取File中的文件
            image=(BufferedImage) ImageIO.read(files);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void write(OutputStream sos) throws IOException
    {
        //将本地文件发到前端
        ImageIO.write(this.image, "png", sos);
        sos.close();
    }
}

获取头像

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        String realPath = req.getServletContext().getRealPath("/WEB-INF/upload");
        HashMap<String,String> map=new HashMap<>(16);
        ArrayList<String> list=new ArrayList<>(16);
        SelectFile.GetFile(new File(realPath),map);
        Set<String> strings = map.keySet();
        for(String string:strings){
            list.add(string);
        }
        String names=list.get(1);
        String Filename =names.split("_")[1];//截取uuid后的源文件名
        //过散列存储算法查找的源文件地址
        String storage = UUIDFile.storage(Filename, realPath);
        String Path=storage+"\\"+names;
        Head_Portrait head_portrait=new Head_Portrait(Path);
        head_portrait.write(resp.getOutputStream());
    }

前端获取

<body>
<img src="/FileWebJava_war_exploded/Head">
</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值