如何在Spring MVC 中下载文件时弹出"Save As" window?

How to make "Save As" window pop up when downloading files in Spring MVC?

我目前正在实现下载上传文件的功能。

功能正常, "Save As" window 没有出现

我会附上执行下载功能的代码。

DownLoadView.java

public class DownLoadView extends AbstractView
{
    private File file;

    public  DownLoadView(File file)
    {
        setContentType("application/octet-stream");
        this.file = file;
    }

    @Override
    protected void renderMergedOutputModel(Map<String, Object> arg0, HttpServletRequest req, HttpServletResponse resp)
            throws Exception
    {
        resp.setContentType(getContentType());
        resp.setContentLength((int) file.length());
        System.out.println("getContentType >> " + resp.getContentType());
        String userAgent = req.getHeader("User-Agent");

        boolean ie = userAgent.indexOf("MSIE") > -1;

        String fileName = file.getName();

        if(ie)
        {
            fileName = URLEncoder.encode(file.getName(), "utf-8").replaceAll("\+", "%20");
        }
        else
        {
            fileName = new String(file.getName().getBytes("utf-8")).replaceAll("\+", "%20");
        }

        resp.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

        OutputStream out = resp.getOutputStream();
        FileInputStream fis = null;

        System.out.println("resp : " + resp);

        try
        {
            fis = new FileInputStream(file);
            FileCopyUtils.copy(fis, out);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally 
        {
            if(fis != null)
            {
                try 
                {
                    fis.close();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }       
        }
        out.flush();
    }
}

学习了MIME-TYPE和Content-Type实现文件下载功能

因此,据我所知,要执行 "Save As" 功能,

  resp.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

我发现您需要设置 "Content-Disposition"。

我按原样设置,但是"Save As" window没有出现。 (当我用 Chrome 打开你的浏览器时)

由于记录了 ContentType,

getContentType >> application/octet-stream;charset=UTF-8

我确认是像上面的日志那样设置的

我设置错误,"Save As" window 没有弹出?

如果你能告诉我哪里出了问题,我将不胜感激。

哦,还有一个问题。

为了测试这些东西,我尝试从 Microsoft Edge 浏览器和 firefox 浏览器下载文件。

对于 Edge,"Save As" window 打开!

而对于 fire fox,"Save As" window 不会出现。 但是,打开检查 window 是否打开或保存文件。

这是因为每个浏览器都有的属性吗?

  1. 为什么 "Save As" window 没有出现在我的逻辑中?
  2. 为什么我在下载文件时会针对每种浏览器类型下载文件 window?

据我所知,Open/Save As 对话是默认情况下发生的。而且我认为,试图强迫任何事情是没有意义的。这是您无法在客户端更改的浏览器设置。

通过'Save As'对话,只能做2件事。

  • 更改下载文件名、类型、扩展名..
  • 选择下载文件夹

在目前的代码中,我们总是可以指定文件名和扩展名(test.txt,test.jpeg,test.xls,...)

response.setHeader("Content-Disposition","attachment; filename=test.txt");

以下程序在每个浏览器中都适用于我自己的设置

@RequestMapping("/downloadFile")
public ModelAndView writeFileContentInResponse(HttpServletResponse response) throws IOException {
 FileInputStream inputStream = new FileInputStream("Fullfilepathto/test.txt"); 
 response.setHeader("Content-Disposition","attachment; filename=test.txt");
        try {
            int c;
            while ((c = inputStream.read()) != -1) {
            response.getWriter().write(c);
            }
        } finally {
            if (inputStream != null) 
                inputStream.close();
                response.getWriter().close();
        }
        return "index";
        }