如何从xml中获取一些值
当我通过html上传xml文档并将其传递给servlet时。在servlet中,我必须提取一些xml字段并将其与文件(xml)一起发送到数据库(我在数据库(mysql)中创建了一个表) 有什么建议吗 My index.html如何从xml中获取一些值,xml,database,servlets,Xml,Database,Servlets,当我通过html上传xml文档并将其传递给servlet时。在servlet中,我必须提取一些xml字段并将其与文件(xml)一起发送到数据库(我在数据库(mysql)中创建了一个表) 有什么建议吗 My index.html 文件: My InsertXMLServlet.java(Servlet) package-com; 导入java.io.*; 导入java.util.logging.Level; 导入java.util.logging.Logger; 导入javax.servl
文件:
My InsertXMLServlet.java(Servlet)
package-com;
导入java.io.*;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.servlet.ServletException;
导入javax.servlet.annotation.WebServlet;
导入javax.servlet.http.HttpServlet;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入javax.servlet.annotation.MultipartConfig;
导入javax.servlet.http.Part;
/**
*Servlet实现类InsertXMLServlet
*/
@WebServlet(description=“InsertXMLServlet”,urlPatterns={”/upload“})
公共类InsertXMLServlet扩展了HttpServlet{
私有静态最终长serialVersionUID=1L;
公共InsertXMLServlet(){
超级();
//TODO自动生成的构造函数存根
}
受保护的无效数据集(HttpServletRequest请求,
HttpServletResponse响应)引发ServletException,IOException{
//TODO自动生成的方法存根
}
受保护的void doPost(HttpServletRequest请求,
HttpServletResponse响应)引发ServletException,IOException{
int行=0;
//File xmlFile=新文件(“D:Name.xml”);
最终部分filePart=request.getPart(“文件”);
OutputStream out=null;
InputStream filecontent=null;
最终PrintWriter=response.getWriter();
试一试{
filecontent=filePart.getInputStream();
int read=0;
最终字节[]字节=新字节[1024];
而((read=filecontent.read(bytes))!=-1){
输出。写入(字节,0,读取);
}
writer.println(“新文件”+文件名+”,创建于“+路径);
}捕获(FileNotFoundException fne){
writer.println(“
错误:+fne.getMessage());
}最后{
if(out!=null){
out.close();
}
if(filecontent!=null){
filecontent.close();
}
if(writer!=null){
writer.close();
}
}
PreparedStatement ps=null;
连接con=null;
String url=“jdbc:mysql://@inblrl0746/test”;
String driver=“com.mysql.jdbc.driver”;
字符串userName=“root”;
字符串password=“password”;
String INSERT_SQL=“插入附件(文件名、内容)值(?,)”;
试一试{
Class.forName(驱动程序);
con=DriverManager.getConnection(url、用户名、密码);
ps=con.prepareStatement(插入SQL);
ps.setString(1,xmlFile.getName());
ps.setBinaryStream(2,新文件输入流(xmlFile));
行=ps.executeUpdate();
返回行;
}
捕获(SQLE异常){
e、 printStackTrace();
//ps.close();
//con.close();
}catch(filenotfounde异常){
e、 printStackTrace();//关闭ps和con;
}最后{
ps.close();
con.close();
}
返回行;
}
}
有人能建议如何从servlet中的xml中获取一些字段吗?有人能帮我找到解决方案吗??
<!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>
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;
}
}