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

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

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

MultipartFile.getBytes介绍

[英]Return the contents of the file as an array of bytes.
[中]以字节数组的形式返回文件的内容。

代码示例

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

@Override
public void setValue(Object value) {
  if (value instanceof MultipartFile) {
    MultipartFile multipartFile = (MultipartFile) value;
    try {
      super.setValue(this.charsetName != null ?
          new String(multipartFile.getBytes(), this.charsetName) :
          new String(multipartFile.getBytes()));
    }
    catch (IOException ex) {
      throw new IllegalArgumentException("Cannot read contents of multipart file", ex);
    }
  }
  else {
    super.setValue(value);
  }
}

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

public File convert(MultipartFile file)
{    
  File convFile = new File(file.getOriginalFilename());
  convFile.createNewFile(); 
  FileOutputStream fos = new FileOutputStream(convFile); 
  fos.write(file.getBytes());
  fos.close(); 
  return convFile;
}

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

@Override
public void setValue(@Nullable Object value) {
  if (value instanceof MultipartFile) {
    MultipartFile multipartFile = (MultipartFile) value;
    try {
      super.setValue(multipartFile.getBytes());
    }
    catch (IOException ex) {
      throw new IllegalArgumentException("Cannot read contents of multipart file", ex);
    }
  }
  else if (value instanceof byte[]) {
    super.setValue(value);
  }
  else {
    super.setValue(value != null ? value.toString().getBytes() : null);
  }
}

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

@Override
public void setValue(Object value) {
  if (value instanceof MultipartFile) {
    MultipartFile multipartFile = (MultipartFile) value;
    try {
      super.setValue(this.charsetName != null ?
          new String(multipartFile.getBytes(), this.charsetName) :
          new String(multipartFile.getBytes()));
    }
    catch (IOException ex) {
      throw new IllegalArgumentException("Cannot read contents of multipart file", ex);
    }
  }
  else {
    super.setValue(value);
  }
}

代码示例来源:origin: ityouknow/spring-boot-examples

@PostMapping("/upload") // //new annotation since 4.3
public String singleFileUpload(@RequestParam("file") MultipartFile file,
                RedirectAttributes redirectAttributes) {
  if (file.isEmpty()) {
    redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
    return "redirect:uploadStatus";
  }
  try {
    // Get the file and save it somewhere
    byte[] bytes = file.getBytes();
    Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
    Files.write(path, bytes);
    redirectAttributes.addFlashAttribute("message",
        "You successfully uploaded '" + file.getOriginalFilename() + "'");
  } catch (IOException e) {
    e.printStackTrace();
  }
  return "redirect:/uploadStatus";
}

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

@RequestMapping(value = "/multipartfile", method = RequestMethod.POST)
public String processMultipartFile(@RequestParam(required = false) MultipartFile file,
    @RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
  if (file != null) {
    model.addAttribute("fileContent", file.getBytes());
  }
  if (json != null) {
    model.addAttribute("jsonContent", json);
  }
  return "redirect:/index";
}

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

@RequestMapping(value = "/optionalfile", method = RequestMethod.POST)
public String processOptionalFile(@RequestParam Optional<MultipartFile> file,
    @RequestPart Map<String, String> json, Model model) throws IOException {
  if (file.isPresent()) {
    model.addAttribute("fileContent", file.get().getBytes());
  }
  model.addAttribute("jsonContent", json);
  return "redirect:/index";
}

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

@RequestMapping(value = "/optionalfilearray", method = RequestMethod.POST)
public String processOptionalFileArray(@RequestParam Optional<MultipartFile[]> file,
    @RequestPart Map<String, String> json, Model model) throws IOException {
  if (file.isPresent()) {
    byte[] content = file.get()[0].getBytes();
    Assert.assertArrayEquals(content, file.get()[1].getBytes());
    model.addAttribute("fileContent", content);
  }
  model.addAttribute("jsonContent", json);
  return "redirect:/index";
}

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

