首页 > 解决方案 > 将“java.lang.String”转换为所需类型“javax.servlet.http.Part”

问题描述

我有一个功能uploadFile()

public String uploadFile(Part filePart, final String bucketName) throws IOException {
  DateTimeFormatter dtf = DateTimeFormat.forPattern("-YYYY-MM-dd-HHmmssSSS");
  DateTime dt = DateTime.now(DateTimeZone.UTC);
  String dtString = dt.toString(dtf);
  final String fileName = filePart.getSubmittedFileName() + dtString;

  // The InputStream is closed by default, so we don't need to close it here
  // Read InputStream into a ByteArrayOutputStream.
  InputStream is = filePart.getInputStream();
  ByteArrayOutputStream os = new ByteArrayOutputStream();
  byte[] readBuf = new byte[4096];
  while (is.available() > 0) {
    int bytesRead = is.read(readBuf);
    os.write(readBuf, 0, bytesRead);
  }

  // Convert ByteArrayOutputStream into byte[]
  BlobInfo blobInfo =
      storage.create(
          BlobInfo
              .newBuilder(bucketName, fileName)
              // Modify access list to allow all users with link to read file
              .setAcl(new ArrayList<>(Arrays.asList(Acl.of(User.ofAllUsers(), Role.READER))))
              .build(),
          os.toByteArray());
  // return the public download link
  return blobInfo.getMediaLink();
}

我试图在另一个名为uploadService.java. 如何Part在以下函数中将参数作为类型传递? uploadService.uploadFile();

标签: javaspring-bootservletsgoogle-cloud-platform

解决方案


推荐阅读