如何从xml中获取一些值

时间:2014-03-12 16:29:28

标签: xml database servlets

我正在通过html上传xml文档并将其传递给servlet。在servlet中,我将提取一些xml字段并将其与文件(xml)一起发送到数据库(我在数据库中创建了一个表(mysql))。

有什么建议吗?

我的index.html

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
  <head>
    <meta http-equiv="Con`tent-Type" content="text/html; charset=iso-8859-1">
     <meta name="GENERATOR" content="Mozilla/4.61 [en] (WinNT; I) [Netscape]">
      <title> </title>
  </head>
  <body>    
      <form method="POST" action="upload" enctype="multipart/form-data" >
          File:
             <input type="file" name="file" id="file" /> <br/>    
             <input type="submit" value="Upload" name="upload" id="upload" />
       </form>
  </body>

我的InsertXMLServlet.java(Servlet)

    package com;

    import java.io.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.annotation.MultipartConfig;
    import javax.servlet.http.Part;

/**
 * Servlet implementation class InsertXMLServlet
 */
@WebServlet(description = "InsertXMLServlet", urlPatterns = { "/upload" })
public class InsertXMLServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public InsertXMLServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        int rows = 0;

        // File xmlFile = new File("D:Name.xml");

        final Part filePart = request.getPart("file");

        OutputStream out = null;
        InputStream filecontent = null;
        final PrintWriter writer = response.getWriter();

        try {

            filecontent = filePart.getInputStream();

            int read = 0;
            final byte[] bytes = new byte[1024];

            while ((read = filecontent.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            writer.println("New file " + fileName + " created at " + path);

        } catch (FileNotFoundException fne) {

            writer.println("<br/> ERROR: " + fne.getMessage());

        } finally {
            if (out != null) {
                out.close();
            }
            if (filecontent != null) {
                filecontent.close();
            }
            if (writer != null) {
                writer.close();
            }
        }

        PreparedStatement ps = null;
        Connection con = null;
        String url = "jdbc:mysql://@inblrl0746/test";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "password";
        String INSERT_SQL = "INSERT INTO ATTACHMENT(FILENAME,CONTENT) values(?, ?)";

        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url, userName, password);
            ps = con.prepareStatement(INSERT_SQL);
            ps.setString(1, xmlFile.getName());
            ps.setBinaryStream(2, new FileInputStream(xmlFile));
            rows = ps.executeUpdate();
            return rows;

        }

        catch (SQLException e) {
            e.printStackTrace();
            // ps.close();
            // con.close();
        } catch (FileNotFoundException e) {

            e.printStackTrace();// CLOSE ps and con;
        } finally {
            ps.close();
            con.close();

        }
        return rows;

    }

}

0 个答案:

没有答案