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;
}
<td align="left">
<a href="${ctx}/zxsb/dcProjectatt.do?method=outputBlob&attid=${dcProjectatt.attid}" target="_blank"><c:out
value="${dcProjectatt.attname}" />
</a>
</td>页面点击下载的时候为什么默认文件的后缀名是.zip 压缩的形式啊?

解决方案 »

  1.   

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

  2.   


    int length = 0;
                byte[] buf = new byte[1024];
                while ((length = is.read(buf)) != -1) {
                    response.getOutputStream().write(buf);
                }先按3楼的试试看,如果问题依旧,请楼主的代码改成int length = 0;
                byte[] buf = new byte[1024];
                while ((length = is.read(buf)) != -1) {
                    response.getOutputStream().write(buf, 0, length);//指定写出的数组长度
                }
    试试