简体   繁体   中英

Image save, edit, delete to specific folder and save path and other data in database in java (jsp, servlet,mysql)

I want to get image from jsp page save it specific folder which is out of the server container and path of image and other details (ie name, path,etc) store in mysql database, after that user need to able to update the image (delete, add more images,etc). Now I am able to store the image in specific folder and path and other details in database, now am stuck in how to get the actual image from folder to the jsp page as per the path getting from sql database. There is any example which help me regarding that. CODE:

private static final long serialVersionUID = 1L;
public BasicInfoFrmDao dao;
int page = 1;
int recordsPerPage = 5;
public static final String MUNCIPAL_COUNCIL_FORM = "/mc-basic-form-list.jsp";

public BasicFormImageController() {
    dao = new BasicInfoFrmDaoImplementation();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    HttpSession session = request.getSession(false);
    String sessionUserName = (String) session.getAttribute("username");
    int sessionUserId = (Integer) session.getAttribute("user_id");
    System.out.println("session Username = : " + sessionUserName + " & UserId = : " + sessionUserId);

    List<BasicInfoFrm> basicInfoFrms = dao.getAllMuncipalCouncils((page - 1) * recordsPerPage, recordsPerPage,
            sessionUserId);
    for (BasicInfoFrm bc : basicInfoFrms) {
        int i = bc.getMuncipalCouncilId();
        System.out.println("muncipal id : " + i);
    }
    request.setAttribute("mcouncils", basicInfoFrms);
    int noOfRecords = dao.getNoOfRecords();
    int noOfPages = (int) Math.ceil(noOfRecords * 1.0 / recordsPerPage);
    request.setAttribute("noOfPages", noOfPages);
    request.setAttribute("currentPage", page);
    request.setAttribute("alertMsg", "Your information saved successfully...... Thank You!");
    RequestDispatcher requestDispatcher = request.getRequestDispatcher(MUNCIPAL_COUNCIL_FORM);
    requestDispatcher.forward(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    OrgImage orgImage = new OrgImage();
    int orgId = 0;
    int step = 0;

    ArrayList<String> path = new ArrayList<String>();
    // String orgId = request.getParameter("basicInfoFrmId2");
    // System.out.println("org id = "+orgId);
    // ======================================
    // Uploading multiple images at specific folder and path in DB
    // 07-12-2017....

    ServletFileUpload sf = new ServletFileUpload(new DiskFileItemFactory());
    try {
        List<FileItem> multiFiles = sf.parseRequest(request);
        for (FileItem item : multiFiles) {
            if (item.isFormField()) {
                // Process regular form field (input
                // type="text|radio|checkbox|etc", select, etc).
                String fieldName = item.getFieldName();
                System.out.println("text fieldName" + fieldName);
                String fieldValue = item.getString();
                System.err.println("text fieldValue " + fieldValue);
                // ... (do your job here)
                if (fieldName.equalsIgnoreCase("basicInfoFrmId")) {
                    orgId = Integer.parseInt(fieldValue);
                    step = 4;
                    orgImage.setOrgId(orgId);
                }
            } else {
                // Process form file field (input type="file").
                String fieldName = item.getFieldName();
                System.out.println("fieldName " + fieldName);
                String fileName = FilenameUtils.getName(item.getName());
                System.out.println("fileName " + fileName);

                InputStream fileContent = item.getInputStream();
                System.out.println("fileContent " + fileContent);
                // ... (do your job here)
                try {
                    item.write(new File("/myDemoFileFolder/" + item.getName()));
                    path.add("c:/myDemoFileFolder/" + item.getName());
                    // file.mkdirs();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                // file.mkdirs();
                System.out.println("Your file " + item.getName() + " is uploaded successfully...");
            }
            orgImage.setImagePath(path);
            System.out.println("org id = " + orgId);
            System.out.println("step = " + step);

            // send data to server 08-12-2017....
            if (orgId != 0) {
                int lastlyEnteredImgId = dao.addOrgImages(orgImage);
                System.out.println("Lastly entered Basic Information Img Id : " + lastlyEnteredImgId);
            }
        }
    } catch (FileUploadException e) {
        e.printStackTrace();
    }
    // Up to yet.....Uploading multiple images at specific folder
    // and path in DB 07-12-2017....
    // ====================================
    doGet(request, response);
}

I got the proper solution " http://balusc.omnifaces.org/2007/04/imageservlet.html " here. Create the separate servlet and pass image path in and got the image in the jsp page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM