生成的下载文件包含html

时间:2016-04-20 15:04:02

标签: spring spring-mvc download

我想编写一个方法,将应用程序生成的下载文件发送回内存。问题是发回的文件包含从其发布的页面的HTML。我认为这与我通过回复有关。

下载控制器

@RequestMapping(value="/download", method = RequestMethod.POST)
public String downloadRequest(@ModelAttribute("downloadFormBean") DownloadFormBean downloadFormBean, BindingResult result, Model model, HttpServletResponse response) {

    try{
          DownloadResponse dldres = pgpEncryptionService.getEncryptedFile(intNbr, false);
                if (dldres.getErrorType() != null) {
                    logger.error("An error occured during encryption");
                    model.addAttribute("ERROR",convert(dldres.getErrorType()));
                } else {
                    //Unencrypted file here
                    downloadRequest(dldres, false, response);
                }
            }
        }
    }catch(Exception ex){
        model.addAttribute("ERROR",ex.getMessage());
    }

    return initDownloadForm(model);
}

下载方法

public void downloadRequest(DownloadResponse dldres, Boolean encrpt, HttpServletResponse response) {

    try {

        String downloadTime = getCurTime();
        String fileName = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        response.resetBuffer();
        if(encrpt){
            fileName = "generated-"+downloadTime+".asc";
        } else {
            //Compress the bytearray
            ZipOutputStream zos = new ZipOutputStream(baos);
            ZipEntry entry;
            entry = new ZipEntry("ECUIDs-"+gmin+"-"+downloadTime+".bin");
            entry.setSize(dldres.getDownloadFile().length);
            zos.putNextEntry(entry);
            zos.write(dldres.getDownloadFile());
            zos.closeEntry();
            zos.close();
            fileName = "generated-"+downloadTime+".zip";
        }

        //These headers force the download of the page.
        response.setContentType("application/force-download");
        response.setHeader("Content-Transfer-Encoding", "binary");
        response.setHeader("Content-Disposition","attachment; filename="+fileName);
        if(encrpt){
            //File is already in memory and we do not need to convert to BAOS
            response.getOutputStream().write(dldres.getDownloadFile());
        }else{
            response.getOutputStream().write(baos.toByteArray());
        }
        response.flushBuffer();
    } catch (IOException e) {
        logger.error("Error Returning download file", e);
    }
}

当我在记事本中打开文件时,我得到这样的回复。

-----BEGIN PGP MESSAGE-----
Version: BCPG v1.54

hQEMA3UouH4AMEdDAQf/ey3BUjpHXgYK9lyGvyJZgAlczJCY7ey86Hj4PU7nlmyr
sQsbYtsvTTrZLE5B28qXnbZz21RFW4k3y/GIwGsH5RPBNcjLkkNVse41jwYCgVyv
KZWztCnS2S48ecmsHHBGkMAwhmkdk8/R++IfjP7lY7Ns5voH5tquCjepyHUYYaIw
OQu9zullnHbz0O05fxxDnUzoV9vCuGy8GhcuuR4+chzBrkskHr3lSHXlWZFqm8zM
FKPzozj0G2NG9OH0H2utZr5mosCbia/HW1b4B3pkNkbWpL3N3pPyibZdZ2qvPi/U
ldVoDKFPWu56EZibAHWZYZu21r2hFKpjuLnjstOfodLAtQEj/zN7YtnCmbZnbE49
njLMdJpp09DolC/xDS5cDSGUWkuK8oAJg3mIROv7xfJA7O0kryXGoznlxyoMuoxb
Yd/OiRbaZYqcR36hN7rhn4UcsoB6V4TvkCrqv1Cu7bVhs5S6F9CtldY5abI/DASO
s20vSjwaUXM0h+M6aNNGeK6Lg/SDh2DE7KTqtrM4k7uvHCeSSn7uEe62cX3CFYHc
yG23asx2jTwm7+nqZYJBRCKtnEO9axT2BxeOmASzJ/J5lnzs89Wi+DqcqhRDhAvs
6s5YycT3UIkaVWOhPzCqi1ICEgjPtK8nUgyxkgqisZoILuM0xN25O8Q5u1HKGPx2
OB8I7vvS0mx08WBz+fBcfo8CIEN3ZJvB48r0gL168bTa5/MAvrTEqodeyXuy7edr
G+It+7Knx2zgMAV1aetLVXh+O8WgBJlENI+eIxcBEqAE0jBIKiVp6tlV5fdafafd
y1RlHMI8mw3SSaPT6eg8xS0I/AYv8eo=
=j8P5
-----END PGP MESSAGE-----




<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <!-- This overrides the compatibility mode issues experienced in IE8 and 9 regarding intranet sites -->
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta charset="utf-8">
        <meta name="description" content="">

1 个答案:

答案 0 :(得分:0)

正如BalusC所说,即使我没有错误,我也会返回页面。为了解决这个问题,我在下载响应后返回null。

@RequestMapping(value =&#34; / download&#34;,method = RequestMethod.POST) public String downloadRequest(@ModelAttribute(&#34; downloadFormBean&#34;)DownloadFormBean downloadFormBean,BindingResult result,Model model,HttpServletResponse response){

try{
      DownloadResponse dldres = pgpEncryptionService.getEncryptedFile(intNbr, false);
            if (dldres.getErrorType() != null) {
                logger.error("An error occured during encryption");
                model.addAttribute("ERROR",convert(dldres.getErrorType()));
            } else {
                //Unencrypted file here
                downloadRequest(dldres, false, response);
                return null;
            }
        }
    }
}catch(Exception ex){
    model.addAttribute("ERROR",ex.getMessage());
}

return initDownloadForm(model);

}