我正在使用播放框架 2.0 和 Intellij。我试图让用户上传一个文档,然后将其保存为 Oracle 数据库中的 blob 对象。我正在使用 play 的方法从用户那里获取文件。然后在控制器中,我将它发送到模型类,我将文件保存在数据库中。问题是“附件”字段在保存到数据库时始终为空,我不知道为什么。我尝试将文件对象读取为字节字符串,然后将其转换为 blob 对象(我还将附件字段转换为 Blob 类型),但该 blob 对象也是空的。我怎样才能做到这一点?
//控制器代码
public static Result ValidateQuestion() {
Form<Questions> filledForm = questionForm.bindFromRequest();
if (filledForm.hasErrors()) {
return badRequest(createQuestion.render(questionForm,fillCourseList(),fillSemesterList()));
}
else {
Http.MultipartFormData body = request().body().asMultipartFormData();
Http.MultipartFormData.FilePart attachment = body.getFile("attachment");
if(attachment!=null) {
Attachmenttable.create(1, "other", attachment.getFile());
}
return redirect(routes.TeacherController.TeacherDashboard());
}
}
//模型类代码
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "s_attachmenttable")
public int attachment_id;
@Constraints.Required
public int question_id;
@Constraints.Required
public String name;
@Transient
public File attachment;
public Attachmenttable(int spurning_id, String name, File attachment) {
this.spurning_id = spurning_id;
this.nafn = nafn;
this.vidhengi = vidhengi;
}
public static Attachmenttable create(int question_id, String name, File attachment) {
Attachmenttable attachmenttable = new Attachmenttable(question_id, name, attachment);
attachmenttable.save();
return attachmenttable;
}