使用java rest webservice 0B上载Azure映像

时间:2015-07-20 11:02:25

标签: java rest azure blob

@Path("/files")
public class fileupload {
    private String storageConnectionString=
        "DefaultEndpointsProtocol=http;" + 
        "AccountName=xxxxxxxxxxx;" + 
        "AccountKey=xxxxxxxxxxxxxxxxxxxxx";

    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public  Response mymethod(  
           @FormDataParam("file") InputStream fileInputStream,
           @FormDataParam("file") FormDataContentDisposition contentDispositionHeader
                ) 
    {
        String fileName = contentDispositionHeader.getFileName();
        UploadtoAzureStorage(fileInputStream,fileName);
        String result="File saved to Azure storage, container name: \"mycontainer\"";
        return Response.status(200).entity(result).build();
    }


    public void UploadtoAzureStorage(InputStream fileInputStream,String filename)
    {
        try
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

            CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

            CloudBlobContainer container = blobClient.getContainerReference("mycontainer");

            byte[] bytes = IOUtils.toByteArray(fileInputStream);
            CloudBlockBlob blob = container.getBlockBlobReference(filename);

            blob.upload(fileInputStream,bytes.length);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

将图片名称上传到我的azure存储blob容器中,但图片无法上传。它显示为0B图像。

1 个答案:

答案 0 :(得分:0)

使用球衣时存在一个已知问题,请在jersey-multipart库中排除默认的mimepull,并明确包含此依赖关系:

         <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-multipart</artifactId>
            <version>1.9</version>
             <exclusions>
                 <exclusion>
                    <groupId>org.jvnet</groupId>
                    <artifactId>mimepull</artifactId>
                 </exclusion>
         </exclusions>
        </dependency>
        <dependency>
         <groupId>org.jvnet.mimepull</groupId>
         <artifactId>mimepull</artifactId>
         <version>1.9</version>
        </dependency>

有关详细信息,请参阅http://www.javaroots.com/2013/09/jersey-rest-service-stream-already.html。 另请考虑更改上传方法,如下所示:

 public void UploadtoAzureStorage(InputStream fileInputStream,String filename)
      {
          try
          {
              CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

              CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

              CloudBlobContainer container = blobClient.getContainerReference("mycontainer");

              byte[] bytes = IOUtils.toByteArray(fileInputStream);
              CloudBlockBlob blob = container.getBlockBlobReference(filename);

              BlobOutputStream blobOutputStream = blob.openOutputStream();
              ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); 

              int next = inputStream.read();
              while (next != -1) {
                    blobOutputStream.write(next);
                    next = inputStream.read();
              }
              blobOutputStream.close();

          }
          catch (Exception e)
          {
              e.printStackTrace();
          }
      }

完成这些步骤后,我已成功上传文件。