How to store image on disk (using servelets) but p

2019-09-16 19:47发布

问题:

I want to store image in a folder on my disk but want to save its path in database.And then retrieve or display the same image using the path stored in database.

Here is my code:

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 class

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 class

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;
    }
}