当前位置: 代码迷 >> J2EE >> 急加班中,大侠帮看下文件下载的后缀名为什么是.zip?该怎么处理
  详细解决方案

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

热度:536   发布时间:2016-04-22 01:20:01.0
急,加班中,大侠帮看下文件下载的后缀名为什么是.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的Headerresponse.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……
  相关解决方案