[置顶] Java WebService将外部磁盘文件按照流的方式显示在页面

//调用  

@RequestMapping(value = "/pic/eid-{eventid:.*}", method = RequestMethod.GET)

    public void getpicByeventid(HttpServletRequest request, HttpServletResponse response,
            @PathVariable("eventid") int eventid,
            @RequestParam(value = "w", defaultValue = "0") int w,
            @RequestParam(value = "h", defaultValue = "0") int h) throws IOException {
        response.setHeader("Cache-Control", "max-age=" + (7 * 24 * 60 * 60));
        response.setDateHeader("Last-Modified", new Date().getTime());


        String mediaid = mediaDao.getMiediaideWeventid(eventid);
        try {
            if (mediaid.length() < 2) {
                response.sendError(404, "exp");
                return;
            }
            String imgurl = rootpath + mediaid.substring(0, 2) + "/" + mediaid.substring(2);


            if (w != 0 || h != 0) {
                this.diplayImageBySize(request, response, imgurl, mediaid, w, h);
            } else {
                this.diplayImage(request, response, imgurl, mediaid);
            }
        } catch (Exception ex) {
            response.sendError(500, "exp");
            return;
        }


    }

//设置宽度高度

   public void diplayImageBySize(HttpServletRequest request, HttpServletResponse response, String imgurl, String id, int w, int h) throws IOException {
        response.setHeader("Cache-Control", "max-age=" + (7 * 24 * 60 * 60));
        response.setDateHeader("Last-Modified", new Date().getTime());
        if (request.getHeader("If-Modified-Since") != null) {
            response.setStatus(304);
            return;
        }


        BufferedImage buffImg = dpservice.diplayImg(w + "," + h, imgurl);
        Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter writer = (ImageWriter) iter.next();
        ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(0.9f);
        ColorModel colorModel = ColorModel.getRGBdefault();
        iwp.setDestinationType(new javax.imageio.ImageTypeSpecifier(colorModel,
                colorModel.createCompatibleSampleModel(8, 8)));
        response.setContentType(request.getSession().getServletContext().getMimeType(id));
        OutputStream os = response.getOutputStream();
        MemoryCacheImageOutputStream output = new MemoryCacheImageOutputStream(os);
        writer.setOutput(output);
        IIOImage image = new IIOImage(buffImg, null, null);
        writer.write(null, image, iwp);
        writer.dispose();
        output.flush();
        output.close();
        os.close();
        buffImg = null;
    }

//不设置宽度高度 

  public void diplayImage(HttpServletRequest request, HttpServletResponse response, String imgurl, String id) throws IOException {
        response.setHeader("Cache-Control", "max-age=" + (7 * 24 * 60 * 60));
        response.setDateHeader("Last-Modified", new Date().getTime());
        if (request.getHeader("If-Modified-Since") != null) {
            response.setStatus(304);
            return;
        }
        //String imgurl = String.format(this.imgpath, id, id);
        BufferedImage buffImg = ImageIO.read(new File(imgurl));
        response.setContentType(request.getSession().getServletContext().getMimeType(id));
        OutputStream os = response.getOutputStream();
        ImageIO.write(buffImg, "JPEG", os);
        os.flush();
        os.close();
        buffImg = null;
    }

//视频  

 @RequestMapping(value = "/video/mid-{mediaid:.*}", method = RequestMethod.GET)
    public void getvideo(HttpServletRequest request, HttpServletResponse response,
            @PathVariable("mediaid") String mediaid) throws FileNotFoundException, IOException {
        try {
            if (mediaid.length() < 2) {
                response.sendError(404, "exp");
                return;
            }
            String voidurl = rootpath + mediaid.substring(0, 2) + "/" + mediaid.substring(2);
            BufferedInputStream buffImg = new BufferedInputStream(new FileInputStream(new File(voidurl)));
            if (mediaDao.getMiediatype(mediaid).equals("")) {
                response.sendError(404, "exp");
                return;
            }
            response.setContentType(mediaDao.getMiediatype(mediaid));
            byte[] b = new byte[buffImg.available()];
            buffImg.read(b);
            response.getOutputStream().write(b);
            response.getOutputStream().flush();
            response.getOutputStream().close();


        } catch (Exception ex) {
            response.sendError(500, "exp");
            return;
        }




    }

页面直接<img src='项目名/Action/...'>直接显示

视频一样直接显示




getpicByeventid方法的异常处理时response.sendError(500, "exp");直接在http头中加入500并返回,但是因为读取图片是直接在http头中的以io流写进去,并且push出去的,所以异常以后再那边在给http头中写数据就会出现异常.........最好的改动是将response.sendError(500, "exp");这个换为一个logger日志打印出来就好

你可能感兴趣的:(java,Web,service)