如何比较上传文件中的字符串

时间:2014-04-24 18:20:28

标签: java jsp servlets

此代码应该尝试比较上传文件中的字符串。如果我上传文件并匹配字符串是否存在并显示一些信息。但运行后这不能显示任何内容

的servlet:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WebPageSourceServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    private static final Logger log = Logger
            .getLogger(WebPageSourceServlet.class.getName());

    @SuppressWarnings("resource")
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {

        String phish = req.getParameter("phish");
        PrintWriter out = resp.getWriter();
        resp.setContentType("text/html");
        try {

            String imageurl = "<img src=https://";
            String amb = "@";
            String action = "<form action=http://";
            String frame = "<iframe";
            String script = "<script";
            String popup = "popUp";
            String popup1 = "window.open";
            String actions = "<form action=https://";

            File f = new File(req.getParameter("phish"));
            FileReader fr = new FileReader(f);
            BufferedReader br = new BufferedReader(fr);
            phish = br.readLine();

            while (phish != null) {
                // phish=br.readLine();
                if (phish.equalsIgnoreCase(popup) || phish.equals(amb)
                        || phish.equalsIgnoreCase(popup1)
                        || phish.equalsIgnoreCase(frame)) {
                    out.println("<p>WARNING:This May Cause a Phishing Attacks!!!</p>");
                } else {
                    out.println("<p>INFO:This is Secure Website!!!</p>");
                }
                if (phish.equalsIgnoreCase(script)) {
                    out.println("<p>WARNING:This may Cause a Doubtful and Phishing attacks</p>");
                } else {
                    out.println("<p>This is Phishing</p>");

                }
                if (phish.equalsIgnoreCase(actions)) {
                    out.println("<p>INFO:This is a Secure Website</p>");
                } else {
                    out.println("WARNING:<p>This may cause a Possible to Phishing attacks:</p>");
                    br.close();
                    return;
                }
            }
        } catch (FileNotFoundException f)

        {
            System.out.println(f);
            log.info("file not found");
        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
            log.info("exception occured");
        }

    }
}

jsp文件:

<form action="/UploadServlet" method="post"
              enctype="multipart/form-data">
    <input type="file" name="phish" size="50" /><br />
    <input type="submit" value="Upload File" />
</form>

iam使用jsp文件名是phish来使用req获取参数.so,它如何处理上传的文件。

1 个答案:

答案 0 :(得分:0)

更改

 phish = br.readLine();
 while (phish != null) {

 while ((phish=br.readLine()) != null) {

就目前而言,你不是在阅读循环中的下一行,所以它基本上是一个无限循环。

此外,您需要查看Apache Commons File Upload库的文档。因为File f = new File(req.getParameter("phish"));不起作用。你根本就没有读过这个文件。当您进行文件上传(即enctype="multipart/form-data")时,request.getParameter始终会为所有内容返回null