读取上传的文本文件并插入数据库

时间:2013-11-15 14:34:24

标签: java servlets

我正在使用servlet上传文本文件并读取它,并尝试插入数据库

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        ServletContext context = getServletContext();
        String path = context.getRealPath(file);
        File file = new File(path);
        FileInputStream in = null;
        try {
            in = new FileInputStream(uploadFilePath+File.separator+file.getName());
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            while ((strLine = br.readLine()) != null) {
                System.out.println(strLine);
                decodeAisData(strLine); //it is reading each line but not inserting into database
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

在解码中我编写了数据库插入逻辑,但它没有执行

1 个答案:

答案 0 :(得分:1)

阅读时看起来文件路径不正确,你试试了吗?  in = new FileInputStream(uploadFilePath + File.separator+file1.getName());?或者更好地使用getResourceAsStream()从Classloader获取流,可以在here

中找到更多信息