我正在使用play framework 2.0和Intellij。我试图让用户上传文档,然后将其保存为Oracle数据库中的blob对象。 我正在使用play的方法从用户那里获取文件。然后在控制器中,我将它发送到模型类,在那里我将文件保存在数据库中。问题是保存到数据库时“附件”字段始终为空,我不知道原因。 我尝试将文件对象读取为一个字节字符串,然后我将其转换为blob对象(我还将附件字段转换为Blob类型)但该blob对象也为null。我怎么能这样做?
// CONTROLLER CODE
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;
}