我有一個Java Servlet的它試圖從蒙戈DB將圖像發送到的Ext JS:如何從蒙戈DB將圖像發送到Ext JS的與Java Servlet的
@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的響應? 在此先感謝!
而不是發送圖像,您可以發送圖像網址嗎?將會更容易處理。 – 2013-03-25 14:51:32