Servlet通过文件获取图片头像
public class Head_Portrait {
File files;
BufferedImage image;
public Head_Portrait(String file){
try {
this.files=new 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];
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>