我有一个控制器:
@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);
}
Utility类中的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;
}
我的代码段会下载所需的日志文件。预期的情况是所需的日志文件应作为浏览器窗口中的新选项卡打开。如何通过修改代码来实现这一目标?
答案 0 :(得分:2)
尝试以下更改,
- 要在浏览器中打开而不是下载:
醇>
来自:
res.setHeader("Content-Disposition",
"inline; filename=\"" + file.getName() + "\";");
要:
target="_blank"
- 要在新标签页中打开:
醇>
添加<form method="post" action="/urlhere" target="_blank">
属性
如果是表单提交
<a href="/urlhere" target="_blank"/>
如果是锚标记
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/erdogan/PycharmProjects/CNN/tens/model/research/object_detection/object_detection_tutorial.py
/Users/erdogan/PycharmProjects/CNN/tens/model/research/object_detection/utils/visualization_utils.py:25: UserWarning:
This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.
The backend was *originally* set to 'MacOSX' by the following code:
File "/Users/erdogan/PycharmProjects/CNN/tens/model/research/object_detection/object_detection_tutorial.py", line 22, in <module>
from matplotlib import pyplot as plt
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/pyplot.py", line 71, in <module>
from matplotlib.backends import pylab_setup
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/backends/__init__.py", line 16, in <module>
line for line in traceback.format_stack()
import matplotlib; matplotlib.use('Agg') # pylint: disable=multiple-statements