0

I m having trouble to set value for entity bean. the problem is that when i populate form file will be upload but i need file path to store in data base. In my bean i have used setter of employee entity to set file url but And I think the code is enough to set file path for database but data is storing on database leaving employeePicture as null..

        @Named
        @RequestScoped
        public class EmployeeAddController {

            private Employees employees;
            private String fileNameForDataBase;
            private Part file;

            @Inject
            private EmployeeUpdateService updateService;

            @PostConstruct
            public void init() {
                employees = new Employees();

            }

            public Employees getEmployees() {
                return employees;
            }

            public void setEmployees(Employees employees) {
                this.employees = employees;
            }

            public String getFileNameForDataBase() {


                return fileNameForDataBase;
            }

            public void setFileNameForDataBase(String fileNameForDataBase) {

                this.fileNameForDataBase = fileNameForDataBase;
            }

            public Part getFile() {
                return file;
            }

            public void setFile(Part file) {
                this.file = file;
            }

            public void upload() throws IOException {

                ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance()
                        .getExternalContext().getContext();
                String realPath = ctx.getRealPath("/");
                int random =(int) (Math.random() * 10000 + 1);
                String fileString= realPath + File.separator + "resources/image/employee"+random+".jpg";
                employees.setEmployeePicture(fileString);
                try (InputStream input = file.getInputStream()) {
                    Files.copy(input, new File(fileString).toPath());
                }
            }

            public String addEmployee() {
                try {


                    this.updateService.add(employees);

                    return "index?faces-redirect=true";
                } catch (Exception e) {
                    return null;
                }
            }

        }

in My jsf page

 "<div class="form-group">
                                        <h:outputText value=" Employee Picture" class="col-sm-3 control-label"/>
                                        <div class="col-sm-9">
                                            <h:inputFile value="#{employeeAddController.file}">
                                                <f:ajax listener="#{employeeAddController.upload()}"/>
                                            </h:inputFile>
                                            <h:outputText value="#{employeeAddController.fileNameForDataBase}"/>

                                        </div>
                                        <div>
                                            <h:message for="fileUpload" class="text-primary"/>
                                        </div>
                                    </div>"***strong text***
Bibek Shakya
  • 1,233
  • 2
  • 23
  • 45
  • Possible duplicate of [How to upload file using JSF 2.2 ? Where is the saved File?](http://stackoverflow.com/questions/27677397/how-to-upload-file-using-jsf-2-2-hinputfile-where-is-the-saved-file) – BalusC May 30 '16 at 17:23
  • i m not asking for upload file in server i m asking for not getting path that i used for uploading as a string value to save in database – Bibek Shakya May 30 '16 at 17:28
  • Your way of saving the uploaded file is wrong. The duplciate explains exactly how to achieve that properly. – BalusC May 30 '16 at 17:31
  • can you please tell me why employees.setEmployeePicture(fileString); this line of code is not working – Bibek Shakya May 30 '16 at 17:36
  • That's answered by http://stackoverflow.com/q/7031885 – BalusC May 30 '16 at 17:37

0 Answers0