org.springframework.web.multipart.MultipartFile.getContentType()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(367)

本文整理了Java中org.springframework.web.multipart.MultipartFile.getContentType()方法的一些代码示例,展示了MultipartFile.getContentType()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MultipartFile.getContentType()方法的具体详情如下:
包路径:org.springframework.web.multipart.MultipartFile
类名称:MultipartFile
方法名:getContentType

MultipartFile.getContentType介绍

[英]Return the content type of the file.
[中]返回文件的内容类型。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
public String getMultipartContentType(String paramOrFileName) {
  MultipartFile file = getFile(paramOrFileName);
  if (file != null) {
    return file.getContentType();
  }
  else {
    return null;
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public String getMultipartContentType(String paramOrFileName) {
  MultipartFile file = getFile(paramOrFileName);
  if (file != null) {
    return file.getContentType();
  }
  else {
    return getMultipartParameterContentTypes().get(paramOrFileName);
  }
}

代码示例来源:origin: linlinjava/litemall

@PostMapping("/upload")
public Object upload(@RequestParam("file") MultipartFile file) throws IOException {
  String originalFilename = file.getOriginalFilename();
  String url = storageService.store(file.getInputStream(), file.getSize(), file.getContentType(), originalFilename);
  Map<String, Object> data = new HashMap<>();
  data.put("url", url);
  return ResponseUtil.ok(data);
}

代码示例来源:origin: org.springframework/spring-web

@Override
public String getMultipartContentType(String paramOrFileName) {
  MultipartFile file = getFile(paramOrFileName);
  if (file != null) {
    return file.getContentType();
  }
  else {
    return getMultipartParameterContentTypes().get(paramOrFileName);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public String getMultipartContentType(String paramOrFileName) {
  MultipartFile file = getFile(paramOrFileName);
  if (file != null) {
    return file.getContentType();
  }
  else {
    return null;
  }
}

代码示例来源:origin: linlinjava/litemall

@RequiresPermissions("admin:storage:create")
@RequiresPermissionsDesc(menu={"系统管理" , "对象存储"}, button="上传")
@PostMapping("/create")
public Object create(@RequestParam("file") MultipartFile file) throws IOException {
  String originalFilename = file.getOriginalFilename();
  String url = storageService.store(file.getInputStream(), file.getSize(), file.getContentType(), originalFilename);
  Map<String, Object> data = new HashMap<>();
  data.put("url", url);
  return ResponseUtil.ok(data);
}

代码示例来源:origin: hs-web/hsweb-framework

fileInfo = fileService.saveFile(file.getInputStream(), fileName, file.getContentType(), creator);
} catch (IOException e) {
  throw new BusinessException("save file error", e);

代码示例来源:origin: spring-projects/spring-framework

private void doTestMultipartHttpServletRequest(MultipartHttpServletRequest request) throws IOException {
  Set<String> fileNames = new HashSet<>();
  Iterator<String> fileIter = request.getFileNames();
  while (fileIter.hasNext()) {
    fileNames.add(fileIter.next());
  }
  assertEquals(2, fileNames.size());
  assertTrue(fileNames.contains("file1"));
  assertTrue(fileNames.contains("file2"));
  MultipartFile file1 = request.getFile("file1");
  MultipartFile file2 = request.getFile("file2");
  Map<String, MultipartFile> fileMap = request.getFileMap();
  List<String> fileMapKeys = new LinkedList<>(fileMap.keySet());
  assertEquals(2, fileMapKeys.size());
  assertEquals(file1, fileMap.get("file1"));
  assertEquals(file2, fileMap.get("file2"));
  assertEquals("file1", file1.getName());
  assertEquals("", file1.getOriginalFilename());
  assertNull(file1.getContentType());
  assertTrue(ObjectUtils.nullSafeEquals("myContent1".getBytes(), file1.getBytes()));
  assertTrue(ObjectUtils.nullSafeEquals("myContent1".getBytes(),
    FileCopyUtils.copyToByteArray(file1.getInputStream())));
  assertEquals("file2", file2.getName());
  assertEquals("myOrigFilename", file2.getOriginalFilename());
  assertEquals("text/plain", file2.getContentType());
  assertTrue(ObjectUtils.nullSafeEquals("myContent2".getBytes(), file2.getBytes()));
  assertTrue(ObjectUtils.nullSafeEquals("myContent2".getBytes(),
    FileCopyUtils.copyToByteArray(file2.getInputStream())));
}

代码示例来源:origin: spring-projects/spring-integration

/**
 * Once we depend upon Spring 3.1 as a minimum, this code can be changed to:
 *     this.multipartServletRequest.getMultipartContentType(String paramOrFileName)
 */
public String getMultipartContentType(String paramOrFileName) {
  MultipartFile file = getFile(paramOrFileName);
  if (file != null) {
    return file.getContentType();
  }
  else {
    throw new UnsupportedOperationException("unable to retrieve multipart content-type for parameter");
  }
}

代码示例来源:origin: spring-projects/spring-restdocs

private OperationRequestPart createOperationRequestPart(MultipartFile file)
    throws IOException {
  HttpHeaders partHeaders = new HttpHeaders();
  if (StringUtils.hasText(file.getContentType())) {
    partHeaders.setContentType(MediaType.parseMediaType(file.getContentType()));
  }
  return new OperationRequestPartFactory().create(file.getName(),
      StringUtils.hasText(file.getOriginalFilename())
          ? file.getOriginalFilename() : null,
      file.getBytes(), partHeaders);
}

代码示例来源:origin: spring-projects/spring-integration

@Override
public MultipartFile readMultipartFile(MultipartFile multipartFile) throws IOException {
  File upload = File.createTempFile(this.prefix, this.suffix, this.directory);
  multipartFile.transferTo(upload);
  UploadedMultipartFile uploadedMultipartFile = new UploadedMultipartFile(upload, multipartFile.getSize(),
      multipartFile.getContentType(), multipartFile.getName(), multipartFile.getOriginalFilename());
  if (logger.isDebugEnabled()) {
    logger.debug("copied uploaded file [" + multipartFile.getOriginalFilename() +
        "] to [" + upload.getAbsolutePath() + "]");
  }
  return uploadedMultipartFile;
}

代码示例来源:origin: spring-projects/spring-integration

@Override
public Object readMultipartFile(MultipartFile multipartFile) throws IOException {
  String mpContentType = multipartFile.getContentType();
  if (mpContentType != null && mpContentType.startsWith("text")) {
    MediaType contentType = MediaType.parseMediaType(mpContentType);
    Charset charset = contentType.getCharset();
    if (charset == null) {
      charset = this.defaultCharset;
    }
    return new String(multipartFile.getBytes(), charset.name());
  }
  else {
    return multipartFile.getBytes();
  }
}

代码示例来源:origin: spring-projects/spring-integration

public MultipartFile readMultipartFile(MultipartFile multipartFile) throws IOException {
  return new UploadedMultipartFile(multipartFile.getBytes(),
      multipartFile.getContentType(), multipartFile.getName(), multipartFile.getOriginalFilename());
}

代码示例来源:origin: tomoya92/pybbs

String suffix = "." + file.getContentType().split("/")[1];

代码示例来源:origin: stackoverflow.com

@RequestMapping("/save")
public String saveSkill(@RequestParam(value = "file", required = false) MultipartFile file) {   
    if(!file.getContentType().equalsIgnoreCase("text/html")){
      return "normalProcessing";
    }else{
      return "redirect: /some/page";
    }
}

代码示例来源:origin: xkcoding/spring-boot-demo

@PostMapping("/{id}/file")
  @ApiOperation(value = "文件上传(DONE)")
  public String file(@PathVariable Long id, @RequestParam("file") MultipartFile file) {
    log.info(file.getContentType());
    log.info(file.getName());
    log.info(file.getOriginalFilename());
    return file.getOriginalFilename();
  }
}

代码示例来源:origin: org.activiti.cloud.common/activiti-cloud-services-commons-io

public static FileContent multipartToFileContent(MultipartFile file) throws IOException {
  return new FileContent(file.getOriginalFilename(),
              file.getContentType(),
              toByteArray(file.getInputStream()));
}

代码示例来源:origin: org.springframework.integration/spring-integration-http

@Override
public MultipartFile readMultipartFile(MultipartFile multipartFile) throws IOException {
  File upload = File.createTempFile(this.prefix, this.suffix, this.directory);
  multipartFile.transferTo(upload);
  UploadedMultipartFile uploadedMultipartFile = new UploadedMultipartFile(upload, multipartFile.getSize(),
      multipartFile.getContentType(), multipartFile.getName(), multipartFile.getOriginalFilename());
  if (logger.isDebugEnabled()) {
    logger.debug("copied uploaded file [" + multipartFile.getOriginalFilename() +
        "] to [" + upload.getAbsolutePath() + "]");
  }
  return uploadedMultipartFile;
}

代码示例来源:origin: org.motechproject/motech-cmslite-api

@RequestMapping(value = "/resource/stream/{language}/{name}", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
public void editStreamContent(@PathVariable String language, @PathVariable String name,
               @RequestParam MultipartFile contentFile) throws ContentNotFoundException, CMSLiteException, IOException {
  StreamContent streamContent = cmsLiteService.getStreamContent(language, name);
  try (InputStream inputStream = contentFile.getInputStream()) {
    streamContent.setChecksum(md5Hex(contentFile.getBytes()));
    streamContent.setContentType(contentFile.getContentType());
    streamContent.setInputStream(inputStream);
    cmsLiteService.addContent(streamContent);
  }
}

代码示例来源:origin: joshlong/the-spring-rest-stack

@RequestMapping(method = POST)
HttpEntity<Void> writeUserProfilePhoto(@PathVariable Long user, @RequestParam MultipartFile file) throws Throwable {
  byte bytesForProfilePhoto[] = FileCopyUtils.copyToByteArray(file.getInputStream());
  this.crmService.writeUserProfilePhoto(user, MediaType.parseMediaType(file.getContentType()), bytesForProfilePhoto);
  Resource<User> userResource = this.userResourceAssembler.toResource(crmService.findById(user));
  List<Link> linkCollection = userResource.getLinks();
  Links wrapperOfLinks = new Links(linkCollection);
  HttpHeaders httpHeaders = new HttpHeaders();
  httpHeaders.add("Link", wrapperOfLinks.toString());  // we can't encode the links in the body of the response, so we put them in the "Links:" header.
  httpHeaders.setLocation(URI.create(userResource.getLink("photo").getHref())); // "Location: /users/{userId}/photo"
  return new ResponseEntity<>(httpHeaders, HttpStatus.ACCEPTED);
}

相关文章