附件在客户端打开或下载

    

        response.reset(); // 非常重要
        if (true) { // 在线打开方式
            URL u = new URL("file:///" + f.getAbsolutePath());
            String contentType = u.openConnection().getContentType();
            response.setContentType(contentType);
            response.setHeader("Content-Disposition", "inline;filename=" + f.getName());
            // 文件名应该编码成utf-8
        }
        else {
            // 纯下载方式
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-Disposition", "attachment;filename=" + f.getName());
        }


你可能感兴趣的:(附件在客户端打开或下载)