图像保存,编辑,删除到特定文件夹,并在java中保存数据库中的路径和其他数据(jsp,servlet,mysql)

时间:2018-03-15 12:43:26

标签: java mysql spring jsp servlets

我想从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);
}

1 个答案:

答案 0 :(得分:0)

我得到了正确的解决方案&#34; http://balusc.omnifaces.org/2007/04/imageservlet.html&#34;这里。 创建单独的servlet并传入图像路径并在jsp页面中获取图像。