@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); } }
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; }
@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); } }
@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); } }
@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"; }
@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"; }
@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"; }
@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"; }
@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"; }
@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"; }
@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"; }
stream.write(uploadfile.getBytes()); stream.close();
@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); } }
/** * 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); } }
@Test(expected = IllegalArgumentException.class) public void setValueAsMultipartFileWithBadBytes() throws Exception { MultipartFile file = mock(MultipartFile.class); given(file.getBytes()).willThrow(new IOException()); editor.setValue(file); }
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()))); }
@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()); }
@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. } }
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); }
public MultipartFile readMultipartFile(MultipartFile multipartFile) throws IOException { return new UploadedMultipartFile(multipartFile.getBytes(), multipartFile.getContentType(), multipartFile.getName(), multipartFile.getOriginalFilename()); }