使用java从MYSQL数据库检索多个图像
问题描述:
我已经在MYSQL数据库中上传了一些图像作为BLOB.Now我想通过其余的Web服务和java.I检索它我能够检索一个图像。但我如何检索多个在同一time.What图像可能是对此最好的解决办法吗?我的数据库,它只是像这样使用java从MYSQL数据库检索多个图像
任何帮助将提前appreciated.Thanks。
我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;
}
}
我把图像文件夹中,并从那里我acessing它。
答
',并带有一个带有单独ID的URL。然后img请求被分开处理,带有id的SELECT,并返回单个图像。 –
通常情况下,您会首先检索一个HTML块,并列出所有图像<img src=FURTHER-REQUEST?id=... ...>
。这会导致很多后续请求,可能会导致一些双重工作,即针对特定ID的SELECT。
有一些技术可以优化这一点,但他们通常不值得付出努力。例如,您可以为每个用户返回一张包含所有小图片的大图片,并通过CSS显示各个块。
+0
谢谢你的帮助先生。我可以从后端做到吗?我的代码中的手段我只发送一个图像的响应。我可以将它发送给多个图像吗? –
+0
您只需发送一段HTML,例如带有的'
请显示您的当前代码 –
'SELECT'。循环。读。 –
我正在使用hibernate和spring。因为我们正在将对象存储在ArrayList.What可以用于存储多个图像。 –