@RequestMapping(value = "/optionalfilelist", method = RequestMethod.POST)
public String processOptionalFileList(@RequestParam Optional<List<MultipartFile>> file,
    @RequestPart Map<String, String> json, Model model) throws IOException {
  if (file.isPresent()) {
    byte[] content = file.get().get(0).getBytes();
    Assert.assertArrayEquals(content, file.get().get(1).getBytes());
    model.addAttribute("fileContent", content);
  }
  model.addAttribute("jsonContent", json);
  return "redirect:/index";
}

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

@RequestMapping(value = "/multipartfilearray", method = RequestMethod.POST)
public String processMultipartFileArray(@RequestParam(required = false) MultipartFile[] file,
    @RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
  if (file != null && file.length > 0) {
    byte[] content = file[0].getBytes();
    Assert.assertArrayEquals(content, file[1].getBytes());
    model.addAttribute("fileContent", content);
  }
  if (json != null) {
    model.addAttribute("jsonContent", json);
  }
  return "redirect:/index";
}

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

@RequestMapping(value = "/multipartfilelist", method = RequestMethod.POST)
public String processMultipartFileList(@RequestParam(required = false) List<MultipartFile> file,
    @RequestPart(required = false) Map<String, String> json, Model model) throws IOException {
  if (file != null && !file.isEmpty()) {
    byte[] content = file.get(0).getBytes();
    Assert.assertArrayEquals(content, file.get(1).getBytes());
    model.addAttribute("fileContent", content);
  }
  if (json != null) {
    model.addAttribute("jsonContent", json);
  }
  return "redirect:/index";
}

代码示例来源:origin: netgloo/spring-boot-samples

stream.write(uploadfile.getBytes());
stream.close();

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

@Override
public void setValue(@Nullable Object value) {
  if (value instanceof MultipartFile) {
    MultipartFile multipartFile = (MultipartFile) value;
    try {
      super.setValue(multipartFile.getBytes());
    }
    catch (IOException ex) {
      throw new IllegalArgumentException("Cannot read contents of multipart file", ex);
    }
  }
  else if (value instanceof byte[]) {
    super.setValue(value);
  }
  else {
    super.setValue(value != null ? value.toString().getBytes() : null);
  }
}

代码示例来源:origin: kaaproject/kaa

/**
 * Gets the file content.
 *
 * @param file the file
 * @return the file content
 * @throws KaaAdminServiceException the kaa admin service exception
 */
protected byte[] getFileContent(MultipartFile file) throws KaaAdminServiceException {
 if (!file.isEmpty()) {
  LOG.debug("Uploading file with name '{}'", file.getOriginalFilename());
  try {
   return file.getBytes();
  } catch (IOException ex) {
   throw Utils.handleException(ex);
  }
 } else {
  LOG.error("No file found in post request!");
  throw new KaaAdminServiceException(
    "No file found in post request!",
    ServiceErrorCode.FILE_NOT_FOUND);
 }
}

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

@Test(expected = IllegalArgumentException.class)
public void setValueAsMultipartFileWithBadBytes() throws Exception {
  MultipartFile file = mock(MultipartFile.class);
  given(file.getBytes()).willThrow(new IOException());
  editor.setValue(file);
}

代码示例来源: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-framework

@Test
public void setValueAsMultipartFile() throws Exception {
  String expectedValue = "That is comforting to know";
  MultipartFile file = mock(MultipartFile.class);
  given(file.getBytes()).willReturn(expectedValue.getBytes());
  editor.setValue(file);
  assertEquals(expectedValue, editor.getAsText());
}

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

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String handleFormUpload( 
  @RequestParam("file") MultipartFile file) throws IOException{
if (!file.isEmpty()) {
 BufferedImage src = ImageIO.read(new ByteArrayInputStream(file.getBytes()));
 File destination = new File("File directory with file name") // something like C:/Users/tom/Documents/nameBasedOnSomeId.png
 ImageIO.write(src, "png", destination);
 //Save the id you have used to create the file name in the DB. You can retrieve the image in future with the ID.
 }  
}

代码示例来源: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

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

相关文章