我编写了一个简单的JSP代码,将PDF文件上传到Mysql BLOB数据库
我的HTML代码是
<form method="post" action="uploadfile.jsp" 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>
我的JSP代码是
<%@page import="java.io.*" %>
<%@page import="java.sql.*" %>
<%
response.setContentType("text/html;charset=UTF-8");
InputStream inputStream = null;
Connection conn=null;
PreparedStatement st=null;
ResultSet rs=null;
String title=(request.getParameter("title"));
Part filePart = request.getPart("file_uploaded");
if (filePart != null)
{
out.println(filePart.getName());
out.println(filePart.getSize());
out.println(filePart.getContentType());
inputStream = filePart.getInputStream();
}
try
{
String idTemp="1";
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/fileupload","root","password");
String sql = "INSERT INTO files (id, title, file) values (?, ?, ?)";
st = conn.prepareStatement(sql);
st.setString(1, idTemp);
st.setString(2, title);
if (inputStream != null)
{
st.setBinaryStream(3, inputStream, (int) filePart.getSize());
}
int row = st.executeUpdate();
if (row > 0)
{
out.println("File uploaded!!!");
conn.close();
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.include(request, response);
}
else
{
out.println("Couldn't upload your file!!!");
conn.close();
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.include(request, response);
}
}catch(Exception e){out.print(e);}
%>
执行此代码后。我收到错误“ java.sql.SQLException:没有为参数3指定值”
此代码有什么问题。我的数据库表为空,我无法上传任何pdf数据
我的数据库SQL是
create table files(id int(4),title varchar(20),file mediumblob,primary key(id)) ENGINE=InnoDB DEFAULT CHARSET=latin1;
答案 0 :(得分:1)
您在本节中遇到错误。
String sql = "INSERT INTO files (id, title, file) values (?, ?, ?)";
st = conn.prepareStatement(sql);
st.setString(1, idTemp);
st.setString(2, title);
在这里您需要添加另一个参数。您仅添加了两个参数,但是在查询中您通过values (?, ?, ?)
声明了三个参数
答案 1 :(得分:0)
我刚刚创建了一个Servlet代码,现在可以正常工作了
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
@WebServlet("/uploadServlet")
@MultipartConfig(maxFileSize = 16177215)
public class UploadFileController extends HttpServlet
{
/**
*
*/
private static final long serialVersionUID = 1L;
@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;
Connection conn=null;
PreparedStatement st=null;
ResultSet rs=null;
String idTemp=request.getParameter("id");
String title=(request.getParameter("title"));
String site=request.getParameter("site");
Part filePart = request.getPart("file_uploaded");
HttpSession hs=request.getSession(true);
hs.setAttribute("site", site);
if (filePart != null)
{
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
inputStream = filePart.getInputStream();
}
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database","root","password");
String sql = "INSERT INTO files (id, title,file) values (?, ?, ?)";
st = conn.prepareStatement(sql);
st.setString(1, idTemp);
st.setString(2, title);
//st.setString(3, site);
if (inputStream != null)
{
st.setBinaryStream(3, inputStream, (int) filePart.getSize());
}
int row = st.executeUpdate();
if (row > 0)
{
String fileuploaded="fileuploaded";
out.println("<script> alert("+fileuploaded+")</script>");
conn.close();
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.include(request, response);
}
else
{
out.println("Couldn't upload your file!!!");
conn.close();
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.include(request, response);
}
}catch(Exception e){out.println(e);}
}
}