服务器传输图片到客户端,有时候是直接下载,有时候是类似src=’请求路径’
其主要区别在于
res.setContentType("img/jpg"); res.setHeader("Content-Disposition", "attachment;filename="+fileName);
加了这两行之后就是下载 一个是格式,一个是文件名字
另外附上下载图片代码:
@ResponseBody @ request Mapping(value = "/downloadFile", method = { RequestMethod.GET, RequestMethod.POST }) public void downloadFile(HttpServletRequest request, HttpServletResponse res) { logger.info("download file ..."); String fileName = request.getParameter("fileName"); File file = new File(fileUploadPath + fileName); if (!file. exists ()) { logger.error("resource file not exists."); } BufferedInputStream bis = null; BufferedOutputStream bos = null; try { res.setContentType("img/jpg"); res.setHeader("Content-Disposition", "attachment;filename="+fileName); bis = new BufferedInputStream(new FileInputStream(file)); bos = new BufferedOutputStream(res.getOutputStream()); byte [] bys = new byte[1024]; int len = 0; while ((len = bis.read(bys)) != -1) { bos.write(bys, 0, len); } logger.info("downloadComplete..."); } catch (IOException e) { e.printStackTrace(); logger.error("downloadError", e); } finally { try { bos. close (); bis.close(); } catch (IOException e) { } } }