0

I want to save image from input into webapp folder.

I have tried to use the solution below. But it only saves into target folder.

How to save into webapp or copy from target to webapp?

public static void saveImage(HttpServletRequest request) {
        try {
            ServletFileUpload sf = new ServletFileUpload(new DiskFileItemFactory());
            List<FileItem> fileItems = sf.parseRequest(request);
            for (FileItem item: fileItems) {
                if (!item.isFormField()) {
                    String absolutePath = request.getServletContext().getRealPath("/public/images/product/");
                    System.out.println(absolutePath);
                    String savePath = absolutePath + File.separator;
                    File file = new File(savePath+ item.getName());
                    if(!file.exists()){
                        item.write(file);
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
LeeHari
  • 21
  • 7

0 Answers0