在play框架中使用@ManyToOne关系上传文件

时间:2014-09-04 09:32:22

标签: java javascript playframework-2.2

我有两个实体:学生和信息,他们通过@ManytoOne关系链接。实体Info用于处理文件上传和在数据库中存储上传文件。我能够在数据库中保存文件路径。但不能使用Student_id。我想用学生ID保存文件。并以表格格式显示上传的文件。感谢任何示例建议。我是Web开发的新手。感谢由@ManytoOne关系链接的两个模型实体。型号信息如下。

 package models;
    @Entity
    public class Info extends Model {
        public static Info findById(Long id) {
            return find.byId(id);
        }
        @Id
        public Long id;
        public String picture;
        @ManyToOne(cascade = CascadeType.PERSIST)
        private Student student; 
        public static Model.Finder<Long, Info> find =
                new Model.Finder<Long, Info>(Long.class, Info.class);
        public Info(){
            // Left empty
        }
        public Info(Student  student ,Long id){
            this.customer=customer;
            this.id=id;
        }
        public String toString() {
            return String.format("%s" ,id);
        }
        public void setPicture(String picture) {
            this.picture = picture;
        }
        public Long getId() {
            return id;
        }
        public void setId(Long id) {
            this.id = id;
        }
        public Student getStudent() {
            return student;
        }
        public void setStudent(Student student) {
            this.student = student;
        }
        public String getPicture() {
            return picture;
        }
    } 
                                                                                           another student model                                                                                 



 package models;

        @Entity
        public class Info extends Model{
            private static List<Info> products;
            @Id
            public Long id;
            public String name;
         @OneToMany(mappedBy="student")
         private List<Info> info;
            public static Finder<Long, Student> find = new Finder<Long, Student>(Long.class, Student.class);

            public Student() {
            }
                    public Student(String btn){
                        this.btn = btn;
                        this.info = new ArrayList<Info>();
                    }
            public String toString() {
                return String.format("%s - %s" ,id, business_address);
            }
            public static Student findById(Long id) {
                return find.where().eq("id", id).findUnique();
            }
                    public List<Info> getInfo() {
                        return info;
                    }
                    public void setInfo(List<Info> info) {
                        this.info = info;
                    }
        } 

     and InfoCon Controller is as follows 





 package controllers;
    import java.io.File;

    public class InfoCon extends  Controller {

        private static final Form<Info> infoForm = Form.form(Info.class);

        public static Result save() {

            Form<Info> boundForm = infoForm.bindFromRequest();
            Info info = boundForm.get();

            Http.MultipartFormData body = request().body().asMultipartFormData();
            Http.MultipartFormData.FilePart picture = body.getFile("picture");
            if (picture != null) {
                String fileName = picture.getFilename();
                String contentType = picture.getContentType();
                File file = picture.getFile();
                File newFile = new File("C:\\Folder1\\store\\"+ "_" + fileName);
                file.renameTo(newFile); //here you are moving file to new directory
                info.setPicture(newFile.getPath());
                if (info.id == null) {
                    info.save();
                }
                return ok("File uploaded");
            } else {
                flash("error", "Missing file");
                return redirect(routes.Application.index());
            }
        }
        public static Result info(Long id) {
            final Student student = Student.findById(id);
           Info wk = new Info();
            Form<Info> filledForm = infoForm.fill(wk);
            return ok(info.render(filledForm));
        }
    }

0 个答案:

没有答案