0

我正在使用 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。

对象存储屏幕截图

4

2 回答 2

0

我可以使它工作的唯一方法是将 MultiPart 注释放在 Servlet

 @WebServlet("/objectStorage")
@MultipartConfig
public class SimpleServlet extends HttpServlet

并获取部分而不是整个 inputStream:

 Part filePart = request.getPart("fileToUpload"); // Retrieves <input type="file" name="fileToUpload">  
 InputStream fileContent = filePart.getInputStream();

并在 html 表单上将 enctype 设置为 multipart/formdata

<form action="objectStorage" method="post" enctype="multipart/form-data">

我的整个代码是这样的:

HTML:

<form action="objectStorage" method="post" enctype="multipart/form-data">
            Select image to upload:
            <input type="file" name="fileToUpload" id="fileToUpload">  
            <input type="text" name="nome" id="nome">
            <input type="submit" value="Upload Image" name="submit">
 </form>

Java代码:

@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        ObjectStorageService objectStorage = authenticateAndGetObjectStorageService();
         System.out.println("Storing file in ObjectStorage...");
        String containerName = "abc";
        String fileName = request.getParameter("nome");

         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 {

            Part filePart = request.getPart("fileToUpload"); // Retrieves <input type="file" name="file">  
            InputStream fileContent = filePart.getInputStream();
            Payload<InputStream> payload = new PayloadClass(fileContent);

             objectStorage.objects().put(containerName, fileName,  payload);

            System.out.println("Upload Successful");
        } catch (Exception e) {
            System.out.println(e);
        }

    }
于 2016-08-23T11:43:04.240 回答
0

请使用 getData() 从 InputStream 中提取二进制文件。以下是 openstack4j getData 文档。

http://www.openstack4j.com/javadoc/org/openstack4j/api/sahara/JobBinaryInternalService.html#getData-java.lang.String-

于 2016-08-05T21:11:32.130 回答