当前位置: 首页>>代码示例>>Java>>正文


Java MultipartFormData类代码示例

本文整理汇总了Java中play.mvc.Http.MultipartFormData的典型用法代码示例。如果您正苦于以下问题:Java MultipartFormData类的具体用法?Java MultipartFormData怎么用?Java MultipartFormData使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


MultipartFormData类属于play.mvc.Http包,在下文中一共展示了MultipartFormData类的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));
    }
}
 
开发者ID:vn09,项目名称:rental-helper,代码行数:19,代码来源:CreateProfile.java

示例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();
}
 
开发者ID:Arquisoft,项目名称:ObservaTerra42,代码行数:24,代码来源:Application.java

示例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()));	
}
}
 
开发者ID:Arquisoft,项目名称:ObservaTerra42,代码行数:23,代码来源:API.java

示例4: updateFile

import play.mvc.Http.MultipartFormData; //导入依赖的package包/类
@SubjectPresent
public Result updateFile(final String fileID) {
  final MultipartFormData body = request().body().asMultipartFormData();
  if (body == null || body.getFiles().size() != 1) {
    if (request().headers().containsKey("Content-Type")) {
      if (request().body().asRaw() != null) {
        return updateToFile(fileID, request().body().asRaw().asBytes());
      } else if (request().body().asText() != null) {
        return updateToFile(fileID, request().body().asText().getBytes());
      }
    }
    return badRequest("Request must contain a single file to upload.")
        .as("text/plain");
  } else {
    return uploadToFile(fileID, body.getFiles().get(0));
  }
}
 
开发者ID:uq-eresearch,项目名称:aorra,代码行数:18,代码来源:FileStoreController.java

示例5: uploadToFile

import play.mvc.Http.MultipartFormData; //导入依赖的package包/类
protected Result uploadToFile(final String fileID,
    final MultipartFormData.FilePart filePart) {
  return fileBasedResult(fileID, new FileOp() {
    @Override
    public final Result apply(Session session, FileStore.File f)
        throws RepositoryException, FileNotFoundException {
      try {
        final JsonBuilder jb = new JsonBuilder();
        f.update(getMimeType(filePart),
            new FileInputStream(filePart.getFile()));
        session.save();
        Logger.info(String.format(
          "file %s content type %s uploaded to %s by %s",
          filePart.getFilename(), getMimeType(filePart),
          f.getPath(), getUser()));
        return ok(jb.toJsonShallow(f)).as("application/json; charset=utf-8");
      } catch (ItemExistsException iee) {
        return forbidden(iee.getMessage());
      } catch (AccessDeniedException ade) {
        return forbidden(
            "Insufficient permissions to upload files to folder.");
      }
    }
  });
}
 
开发者ID:uq-eresearch,项目名称:aorra,代码行数:26,代码来源:FileStoreController.java

示例6: updateFileContents

import play.mvc.Http.MultipartFormData; //导入依赖的package包/类
private FileStore.File updateFileContents(FileStore.Folder folder,
    MultipartFormData.FilePart filePart) throws RepositoryException {
  try {
    final FileStore.FileOrFolder fof =
        folder.getFileOrFolder(filePart.getFilename());
    final FileStore.File f;
    if (fof == null) {
      f = folder.createFile(filePart.getFilename(),
          getMimeType(filePart),
          new FileInputStream(filePart.getFile()));
    } else if (fof instanceof FileStore.File) {
      throw new ItemExistsException(
          "File already exists. Add a new version instead.");
    } else {
      throw new ItemExistsException("Item exists and is not a file.");
    }
    return f;
  } catch (FileNotFoundException e) {
    throw new RuntimeException(e);
  }
}
 
开发者ID:uq-eresearch,项目名称:aorra,代码行数:22,代码来源:FileStoreController.java

示例7: 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());
    }
}
 
开发者ID:ukwa,项目名称:w3act,代码行数:27,代码来源:TargetController.java

示例8: 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);
}
 
开发者ID:CSCfi,项目名称:exam,代码行数:39,代码来源:AttachmentController.java

示例9: 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);
}
 
开发者ID:CSCfi,项目名称:exam,代码行数:36,代码来源:AttachmentController.java

示例10: 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;
    }
}
 
开发者ID:vn09,项目名称:rental-helper,代码行数:28,代码来源:EditProfile.java

示例11: 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;
}
 
开发者ID:theAgileFactory,项目名称:app-framework,代码行数:35,代码来源:FileAttachmentHelper.java

示例12: 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;

}
 
开发者ID:theAgileFactory,项目名称:app-framework,代码行数:20,代码来源:FileAttachmentHelper.java

示例13: 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);
}
 
开发者ID:socia-platform,项目名称:htwplus,代码行数:40,代码来源:ProfileController.java

示例14: 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"));	
	  }
}
 
开发者ID:jembi,项目名称:woc,代码行数:42,代码来源:Application.java

示例15: 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));
}
 
开发者ID:saikatgomes,项目名称:CS784-Data_Integration,代码行数:38,代码来源:RuleController.java


注:本文中的play.mvc.Http.MultipartFormData类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。