使用struts2+Hibernate4上传实现头像保存,并将图片路径存储至数据库,且显示在jsp页面

使用struts2+Hibernate4上传实现头像保存,并将图片路径存储至数据库,且显示在jsp页面

问题描述:

1.上传到服务器已经实现并且保存到数据库中了
2.获得服务器上的图片怎么实现
图片说明

既然已经存储了,那么就是图片的回显了,读取数据库中的图片路径,然后将图片内容响应给页面就可以了。
主要代码:

String filePath = "具体数据库存储的路径";

resp.setDateHeader("Expires", 0L);
resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
resp.addHeader("Cache-Control", "post-check=0, pre-check=0");
resp.setHeader("Pragma", "no-cache");
resp.setContentType("image/jpeg");

ServletOutputStream out = resp.getOutputStream();
out.write(FileUtils.readFileToByteArray(new File(filePath)));

FileUtils 是 apache.common.的一个工具类。

/**
* download模版
*/
public String download(){
downloadFile = templateService.getInputStreamById(model.getId());

    Template template = templateService.getById(model.getId());
    String agent = ServletActionContext.getRequest().getHeader("user-agent");
    try {
        fileName = encodeDownloadFilename(template.getName() + ".doc",agent);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally {
        try {
            downloadFile.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return "download";

}