0
我正在嘗試製作一個servlet將blob格式的圖片上傳到數據庫中,但我無法嘗試創建@WebServlet註釋。有問題在jsp中使用WebServlet註釋
當我提交我的表單時,它說沒有找到資源。
studentdashboard.jsp
<form class="form-inline" action="changedp" method="post" enctype="multipart/form-data">
<div class="col-md-4">
<div class="form-group">
<input class="btn" type="file" name="dp" id="dp">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<input class="btn btn-primary" type="submit" value="Upload File">
</div>
</div>
</form>
changedp.java
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Part;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet(name = "changedp",urlPatterns = {"changedp"})
@MultipartConfig(maxFileSize = 16177215) // upload file's size up to 16MB
public class changedp extends HttpServlet {
// database connection settings
private String dbURL = "jdbc:mysql://localhost/cll";
private String dbUser = "root";
private String dbPass = "root";
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
InputStream inputStream = null; // input stream of the upload file
// obtains the upload file part in this multipart request
Part filePart = request.getPart("dp");
if (filePart != null) {
// prints out some information for debugging
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
// obtains input stream of the upload file
inputStream = filePart.getInputStream();
}
Connection conn = null; // connection to the database
String message = null; // message will be sent back to client
try {
// connects to the database
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
HttpSession session=request.getSession();
// constructs SQL statement
String sql = "UPDATE student_login_tabl SET image=? where `sid` = '"+session.getAttribute("sid")+"'";
PreparedStatement statement = conn.prepareStatement(sql);
if (inputStream != null) {
// fetches input stream of the upload file for the blob column
statement.setBlob(1, inputStream);
}
// sends the statement to the database server
int row = statement.executeUpdate();
if (row > 0) {
message = "File uploaded and saved into database";
}
} catch (SQLException ex) {
message = "ERROR: " + ex.getMessage();
ex.printStackTrace();
} finally {
if (conn != null) {
// closes the database connection
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
}
也許顯示確切的錯誤信息 –