springMVC4.0图片显示与文件下载

客户端与手机端公用,图片返回流


@RequestMapping(value = "/download")
    @ResponseBody
    public void download(
            String id,String type,@RequestParam(value = DEFAULT_JQUERY_JSONP_CALLBACK_PARAM_NAME, required = false) String callbackName,
            HttpServletResponse response) throws IOException {
        if (StringUtils.isNotEmpty(id) && StringUtils.isNotEmpty(type)) {
            HttpHeaders headers = new HttpHeaders();
            File file = null;
            if ("real".equals(type)) {
                com.miracle.mby.core.entity.File fileModel = fileService.getFilesByMd5(id);
                if (null != fileModel) {
                    Dict dict = fileService.getUploadFileDict();
                    headers.setContentDispositionFormData("attachment", new String(fileModel.getOrginName().getBytes("UTF-8"), "ISO8859-1"));
                    file = new File(dict.getValue() + fileModel.getPath());

                }
            } else if ("temp".equals(type)) {
                file = FileUtils.getTempUploadFile(id);
                if (file == null) {
                    logger.warn("资源不存在%s", id);
                } else {
                    headers.setContentDispositionFormData("attachment", new String(file.getName().getBytes("gbk"), "ISO8859-1"));
                }
            }
            if (FileUtils.isImage(file)) {
                response.setHeader("Accept-Ranges", "bytes");
                response.setHeader("Pragma", "no-cache");
                response.setHeader("Cache-Control", "no-cache");
                response.setDateHeader("Expires", 0);

            }else{
                response.setContentType("application/octet-stream");
                response.setHeader("Accept-Ranges", "bytes");
                response.setHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes("gbk"), "ISO8859-1"));

            }
            if(file.exists()){
                FileInputStream inputStream = new FileInputStream(file);
                response.setHeader("Content-Length", String.valueOf(file.length()));
                byte[] bos = IOUtils.read(inputStream);
                OutputStream out = null;
                out = response.getOutputStream();
                out.write(bos);
                out.flush();
                out.close();
                inputStream.close();
            }

        }
    }


是否是图片

public static boolean isImage(File file){
        if(file.isDirectory()){
            return false;
        }
        String fileName = file.getName();
        int len = fileName.indexOf(".");
        String imagesPattern  = fileName.substring(len+1,fileName.length()).toLowerCase();
        if("jpg".equals(imagesPattern)){
            return true;
        }else if("bmp".equals(imagesPattern)){
            return true;
        }else if("gif".equals(imagesPattern)){
            return true;
        }else if("psd".equals(imagesPattern)){
            return true;
        }else if("pcx".equals(imagesPattern)){
            return true;
        }else if("png".equals(imagesPattern)){
            return true;
        }else if("dxf".equals(imagesPattern)){
            return true;
        }else if("cdr".equals(imagesPattern)){
            return true;
        }else if("ico".equals(imagesPattern)){
            return true;
        }else if("bmp".equals(imagesPattern)){
            return true;
        }else if("jpeg".equals(imagesPattern)){
            return true;
        }else if("svg".equals(imagesPattern)){
            return true;
        }else if("wmf".equals(imagesPattern)){
            return true;
        }else if("emf".equals(imagesPattern)){
            return true;
        }else if("eps".equals(imagesPattern)){
            return true;
        }else if("tga".equals(imagesPattern)){
            return true;
        }else if("nef".equals(imagesPattern)){
            return true;
        }else if("tif".equals(imagesPattern)){
            return true;
        }else if("tiff".equals(imagesPattern)){
            return true;
        }else{
            return false;
        }

    }



你可能感兴趣的:(java)