本文整理汇总了Java中play.mvc.Http.MultipartFormData.getFile方法的典型用法代码示例。如果您正苦于以下问题:Java MultipartFormData.getFile方法的具体用法?Java MultipartFormData.getFile怎么用?Java MultipartFormData.getFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类play.mvc.Http.MultipartFormData
的用法示例。
在下文中一共展示了MultipartFormData.getFile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: submit
import play.mvc.Http.MultipartFormData; //导入方法依赖的package包/类
public Result submit() {
User user = User.findByEmail(session().get("email"));
Form<UserProfile> filledForm = profileForm.bindFromRequest();
if (filledForm.hasErrors()) {
return badRequest(createprofile.render(filledForm));
} else {
MultipartFormData body = request().body().asMultipartFormData();
FilePart picture = body.getFile("image");
UserProfile newProfile = filledForm.get();
newProfile.image = picture.getFile();
String filePath = "public/user_pictures/"+ user.email + ".png";
newProfile.saveImage(picture.getFile(), filePath);
newProfile.userId = user.id;
newProfile.save();
return ok(viewprofile.render(user, newProfile));
}
}
示例2: guardarArchivo
import play.mvc.Http.MultipartFormData; //导入方法依赖的package包/类
public static Result guardarArchivo() {
MultipartFormData body = request().body().asMultipartFormData();
FilePart fichero = body.getFile("archivo");
String nombreFichero = fichero.getFilename();
int indice = nombreFichero.lastIndexOf(".");
String type = nombreFichero.substring(indice + 1, nombreFichero.length());
if(indice != -1)
nombreFichero = nombreFichero.substring(0,indice);
String tipo = fichero.getContentType();
File file = fichero.getFile();
File newFile = new File("public/data/" + nombreFichero + "."+ type);
try {
Files.copy(file, newFile);
} catch (IOException e) {
e.printStackTrace();
}
return index();
}
示例3: uploadExcel
import play.mvc.Http.MultipartFormData; //导入方法依赖的package包/类
public static Result uploadExcel() {
try {
MultipartFormData body = request().body().asMultipartFormData();
FilePart excel = body.getFile("excel");
if (excel != null) {
File file = excel.getFile();
ExcelReader reader = new ExcelReader();
List<Observation> obsList = reader.read(new FileInputStream(file));
for (Observation obs: obsList) {
obs.save();
}
Logger.info("Excel file uploaded with " + obsList.size() + " observations");
return redirect(routes.Application.index());
} else {
Logger.error("Missing file to upload ");
return redirect(routes.Application.index());
}
}
catch (IOException e) {
return(badRequest(Messages.get("read.excel.error") + "." + e.getLocalizedMessage()));
}
}
示例4: uploadExcel
import play.mvc.Http.MultipartFormData; //导入方法依赖的package包/类
@Security.Authenticated(SecuredController.class)
public static Result uploadExcel() {
MultipartFormData body = request().body().asMultipartFormData();
FilePart picture = body.getFile("excelFile");
if(picture != null) {
String fileName = picture.getFilename();
String contentType = picture.getContentType();
File file = picture.getFile();
Logger.info("Uploaded " + fileName);
try {
excelParser(file);
flash("success", "File " + fileName + " uploaded");
}
catch(Throwable e) {
flash("error", "File " + fileName + " parse errors:");
flash("error_log", e.getMessage());
e.printStackTrace();
}
return redirect(routes.TargetController.upload());
}
else {
Logger.info("Upload failed ");
flash("error", "Missing file");
return redirect(routes.TargetController.upload());
}
}
示例5: addAttachmentToQuestion
import play.mvc.Http.MultipartFormData; //导入方法依赖的package包/类
@Restrict({@Group("TEACHER"), @Group("ADMIN")})
public Result addAttachmentToQuestion() {
MultipartFormData<File> body = request().body().asMultipartFormData();
FilePart<File> filePart = body.getFile("file");
if (filePart == null) {
return notFound();
}
File file = filePart.getFile();
if (file.length() > AppUtil.getMaxFileSize()) {
return forbidden("sitnet_file_too_large");
}
Map<String, String[]> m = body.asFormUrlEncoded();
Long qid = Long.parseLong(m.get("questionId")[0]);
Question question = Ebean.find(Question.class)
.fetch("examSectionQuestions.examSection.exam.parent")
.where()
.idEq(qid)
.findUnique();
if (question == null) {
return notFound();
}
String newFilePath;
try {
newFilePath = copyFile(file, "question", qid.toString());
} catch (IOException e) {
return internalServerError("sitnet_error_creating_attachment");
}
// Remove existing one if found
removePrevious(question);
Attachment attachment = createNew(filePart, newFilePath);
question.setAttachment(attachment);
question.save();
return ok(attachment);
}
示例6: addAttachmentToExam
import play.mvc.Http.MultipartFormData; //导入方法依赖的package包/类
@Restrict({@Group("TEACHER"), @Group("ADMIN")})
public Result addAttachmentToExam() {
MultipartFormData<File> body = request().body().asMultipartFormData();
FilePart<File> filePart = body.getFile("file");
if (filePart == null) {
return notFound();
}
File file = filePart.getFile();
if (file.length() > AppUtil.getMaxFileSize()) {
return forbidden("sitnet_file_too_large");
}
Map<String, String[]> m = body.asFormUrlEncoded();
Long eid = Long.parseLong(m.get("examId")[0]);
Exam exam = Ebean.find(Exam.class, eid);
if (exam == null) {
return notFound();
}
User user = getLoggedUser();
if (!user.hasRole(Role.Name.ADMIN.toString(), getSession()) && !exam.isOwnedOrCreatedBy(user)) {
return forbidden("sitnet_error_access_forbidden");
}
String newFilePath;
try {
newFilePath = copyFile(file, "exam", eid.toString());
} catch (IOException e) {
return internalServerError("sitnet_error_creating_attachment");
}
// Delete existing if exists
removePrevious(exam);
Attachment attachment = createNew(filePart, newFilePath);
exam.setAttachment(attachment);
exam.save();
return ok(attachment);
}
示例7: submit
import play.mvc.Http.MultipartFormData; //导入方法依赖的package包/类
public Result submit() {
User user = User.findByEmail(session().get("email"));
Form<UserProfile> filledForm = profileForm.bindFromRequest();
if (filledForm.hasErrors()) {
return badRequest(editprofile.render(user, profileForm));
} else {
MultipartFormData body = request().body().asMultipartFormData();
FilePart picture = null;
if (body != null && body.getFile("image") != null) {
picture = body.getFile("image");
}
UserProfile profile = null;
if (user != null) {
profile = UserProfile.findByUserId(user.id);
} else {
profile = UserProfile.findByUserId(filledForm.get().userId);
}
profile.set(filledForm.get());
if (picture != null && picture.getFile() != null) {
profile.image = picture.getFile();
String filePath = "public/user_pictures/" + user.email + ".png";
profile.saveImage(picture.getFile(), filePath);
}
profile.save();
return GO_VIEW;
}
}
示例8: hasFileField
import play.mvc.Http.MultipartFormData; //导入方法依赖的package包/类
/**
* Return true if the specified form contains a valid file field.
*
* @param fieldName
* the field name
*
* @return a boolean
*/
public static boolean hasFileField(String fieldName) {
boolean r = false;
MultipartFormData body = Controller.request().body().asMultipartFormData();
if (body != null) {
FileType fileType = getFileType(fieldName);
String fileFieldName = getFileInputName(fieldName, fileType);
switch (fileType) {
case UPLOAD:
if (body.getFile(fileFieldName) != null) {
r = true;
}
break;
case URL:
if (body.asFormUrlEncoded().get(fileFieldName)[0] != null && !body.asFormUrlEncoded().get(fileFieldName)[0].equals("")) {
r = true;
}
break;
}
}
return r;
}
示例9: getFilePart
import play.mvc.Http.MultipartFormData; //导入方法依赖的package包/类
/**
* Get the file part of the attachment for UPLOAD type.
*
* @param fieldName
* the field name
*/
public static FilePart getFilePart(String fieldName) {
FileType fileType = getFileType(fieldName);
if (fileType.equals(FileType.UPLOAD)) {
MultipartFormData body = Controller.request().body().asMultipartFormData();
FilePart filePart = body.getFile(getFileInputName(fieldName, fileType));
return filePart;
}
return null;
}
示例10: createTempAvatar
import play.mvc.Http.MultipartFormData; //导入方法依赖的package包/类
/**
* Handles the upload of the temporary avatar image
*
* @param id
* @return
*/
public Result createTempAvatar(Long id) {
Account account = accountManager.findById(id);
if (account == null) {
return notFound();
}
ObjectNode result = Json.newObject();
MultipartFormData body = request().body().asMultipartFormData();
if (body == null) {
result.put("error", "No file attached");
return badRequest(result);
}
MultipartFormData.FilePart avatar = body.getFile("avatarimage");
if (avatar == null) {
result.put("error", "No file with key 'avatarimage'");
return badRequest(result);
}
try {
avatarManager.setTempAvatar(avatar, account.id);
} catch (ValidationException e) {
result.put("error", e.getMessage());
return badRequest(result);
}
result.put("success", getTempAvatar(id).toString());
return ok(result);
}
示例11: uploadFile
import play.mvc.Http.MultipartFormData; //导入方法依赖的package包/类
/**
* Handles the file upload.
* @throws OWLOntologyCreationException
* @throws InterruptedException
*/
public static Result uploadFile() throws OWLOntologyCreationException, InterruptedException {
Form<Ontology> ontologyForm = form(Ontology.class);
MultipartFormData body = request().body().asMultipartFormData();
FilePart ontologyFile = body.getFile("ontology");
if (ontologyFile != null) {
String fileName = ontologyFile.getFilename();
String contentType = ontologyFile.getContentType();
// ontology.get
File file = ontologyFile.getFile();
try{
loadOntology(file.getPath());
}
catch(UnparsableOntologyException ex){
return ok(index.render("Not a valid Ontology File"));
}
//Initiate the reasoner to classify ontology
if (BeeOntologyfile.exists())
{
reasoner.precomputeInferences(InferenceType.CLASS_HIERARCHY);
//System.out.println("suppp");
loadBeeCharacteristics();
}
Ontology ontology = new Ontology(KeyDescriptionArraylist, null, null);
return ok(view.render(ontology.features));
} else {
flash("error", "Missing file");
return ok(index.render("File Not Found"));
}
}
示例12: importFeatures
import play.mvc.Http.MultipartFormData; //导入方法依赖的package包/类
public static Result importFeatures(String name){
DynamicForm form = form().bindFromRequest();
Logger.info("PARAMETERS : " + form.data().toString());
String table1Name = form.get("table1_name");
String table2Name = form.get("table2_name");
MultipartFormData body = request().body().asMultipartFormData();
FilePart fp = body.getFile("csv_file_path");
if (fp != null) {
String fileName = fp.getFilename();
String contentType = fp.getContentType();
Logger.info("fileName: " + fileName + ", contentType: " + contentType);
File file = fp.getFile();
Project project = ProjectDao.open(name);
try{
Table table1 = TableDao.open(name, table1Name);
Table table2 = TableDao.open(name, table2Name);
List<Feature> features = RuleDao.importFeaturesFromCSVWithHeader(project,
table1, table2, file.getAbsolutePath());
// save the features - this automatically updates and saves the project
System.out.println(features);
System.out.println(name);
RuleDao.save(name, features);
ProjectController.statusMessage = "Successfully imported " + features.size() + " features.";
}
catch(IOException ioe){
flash("error", ioe.getMessage());
ProjectController.statusMessage = "Error: " + ioe.getMessage();
}
} else {
flash("error", "Missing file");
ProjectController.statusMessage = "Error: Missing file";
}
return redirect(controllers.project.routes.ProjectController.showProject(name));
}
示例13: importRules
import play.mvc.Http.MultipartFormData; //导入方法依赖的package包/类
public static Result importRules(String name){
DynamicForm form = form().bindFromRequest();
Logger.info("PARAMETERS : " + form.data().toString());
String table1Name = form.get("table1_name");
String table2Name = form.get("table2_name");
MultipartFormData body = request().body().asMultipartFormData();
FilePart fp = body.getFile("csv_file_path");
if (fp != null) {
String fileName = fp.getFilename();
String contentType = fp.getContentType();
Logger.info("fileName: " + fileName + ", contentType: " + contentType);
File file = fp.getFile();
Project project = ProjectDao.open(name);
try{
List<Rule> rules = RuleDao.importRulesFromCSVWithHeader(project,
table1Name, table2Name, file.getAbsolutePath());
// save the features - this automatically updates and saves the project
RuleDao.saveRules(name, rules);
ProjectController.statusMessage = "Successfully imported " + rules.size() + " rules.";
}
catch(IOException ioe){
flash("error", ioe.getMessage());
ProjectController.statusMessage = "Error: " + ioe.getMessage();
}
} else {
flash("error", "Missing file");
ProjectController.statusMessage = "Error: Missing file";
}
return redirect(controllers.project.routes.ProjectController.showProject(name));
}
示例14: importMatchers
import play.mvc.Http.MultipartFormData; //导入方法依赖的package包/类
public static Result importMatchers(String name){
DynamicForm form = form().bindFromRequest();
Logger.info("PARAMETERS : " + form.data().toString());
String table1Name = form.get("table1_name");
String table2Name = form.get("table2_name");
MultipartFormData body = request().body().asMultipartFormData();
FilePart fp = body.getFile("csv_file_path");
if (fp != null) {
String fileName = fp.getFilename();
String contentType = fp.getContentType();
Logger.info("fileName: " + fileName + ", contentType: " + contentType);
File file = fp.getFile();
Project project = ProjectDao.open(name);
try{
List<Matcher> matchers = RuleDao.importMatchersFromCSVWithHeader(project,
table1Name, table2Name, file.getAbsolutePath());
// save the features - this automatically updates and saves the project
RuleDao.saveMatchers(name, matchers);
ProjectController.statusMessage = "Successfully imported " + matchers.size() + " matchers.";
}
catch(IOException ioe){
flash("error", ioe.getMessage());
ProjectController.statusMessage = "Error: " + ioe.getMessage();
}
} else {
flash("error", "Missing file");
ProjectController.statusMessage = "Error: Missing file";
}
return redirect(controllers.project.routes.ProjectController.showProject(name));
}
示例15: handleFileUploadForm
import play.mvc.Http.MultipartFormData; //导入方法依赖的package包/类
@BodyParser.Of (value = BodyParser.MultipartFormData.class, maxLength = 2 * 1024 * 1024)
public static Result handleFileUploadForm () {
final MultipartFormData body = request ().body ().asMultipartFormData ();
final FilePart uploadFile = body.getFile ("file");
if (uploadFile == null) {
return ok (uploadFileForm.render (null));
}
final String content = handleFileUpload (uploadFile.getFile ());
return ok (uploadFileForm.render (content));
}