java - 将“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();
解决方案
推荐阅读
- java - 我的 Struts.xml 文件无法重定向到我的操作类并给我 404 错误
- ios - 当单元格底部可见时,UICollectionView 触发单元格动画
- android - 以编程方式创建一个带有渐变笔触的按钮?
- r - 如何从 R 中的 aov 摘要中进行子集化?
- kentico - Kentico:与 XML 数据源相关的问题,403 错误
- android-studio - 为什么 Android Studio 在运行 Flutter 应用时找不到 x86 模拟器?
- javascript - 在没有 Jquery 的情况下单击将类添加到子元素
- jquery - jQuery ReferenceError:找不到变量:事件
- angular - 在不使用 *ngIf 的情况下单击另一个 div 时显示一个 div
- html - 我们可以在一个输入中加入 2 个 HTML 模式吗?