我有一个 Java Servlet,它试图将图像从 Mongo DB 发送到 Ext JS:
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String action = req.getParameter("action");
if (action != null && action.equals("download")) {
resp.setContentType("text/html");
resp.setHeader("Content-Disposition", "attachment;filename=" + "images.jpg");
try {
DB db = DataBaseMongoService.getDb("forum_images"); //class that manages Mongo DB access
GridFS gfs = new GridFS(db, "image");
GridFSDBFile imageForOutput = gfs.findOne("images.jpg");
InputStream in = imageForOutput.getInputStream();
ServletOutputStream out = resp.getOutputStream();
out.write(IOUtils.toByteArray(in));
out.flush();
in.close();
out.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
}
}
}
我的 Ext JS 调用如下所示:
Ext.Ajax.request({
url: 'ForumImageServlet',
method: 'GET',
params: {
action: 'download'
},});
响应是图像的字节流,如下所示:
����JFIF��� "" $(4,$&1'-=-157:::#+?D?8C49:77%w777777777777777777777777777777777777777777777777��Pp"��ï...
如何获得真实图像作为对我的 servlet 的响应?提前致谢!