我想将图像存储在磁盘上的文件夹中,但希望将其路径保存在数据库中。然后使用存储在数据库中的路径检索或显示相同的图像。
这是我的代码:
JSP
<form action="<%=request.getContextPath()%>/imageServelet" method="post">
ImagePath: <input type="file" name="fileUpload" id="fileUpload"></input>
<br/><br/>
Image Id: <input type="text" name="id" id="id"></input>
<input type="submit" value="upload"></input>
</form>
的Servlet
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
int imageid = Integer.parseInt(request.getParameter("id"));
String imagepath = request.getParameter("fileUpload");
imageVO vo = new imageVO();
vo.setImageId(imageid);
vo.setImagePath(imagepath);
imageDAO dao = new imageDAO();
try {
dao.insert(vo);
} catch (Exception e) {
e.printStackTrace();
}
}
ImageDAO类
public class imageDAO {
String connectionURL = "jdbc:oracle:thin:@172.26.132.40:1521:ORCL";
String user = "a97core";
String password = "a97core";
public void insert(imageVO vo) throws Exception {
String query = "INSERT INTO Image_670456 VALUES (' "
+ vo.getImageId() + "','" + vo.getImagePath() + "')";
Class.forName("oracle.jdbc.OracleDriver");
Connection con = DriverManager.getConnection(connectionURL, user,
password);
Statement st = con.createStatement();
st.executeQuery(query);
}
}
ImageVO类
public class imageVO {
private int imageId;
private String imagePath;
public int getImageId() {
return imageId;
}
public String getImagePath() {
return imagePath;
}
public void setImageId(int id) {
this.imageId = id;
}
public void setImagePath(String str) {
this.imagePath = str;
}
}