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

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

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

MultipartFile.getOriginalFilename介绍

[英]Return the original filename in the client's filesystem.

This may contain path information depending on the browser used, but it typically will not with any other than Opera.
[中]返回客户端文件系统中的原始文件名。
这可能包含路径信息,具体取决于所使用的浏览器,但除了Opera之外,其他浏览器通常不会包含路径信息。

代码示例

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

@Override
public String getFilename() {
  return this.multipartFile.getOriginalFilename();
}

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

@Override
public String getFilename() {
  return this.multipartFile.getOriginalFilename();
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

private static String normalizeFileExtension(MultipartFile file) {
  int index = file.getOriginalFilename().lastIndexOf(".");
  return file.getOriginalFilename().substring(0, index + 1) + file.getOriginalFilename().substring(index + 1, file.getOriginalFilename().length()).toLowerCase();
}

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

public File multipartToFile(MultipartFile multipart) throws IllegalStateException, IOException 
{
    File convFile = new File( multipart.getOriginalFilename());
    multipart.transferTo(convFile);
    return convFile;
}

代码示例来源:origin: forezp/SpringBootLearning

@Override
public void store(MultipartFile file) {
  try {
    if (file.isEmpty()) {
      throw new StorageException("Failed to store empty file " + file.getOriginalFilename());
    }
    Files.copy(file.getInputStream(), this.rootLocation.resolve(file.getOriginalFilename()));
  } catch (IOException e) {
    throw new StorageException("Failed to store file " + file.getOriginalFilename(), e);
  }
}

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

@PostMapping("/")
public String handleFileUpload(@RequestParam("file") MultipartFile file,
                RedirectAttributes redirectAttributes) {
  storageService.store(file);
  redirectAttributes.addFlashAttribute("message",
      "You successfully uploaded " + file.getOriginalFilename() + "!");
  return "redirect:/";
}

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

@PostMapping(value = "/upload-static")
@ApiOperation(value = "上传静态文件", notes = "上传后响应结果的result字段为文件的访问地址")
@Authorize(action = "static", description = "上传静态文件")
public ResponseMessage<String> uploadStatic(@RequestPart("file") MultipartFile file) throws IOException {
  if (file.isEmpty()) {
    return ResponseMessage.ok();
  }
  return ResponseMessage.ok(fileService.saveStaticFile(file.getInputStream(), file.getOriginalFilename()));
}

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

@RequestMapping(value = "/spr13319", method = POST, consumes = "multipart/form-data")
  public ResponseEntity<Void> create(@RequestPart("file") MultipartFile multipartFile) {
    assertEquals("élève.txt", multipartFile.getOriginalFilename());
    return ResponseEntity.ok().build();
  }
}

代码示例来源:origin: ctripcorp/apollo

List<String> fileNameSplit = Splitter.on(".").splitToList(file.getOriginalFilename());
if (fileNameSplit.size() <= 1) {
 throw new BadRequestException("The file format is invalid.");

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

@RequestMapping(value = "/test", method = POST, consumes = {"multipart/mixed", "multipart/form-data"})
public ResponseEntity<Object> create(@RequestPart(name = "json-data") TestData testData,
    @RequestPart("file-data") Optional<MultipartFile> file,
    @RequestPart(name = "empty-data", required = false) TestData emptyData,
    @RequestPart(name = "iso-8859-1-data") byte[] iso88591Data) {
  Assert.assertArrayEquals(new byte[]{(byte) 0xC4}, iso88591Data);
  String url = "http://localhost:8080/test/" + testData.getName() + "/" + file.get().getOriginalFilename();
  HttpHeaders headers = new HttpHeaders();
  headers.setLocation(URI.create(url));
  return new ResponseEntity<>(headers, HttpStatus.CREATED);
}

代码示例来源:origin: gocd/gocd

protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
  CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
  MultipartHttpServletRequest httpServletRequest = multipartResolver.resolveMultipart(request);
  Map<String, MultipartFile> map = httpServletRequest.getFileMap();
  MultipartFile multipartFile = map.values().iterator().next();
  receivedFiles.add(multipartFile.getOriginalFilename());
}

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

/**
 * ueditor上传文件
 *
 * @return 上传结果
 * @throws IOException 文件上传错误
 */
@RequestMapping(method = RequestMethod.POST, consumes = "multipart/form-data")
@ApiOperation("上传文件")
public String upload(@RequestParam(value = "upfile", required = false) MultipartFile file) throws IOException {
  String fileName = file.getOriginalFilename();
  String suffix = FileType.getSuffixByFilename(fileName);
  String path = fileService.saveStaticFile(file.getInputStream(), System.currentTimeMillis() + suffix);
  State state = new BaseState(true);
  state.putInfo("size", file.getSize());
  state.putInfo("title",fileName);
  state.putInfo("url", path);
  state.putInfo("type", suffix);
  state.putInfo("original",fileName);
  return state.toJSONString();
}

代码示例来源: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  // SPR-13319
public void filenameRfc5987() throws Exception {
  String disposition = "form-data; name=\"file\"; filename*=\"UTF-8''foo-%c3%a4-%e2%82%ac.html\"";
  StandardMultipartHttpServletRequest request = requestWithPart("file", disposition, "");
  MultipartFile multipartFile = request.getFile("file");
  assertNotNull(multipartFile);
  assertEquals("foo-ä-€.html", multipartFile.getOriginalFilename());
}

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

@Test
public void filename() throws Exception {
  String disposition = "form-data; name=\"file\"; filename=\"myFile.txt\"";
  StandardMultipartHttpServletRequest request = requestWithPart("file", disposition, "");
  MultipartFile multipartFile = request.getFile("file");
  assertNotNull(multipartFile);
  assertEquals("myFile.txt", multipartFile.getOriginalFilename());
}

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

@Test  // SPR-15205
public void filenameRfc2047() throws Exception {
  String disposition = "form-data; name=\"file\"; filename=\"=?UTF-8?Q?Declara=C3=A7=C3=A3o.pdf?=\"";
  StandardMultipartHttpServletRequest request = requestWithPart("file", disposition, "");
  MultipartFile multipartFile = request.getFile("file");
  assertNotNull(multipartFile);
  assertEquals("Declaração.pdf", multipartFile.getOriginalFilename());
}

相关文章