Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在浏览器中以新选项卡打开文件而不是下载文件?_Java_Spring Mvc_Cross Browser - Fatal编程技术网

Java 在浏览器中以新选项卡打开文件而不是下载文件?

Java 在浏览器中以新选项卡打开文件而不是下载文件?,java,spring-mvc,cross-browser,Java,Spring Mvc,Cross Browser,我有一个控制器: @RequestMapping(method = RequestMethod.POST, params = "action=downloading") public void downloading(HttpServletRequest request, HttpServletResponse response) throws IOException { String dbType = request .getParameter

我有一个控制器:

@RequestMapping(method = RequestMethod.POST, params = "action=downloading")
public void downloading(HttpServletRequest request,
        HttpServletResponse response) throws IOException {

    String dbType = request
            .getParameter(JDBCConnectionUtility.DATABASE);
    String fileName = request.getParameter("fileType");
    String browserVersion = request.getHeader(Constants.BROWSER_TYPE);
    boolean bFlag = (browserVersion.toUpperCase().contains("MSIE 5.5"));

    Utility.downloadFiles(response, response.getOutputStream(), bFlag ,
            fileName);
}
以及实用程序类中的downloadFiles方法定义:

 public static boolean downloadFiles(HttpServletResponse res,
        ServletOutputStream out, boolean bIE55, String fileName) {

    File file = new File(fileName);
    if (bIE55) {
        res.setContentType("application/download; name=\"" + file.getName()
                + "\"");
        res.setHeader("Content-Disposition",
                "anything; filename=\"" + file.getName() + "\";");
    } else {
        res.setContentType("application/octet-st" + "; name=\""
                + file.getName() + "\"");
        res.setHeader("Content-Disposition",
                "anything; filename=\"" + file.getName() + "\";");
    }
        logger.debug("stored the response");
    BufferedInputStream bis = null;
    try {
        bis = new BufferedInputStream(new FileInputStream(file));

        int bytesRead = 0;
        byte[] byteBuff = new byte[1024];
        while ((bytesRead = bis.read(byteBuff)) > 0) {
            out.write(byteBuff, 0, bytesRead);
        }
        out.flush();
    } catch (Exception exc) {
        logger.error(exc.getStackTrace());
        return false;
    } finally {
        closeStream(bis);
    }

        logger.debug("In the download files Exit");
    return true;
}
我的代码段下载所需的日志文件。预期的情况是,所需的日志文件应在浏览器窗口中作为新选项卡打开。如何通过修改代码来实现这一点?

尝试以下更改

  • 要在浏览器中打开而不是下载,请执行以下操作:
  • 发件人:

     res.setHeader("Content-Disposition",
                    "anything; filename=\"" + file.getName() + "\";");
    
    致:

  • 要在新选项卡中打开,请执行以下操作:
  • 添加
    target=“\u blank”
    属性

    如果提交表格

    <form method="post" action="/urlhere"  target="_blank">
    
    
    
    如果是锚定标签

    <a href="/urlhere" target="_blank"/>
    
    
    
    <a href="/urlhere" target="_blank"/>