日期:2014-05-20  浏览次数:20764 次

用java实现文件下载 解压后提示文件头???被破坏 不可预料的压缩文件末端 请问怎么解决
Java code
public static void downLoadZipAndRar(HttpServletRequest request,
            HttpServletResponse response, String fileName) throws Exception,
            NullPointerException {
        
        String prjId = request.getParameter("prjId");
        if(prjId!=null||!"".equals(prjId)){
            PubDataManager.downloadAdd(prjId);
            StringBuffer redirectString = new StringBuffer();
            redirectString.append(PubDataManager.getPubDataManager().getPubResourceDesc(Constants.RSC_DOWNLOAD_HOST))
            .append("prjInfo/")
            .append(fileName);
            response.sendRedirect(response.encodeRedirectURL(redirectString.toString()));
            return;
        }
        String filePath = request.getSession().getServletContext().getRealPath(
                "/download/prjInfo");
        String codePrjStr = request.getParameter("codePrjId");
        if(codePrjStr != null && !"".equals(codePrjStr)) {
            long codePrjId = Long.parseLong(request.getParameter("codePrjId"));
            String code_prj_flag = request.getParameter("code_prj_flag");
            if(code_prj_flag != null && !"".equals(code_prj_flag) && code_prj_flag.equals("cpflag")) {
                String rootPath = request.getSession().getServletContext().getRealPath("/");
                filePath = rootPath.replaceAll("ruancu", "ruanko");
                filePath += WebappUtil.getCodePrjZipDir(codePrjId);
            }
        }
        
        BufferedOutputStream bos = null;
        BufferedInputStream bis  = null;
        StringBuffer sb = new StringBuffer(50);
        sb.append("attachment;  filename=");
        sb.append(fileName);
        
        try {
            if (fileName != null) {
                response.reset();
                response.setContentType("application/msword");
                response.setHeader("Content-Disposition", new String(sb
                        .toString().getBytes(), "ISO8859-1"));

                String tmp = filePath + "/" + fileName;

                File file = new File(tmp);
                bis = new BufferedInputStream(
                        new FileInputStream(tmp));

                int i;
                byte buffer[] = new byte[1024];
                if (file.exists()) {
                    bos = new BufferedOutputStream(response.getOutputStream());
                    while ((i = bis.read(buffer)) != -1) {
                        bos.write(buffer, 0, i);
                    }
                    bos.flush();    
                }
            }
        } catch (Exception e) {
            if(log.isDebugEnabled()){
                log.debug(fileName + " download was interruppt!", e);
            }
        }finally{
            try {
                if (bis != null) {
                    bis.close();
                }
            } catch (Exception e) {
            }
            try {
                if (bos != null) {
                    bos.close();
                }
            } catch (Exception e) {
            }
        }
    }

请大家看下这是怎么回事

------解决方案--------------------
是否判断了http header?
------解决方案--------------------
文件没有下载玩