日期:2014-05-19  浏览次数:20613 次

急,加班中,大侠帮看下文件下载的后缀名为什么是.zip?
Java code

public ActionForward outputBlob(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) {

        DynaBean dynaform = (DynaBean) form;
        Long attid = Long.valueOf((String) dynaform.get("attid"));
        DcProjectatt dcProjectatt = dcProjectattMag.getObjectById(attid);
        String attcontent = dcProjectatt.getAttpath();
        try {
            File file = new File(attcontent);
            InputStream is = new FileInputStream("D:\\tomcat\\webapps\\ccms\\u\\cms\\www\\201206\\041559429329.xlsx");
            @SuppressWarnings("unused")
            int length = 0;
            byte[] buf = new byte[1024];
            while ((length = is.read(buf)) != -1) {
                response.getOutputStream().write(buf);
            }
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


HTML code

<td align="left">
                    <a href="${ctx}/zxsb/dcProjectatt.do?method=outputBlob&attid=${dcProjectatt.attid}" target="_blank"><c:out
                            value="${dcProjectatt.attname}" />
                    </a>
                </td>


页面点击下载的时候为什么默认文件的后缀名是.zip 压缩的形式啊?

------解决方案--------------------
设置一下HTTP头信息
Java code

// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + "041559429329.xlsx");
response.addHeader("Content-Length", "" + file.length());
response.setContentType("application/octet-stream");

------解决方案--------------------
探讨
引用:
设置一下HTTP头信息

Java code


// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + "041559429329.xlsx");
response.addHeader("Content-Length", "" + fil……