簡體   English   中英

圖像保存,編輯,刪除到特定文件夾並將路徑和其他數據保存在Java中的數據庫中(jsp,servlet,mysql)

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

我想從jsp頁面獲取圖像,將其保存在服務器容器之外的特定文件夾中,並將圖像的路徑以及其他詳細信息(例如名稱,路徑等)存儲在mysql數據庫中,之后用戶需要能夠更新圖像(刪除,添加更多圖像等)。 現在,我能夠將圖像存儲在特定的文件夾和路徑以及數據庫中的其他詳細信息中,現在陷入了如何按照從sql數據庫獲取的路徑將實際圖像從文件夾獲取到jsp頁面的問題。 有任何例子可以幫助我。 碼:

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

我在這里獲得了正確的解決方案“ http://balusc.omnifaces.org/2007/04/imageservlet.html ”。 創建單獨的servlet並傳入圖像路徑,並在jsp頁面中獲得圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM