我正在使用 Bluemix 对象存储服务和 Java 通过编码将文件上传到对象存储。这是我将文件上传到对象存储的代码片段:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ObjectStorageService objectStorage = authenticateAndGetObjectStorageService();
System.out.println("Storing file in post ObjectStorage...0508");
String containerName = request.getParameter("container");
String fileName = request.getParameter("file");
System.out.println("containerName in post: "+containerName +"And file name"+fileName);
if(containerName == null || fileName == null){ //No file was specified to be found, or container name is missing
response.sendError(HttpServletResponse.SC_NOT_FOUND);
System.out.println("File not found.");
return;
}
try {
final InputStream fileStream = request.getInputStream();
System.out.println("fileStream: "+fileStream);
Payload<InputStream> payload = new PayloadClass(fileStream);
objectStorage.objects().put(containerName, fileName, payload);
System.out.println("Successfully stored file in ObjectStorage!");
} catch (Exception e) {
System.out.println("Exception in uploaidng +"+e.toString());
// TODO: handle exception
}
}
但是,一个 0kb 的文件被上传到 Object Storage。