使用java

时间:2016-04-29 11:38:02

标签: java mysql rest

我已经将一些图像作为BLOB上传到MYSQL数据库中。现在我想通过其他Web服务和java来检索它。我能够检索一个图像。但是我怎样才能同时检索多个图像。什么可以是最好的解决方案吗?我的数据库就像这样

Snapshot of Database

任何帮助将不胜感激。谢谢。

我的DAO类方法是

public Response downloadById(int employeeId) {
    ResponseBuilder response=null;
    @SuppressWarnings("unchecked")
    ArrayList<UserProfile> userProfile=(ArrayList<UserProfile>)getHibernateTemplate().find("from UserProfile where employeeId=?",employeeId);
    for(UserProfile user:userProfile){
        byte[] image = user.getProfilePic();
        try{
            //String tomcatDir = System.getProperty("catalina.home");   
        FileOutputStream fos = new FileOutputStream("D:/img/"+employeeId+".png");
            File file=new File("D:/img/"+employeeId+".png");
            response = Response.ok((Object) file);
            response.header("Content-Disposition","attachment; filename=\"javatpoint_image.png\"");  
            fos.write(image);
            fos.close();
        }catch(Exception e){
            e.printStackTrace();
        }

    }

    return response.build();
}

我的Bean类是

public class UserProfile {

  private int employeeId;
  private String role;
  private byte[] profilePic;
public int getEmployeeId() {
    return employeeId;
}
public void setEmployeeId(int employeeId) {
    this.employeeId = employeeId;
}
public String getRole() {
    return role;
}
public void setRole(String role) {
    this.role = role;
}
public byte[] getProfilePic() {
    return profilePic;
}
public void setProfilePic(byte[] profilePic) {
    this.profilePic = profilePic;
}

}

我将图像放在一个文件夹中,然后从那里进行访问。

1 个答案:

答案 0 :(得分:0)

通常,您首先请求检索列出所有图片<img src=FURTHER-REQUEST?id=... ...>的HTML块。这导致许多后续请求,可能导致一些双重工作,一个特定ID的SELECT。

有一些技术可以优化它,但它们通常不值得。例如,您可以返回一个组合所有小图像的大图像,并通过CSS为每个用户显示单个块。

相关问题