今天遇到个一个问题,应该是和bean注入有关

       昨天提交了需求,今天甲方说这个页面下载有些问题,你在看一下,他说下载文件名会出现乱码,凭我的经验,肯定是因为他们古老的tomcat7导致的,结果呢,我下载了tomcat7,准备复现一下,调试了一下午都没复现出乱码的问题,实在是丢人。

        跑去看其他项目里的同目录下载,结果发现人家那是正常的,这真是太奇怪了,然后想着idea搞不到结果,那就换eclipse试试,结果怎么着,果然发现了问题:

       idea下载调用的类和eclipse的不一样,所以才一直无法复现。

导出方法代码:

public ModelAndView download(String fileId) throws Exception {
    ModelAndView mav = new ModelAndView("downloadView");   
    Map<String,String> fileMap= getFileById(fileId);
    File file = new File(fileMap.get("path")+"/"+fileMap.get("fileName"));
    if (!file.exists()){
        throw new Exception("要下载的附件不存在");
    }

    mav.setViewName("downloadView");
    mav.addObject("FileOriginName",fileMap.get("fileName"));
    mav.addObject("downloadFile",file);
    return mav;
}

两个不同的导出View实现类:

public class DownloadView extends AbstractView {

    public DownloadView() {
        setContentType("application/download;utf-8");
    }

    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        File file = (File)model.get("downloadFile");
        response.setContentType(getContentType());
        response.setContentLength((int) file.length());       
        String fileName = new String(file.getName().getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1).replace("+","%20");
        }
        response.setHeader("Content-Disposition","attachment;filename=\""+fileName+"\";");
        response.setHeader("Content-Transfer-Encoding","binary");
        OutputStream outputStream = response.getOutputStream();
        FileInputStream fileInputStream = new FileInputStream(file);
        FileCopyUtils.copy(fileInputStream,outputStream);
        fileInputStream.close();
        outputStream.flush();
        boolean delete = file.delete();
        System.out.println(delete);
    }
}
public class FileDownloadView extends AbstractView {
    public FileDownloadView() {
        setContentType("application/download;utf-8");
    }

    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String fileOriginName = (String) model.get("fileOriginName");
        response.setContentType(getContentType());        
        String fileName = new String(fileOriginName.getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1).replace("+","%20");
        response.setHeader("Content-Disposition","attachment;filename=\""+fileName+"\";");
        response.setHeader("Content-Transfer-Encoding","binary");
        response.setHeader("Content-type","application/octet-stream;charset=uft-8");
        response.setHeader("Set-Cookie","fileDownload=true;path="+request.getContextPath());
        OutputStream outputStream = response.getOutputStream();
        File file = (File)model.get("downloadFile");
        response.setContentLength((int) file.length());
        FileInputStream fileInputStream = new FileInputStream(file);
        FileCopyUtils.copy(fileInputStream,outputStream);
        fileInputStream.close();
        outputStream.flush();
        boolean delete = file.delete();
        System.out.println(delete);
    }
}

在看到这里的时候,我在想肯定有哪里还有另外的配置,不然怎么可能会调用到第二个类呢,果不其然,在 applicationContext-application.xml 中,有这么两行配置:

<bean id="download" class="com.util.DownloadView"/>
<bean id="downloadView" class="com.util.FileDownloadView"/>

从配置看,idea的运行是正常的,但是eclipse为什么会调用到第一个类呢?甚是奇怪,不知道有没有大佬能给个思路,解除我的疑惑,菜鸡不胜感激。

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值