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"/>