当前情况:我正在尝试创建一个(portlet),其中应包含指向我们公司所有用户映射的公共网络驱动器上存储的JSF app
excel 文件(xls
, )的链接。主要目标是统一对这些文件的访问,并将工作保存给用户在 G 驱动器上的某处搜索报告。我希望它清楚..?xlt
G:
我正在使用以下 servlet 打开文件。问题是,它不仅被打开,而且被浏览器下载,然后打开:
@WebServlet(name="fileHandler", urlPatterns={"/fileHandler/*"})
public class FileServlet extends HttpServlet
{
private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.
private String filePath;
public void init() throws ServletException {
this.filePath = "c:\\Export";
System.out.println("fileServlet initialized: " + this.filePath);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
{
String requestedFile = request.getPathInfo();
File file = new File(filePath, URLDecoder.decode(requestedFile, "UTF-8"));
String contentType = getServletContext().getMimeType(file.getName());
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType(contentType);
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
close(output);
close(input);
}
}
private static void close(Closeable resource) {
if (resource != null) resource.close();
}
}
如何启动适当的应用程序(例如Excel
,Word
等)单击链接(带有绝对文件路径)并在其原始位置打开文件?
更新:我正在尝试使用<a>
标签:
<a href="/G:/file.xls">File</a> // various "/" "\" "\\" combinations
<a href="file:///G:/file.xls">File</a>
但它不起作用:
type Status report
message /G:/file.xls
description The requested resource is not available.