我正在尝试将一些pdf文件上传到mysql数据库。目前,此代码可以正常工作,但是我不知道如何从上传的文件中获取一些元数据。例如,如何获取信息的作者,出版年份以及如何自动获取文件名,而无需手动输入...
UploadFileController
package servlets;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import database.Db_Connection;
import java.io.PrintWriter;
import java.util.Random;
import javax.servlet.RequestDispatcher;
@WebServlet("/UploadFileController")
@MultipartConfig(maxFileSize = 16177215)
public class UploadFileController extends HttpServlet
{
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
InputStream inputStream = null;
Random rand = new Random();
int n = rand.nextInt(9999) + 1;
String idTemp=(String.valueOf(n));
String title=(request.getParameter("title"));
Part filePart = request.getPart("file_uploaded");
if (filePart != null)
{
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
inputStream = filePart.getInputStream();
}
try
{
Db_Connection dbconn=new Db_Connection();
Connection conn= dbconn.Connection();
String sql = "INSERT INTO files (id, title, file) values (?, ?, ?)";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, idTemp);
statement.setString(2, title);
if (inputStream != null)
{
statement.setBinaryStream(3, inputStream, (int) filePart.getSize());
}
int row = statement.executeUpdate();
if (row > 0)
{
out.println("File uploaded!!!");
conn.close();
RequestDispatcher rs = request.getRequestDispatcher("upload_form.jsp");
rs.include(request, response);
}
else
{
out.println("Couldn't upload your file!!!");
conn.close();
RequestDispatcher rs = request.getRequestDispatcher("upload_form.jsp");
rs.include(request, response);
}
}catch(Exception e){e.printStackTrace();}
}
}
upload_form.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="database.Db_Connection"%>
<%
String id = request.getParameter("id");
String driver = "com.mysql.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String database = "e-book";
String userid = "root";
String password = "root";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Upload File</title>
</head>
<body>
<form method="post" action="UploadFileController" enctype="multipart/form-data">
<center>
<table border="1" width="25%" cellpadding="5">
<thead>
<th colspan="3">Upload File</th>
</thead>
<tbody>
<tr>
<td>Title : </td>
<td><input type="text" name="title" size="30"></td>
</tr>
<tr>
<td>Choose File : </td>
<td><input type="file" name="file_uploaded" /></td>
</tr>
<tr>
<td colspan="3"><center><input type="submit" value="Upload"></center></td>
</tr>
</tbody>
</table>
</center>
</form>
<br><br>
<table border="1" width="25%" cellpadding="5">
<thead>
<th colspan="3">Uploaded Files</th>
</thead>
<tbody>
<tr>
<td><center><b>Id</b></center><td><center><b>Title</b></center></td><td><center><b>File</b></center></td>
</tr>
<%
try
{
Db_Connection dbconn=new Db_Connection();
Connection myconnection= dbconn.Connection();
String sqlString = "SELECT * FROM files";
Statement myStatement = myconnection.createStatement();
ResultSet rs=myStatement.executeQuery(sqlString);
if(!rs.isBeforeFirst())
{
%>
<tr>
<td colspan="3"><center><%out.print("No Files!"); %></center></td>
</tr>
<%
}
while(rs.next())
{
%>
<tr>
<td><center><%out.print(rs.getString("id")); %></center></td>
<td><center><%out.print(rs.getString("title")); %></center></td>
<td><center><a href="view_file.jsp?id=<%out.print(rs.getString("id"));%>">View</a></center></td>
</tr>
<%
}
%>
</tbody>
</table>
<%
rs.close();
myStatement.close();
myconnection.close();
}catch(Exception e){e.printStackTrace();}
%>
</body>
</html>