English 中文(简体)
我如何写到从一个服务器过滤器上的文件,并在Eclipse读取。
原标题:How do I write to a file from a servlet-filter and read it in Eclipse?

我想从我的过滤器上写到档案,然后能够在Eclipse读到,以确保我能正确写给它。

该法典编纂并实行罚款,但我不知道我可以读到哪里,或者我甚至写任何东西。

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;

    System.out.println("filter invoked");

    InputStream in = null;
    OutputStream out = null;
    String inPath = context.getRealPath("/WEB-INF/template.txt");
    String outPath = context.getRealPath("/WEB-INF/output.txt");
    in = new FileInputStream(inPath);
    out = new FileOutputStream(outPath);

    OutputStreamWriter outstream = new OutputStreamWriter(out);
    outstream.write("hello");


    // pass the request along the filter chain
    chain.doFilter(request, response);
}

文档模板.txt和输出.txt都在WEB-INF目录中。 我已经核实,从档案中读取的作品是有效的,但我可以核实这些文件的写本。 我每次都写出产出。 档案没有变化。

我对网上申请环境中的档案的书写有何了解?

最佳回答
问题回答

不要忘记把它归结为最后尝试。

OutputStreamWriter outstream = new OutputStreamWriter(out);
try {
    outstream.write("hello");
} finally {
    outstream.close();
}

你们可以使用下文中的内容,让大家能够利用资源处理上游问题。

@Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
            throws IOException, ServletException {
        // TODO Auto-generated method stub
        HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;

        HttpServletResponse response = (HttpServletResponse) servletResponse;
        System.out.println(httpRequest.getRequestURI());
        System.out.println(httpRequest.getRequestURI().toLowerCase().endsWith("/v2/api-docs"));
        ByteArrayPrinter pw = new ByteArrayPrinter();
        // here i create a response wrapper
        HttpServletResponse wrappedResp = new HttpServletResponseWrapper(response) {
            @Override
            public PrintWriter getWriter() {
                return pw.getWriter();
            }

            @Override
            public ServletOutputStream getOutputStream() {
                return pw.getStream();
            }

        };
        System.out.println("before chaingin");
        // i get the response data from the stream
        chain.doFilter(httpRequest, wrappedResp);
        byte[] bytes = pw.toByteArray();
        String respBody = new String(bytes);
        System.out.println("in if" + respBody);
        if (httpRequest.getParameter("group") != null) {



            byte[] newByte = pw.toByteArray();
            String newString = new String(newByte);
            System.out.println("new string:" + newString);
            // i make a modification in the stream
            String s = newString.replaceAll("basePath", "Vijayy");
            System.out.println("printing s" + s);

            // here i try to write to a file with the modification
            try (FileOutputStream fos = new FileOutputStream(
                    new File(System.getProperty("user.dir") + "/static/openAPI.json"))) {
                System.out.println("comin in if");
                fos.write(s.getBytes());


                FileCopyUtils.copy(
                        new FileInputStream(new File(System.getProperty("user.dir") + "/static/openAPI.json")),
                        response.getOutputStream());

            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            }
            // FileCopyUtils.copy(, out);

            // IOUtils.copy(new ByteArrayInputStream(newString.getBytes()), response.getOutputStream());

        } else {
            chain.doFilter(httpRequest, wrappedResp);
            response.setHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE);
            response.setContentLength(respBody.length());
            response.getOutputStream().write(respBody.getBytes());
        }

         return;
    }

Byte Arrayprinter是为印刷Stream制作供我使用的一个包裹一。 同样,在你的案件中不需要考虑这一点。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签