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

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

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

MultipartFile.getInputStream介绍

[英]Return an InputStream to read the contents of the file from.

The user is responsible for closing the returned stream.
[中]返回InputStream以从中读取文件内容。
用户负责关闭返回的流。

代码示例

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

/**
 * This implementation throws IllegalStateException if attempting to
 * read the underlying stream multiple times.
 */
@Override
public InputStream getInputStream() throws IOException, IllegalStateException {
  return this.multipartFile.getInputStream();
}

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

/**
 * This implementation throws IllegalStateException if attempting to
 * read the underlying stream multiple times.
 */
@Override
public InputStream getInputStream() throws IOException, IllegalStateException {
  return this.multipartFile.getInputStream();
}

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

/**
 * Transfer the received file to the given destination file.
 * <p>The default implementation simply copies the file input stream.
 * @since 5.1
 * @see #getInputStream()
 * @see #transferTo(File)
 */
default void transferTo(Path dest) throws IOException, IllegalStateException {
  FileCopyUtils.copy(getInputStream(), Files.newOutputStream(dest));
}

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

private boolean saveFile(int convertedAttempt, File artifact, MultipartFile multipartFile, boolean shouldUnzip) throws IOException {
  InputStream inputStream = null;
  boolean success;
  try {
    inputStream = multipartFile.getInputStream();
    success = artifactsService.saveFile(artifact, inputStream, shouldUnzip, convertedAttempt);
  } finally {
    IOUtils.closeQuietly(inputStream);
  }
  return success;
}

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

/**
 * Transfer the received file to the given destination file.
 * <p>The default implementation simply copies the file input stream.
 * @since 5.1
 * @see #getInputStream()
 * @see #transferTo(File)
 */
default void transferTo(Path dest) throws IOException, IllegalStateException {
  FileCopyUtils.copy(getInputStream(), Files.newOutputStream(dest));
}

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

@Transactional("blTransactionManagerAssetStorageInfo")
@Override
public void createStaticAssetStorageFromFile(MultipartFile file, StaticAsset staticAsset) throws IOException {
  createStaticAssetStorage(file.getInputStream(), staticAsset);
}

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

@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: gocd/gocd

private boolean updateChecksumFile(MultipartHttpServletRequest request, JobIdentifier jobIdentifier, String filePath) throws IOException, IllegalArtifactLocationException {
  MultipartFile checksumMultipartFile = getChecksumFile(request);
  if (checksumMultipartFile != null) {
    String checksumFilePath = String.format("%s/%s/%s", artifactsService.findArtifactRoot(jobIdentifier), ArtifactLogUtil.CRUISE_OUTPUT_FOLDER, ArtifactLogUtil.MD5_CHECKSUM_FILENAME);
    File checksumFile = artifactsService.getArtifactLocation(checksumFilePath);
    synchronized (checksumFilePath.intern()) {
      return artifactsService.saveOrAppendFile(checksumFile, checksumMultipartFile.getInputStream());
    }
  } else {
    LOGGER.warn("[Artifacts Upload] Checksum file not uploaded for artifact at path '{}'", filePath);
  }
  return true;
}

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

@Override
@Transactional(TransactionUtils.DEFAULT_TRANSACTION_MANAGER)
public StaticAsset createStaticAssetFromFile(MultipartFile file, Map<String, String> properties) {
  try {
    return createStaticAsset(file.getInputStream(), normalizeFileExtension(file), file.getSize(), properties);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

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

@Override
public Blob createBlob(MultipartFile uploadedFile) throws IOException {
  return createBlob(uploadedFile.getInputStream(), uploadedFile.getSize());
}

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

@Override
public InputStream getBody() throws IOException {
  if (this.multipartRequest instanceof StandardMultipartHttpServletRequest) {
    try {
      return this.multipartRequest.getPart(this.partName).getInputStream();
    }
    catch (Exception ex) {
      throw new MultipartException("Could not parse multipart servlet request", ex);
    }
  }
  else {
    MultipartFile file = this.multipartRequest.getFile(this.partName);
    if (file != null) {
      return file.getInputStream();
    }
    else {
      String paramValue = this.multipartRequest.getParameter(this.partName);
      return new ByteArrayInputStream(paramValue.getBytes(determineCharset()));
    }
  }
}

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

/**
 * 部署流程资源
 * 加载ZIP文件中的流程
 */
@PostMapping(value = "/deploy")
@ApiOperation("上传流程定义文件并部署流程")
@Authorize(action = "deploy")
public ResponseMessage<Deployment> deploy(@RequestPart(value = "file") MultipartFile file) throws IOException {
  // 获取上传的文件名
  String fileName = file.getOriginalFilename();
  // 得到输入流(字节流)对象
  InputStream fileInputStream = file.getInputStream();
  // 文件的扩展名
  String extension = FilenameUtils.getExtension(fileName);
  // zip或者bar类型的文件用ZipInputStream方式部署
  DeploymentBuilder deployment = repositoryService.createDeployment();
  if ("zip".equals(extension) || "bar".equals(extension)) {
    ZipInputStream zip = new ZipInputStream(fileInputStream);
    deployment.addZipInputStream(zip);
  } else {
    // 其他类型的文件直接部署
    deployment.addInputStream(fileName, fileInputStream);
  }
  Deployment result = deployment.deploy();
  return ResponseMessage.ok(result);
}

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

model.setNamespaceId(namespaceId);
String configText;
try(InputStream in = file.getInputStream()){
 configText = ConfigToFileUtils.fileToString(in);
}catch (IOException e) {

代码示例来源: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: org.springframework/spring-web

@Override
public InputStream getBody() throws IOException {
  if (this.multipartRequest instanceof StandardMultipartHttpServletRequest) {
    try {
      return this.multipartRequest.getPart(this.partName).getInputStream();
    }
    catch (Exception ex) {
      throw new MultipartException("Could not parse multipart servlet request", ex);
    }
  }
  else {
    MultipartFile file = this.multipartRequest.getFile(this.partName);
    if (file != null) {
      return file.getInputStream();
    }
    else {
      String paramValue = this.multipartRequest.getParameter(this.partName);
      return new ByteArrayInputStream(paramValue.getBytes(determineCharset()));
    }
  }
}

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

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

相关文章