从src / main / resource文件夹下载文件

时间:2017-09-13 11:14:24

标签: java jsp servlets io

我有一个maven父项目--->车辆。在那之下有两个项目:汽车,自行车。 在汽车项目下 - > src / main / resource文件夹中有一个文件abc.txt。在自行车项目中,我有一个servlet类。从这里我下载abc.txt文件。所以代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            String filename = "abc.txt";
            String filepath = "";

            response.setContentType("APPLICATION/OCTET-STREAM");
            response.setHeader("Content-Disposition", "attachment; filename=\""
                    + filename + "\"");

            // use inline if you want to view the content in browser, helpful for
            // pdf file
            // response.setHeader("Content-Disposition","inline; filename=\"" +
            // filename + "\"");
            FileInputStream fileInputStream = new FileInputStream(filepath
                    + filename);

            int i;
            while ((i = fileInputStream.read()) != -1) {
                out.write(i);
            }
            fileInputStream.close();
            out.close();
        }
}

我需要传递给filePath变量的值是什么?

servlet完整的doGet方法代码:

FilterRow

0 个答案:

没有答案