2016-03-08 78 views
0

我有一个使用Spring和hibernate框架的java web应用程序。我正在将这个网络应用放在天蓝色的。在内部Web应用程序中,我有一个功能,首先将图像上载到C:中的临时文件夹中,然后访问该文件以供应用程序使用。上传文件的位置也存储在数据库中以供进一步参考。我已经定义了用于在属性文件中上传文件的基路径,并通过控制器访问它以及用于创建目录,文件名和文件路径的服务层。通过Java MVC网站的Azure存储

任何人都可以告诉我如何使用天蓝色存储在Azure中执行相同操作?任何帮助表示赞赏。

代码属性文件中:

# Base File Path for Uploading Files 
fileupload.basepath=C:/webApp 

代码创建的临时文件夹

@RequestMapping(value = "/file/upload", method = RequestMethod.POST) 
public @ResponseBody 
String upload(MultipartHttpServletRequest request, 
     HttpServletResponse response) { 

    // 0. notice, we have used MultipartHttpServletRequest 

    // 1. get the files from the request object 
    Iterator<String> itr = request.getFileNames(); 

    MultipartFile mpf = request.getFile(itr.next()); 

    if (!CommonUtil.isNull(mpf)) { 
     if (mpf.getSize() > ProductCommonConstants.MAX_FILE_UPLOAD_SIZE_IN_BYTES) { 
      return CommonConstants.STR_FAILURE; 
     } 

    } 

    long fileName = Calendar.getInstance().getTimeInMillis(); 

    final String modelImageDirPath = baseUploadFilePath + "/" 
      + CommonConstants.TEMP_FILE_NAME; 

    // Check for folder existence 
    final File modelImageDir = new File(modelImageDirPath); 
    if (!modelImageDir.exists()) { 
     // Create the directory 
     modelImageDir.mkdirs(); 
    } 

    InputStream is = null; 
    FileOutputStream fos = null; 

    try { 
     String contentType = mpf.getContentType(); 

     if (contentType != null) { 

      is = new DataInputStream(mpf.getInputStream()); 

      // just temporary save file info 
      File file = new File(modelImageDirPath + "/" + fileName); 

      fos = new FileOutputStream(file); 

      // Write to the file 
      IOUtils.copy(is, fos); 
     } 

    } catch (FileNotFoundException ex) { 

    } catch (IOException ex) { 

    } finally { 

     try { 
      if (fos != null) { 
       fos.close(); 
      } 
      if (is != null) { 
       is.close(); 
      } 
     } catch (IOException ignored) { 
      // Log the Exception 

     } 
    } 

    // 2. send it back to the client as <img> that calls get method 
    // we are using getTimeInMillis to avoid server cached image 

    return "/service/common/file/get/" + fileName; 

} 

} 
+0

您仍然可以使用相对路径而不是绝对路径存储这些文件。但是,应该重新部署Web应用程序后,这些文件将被覆盖。如果要将这些文件存储在存储中,则可以读取Azure存储的Java API。这里是文档 - [如何使用Blob存储从Java](https://azure.microsoft.com/en-in/documentation/articles/storage-java-how-to-use-blob-storage/)和[如何使用Java中的文件存储](https://azure.microsoft.com/en-in/documentation/articles/storage-java-how-to-use-file-storage/) –

+0

如何获取上载的路径团块?我想将文件的路径存储在数据库中。 – Nidhee

+0

你说你需要在DB中存储路径。这意味着你不希望你的文件被覆盖。 blob的路径可能如下所示:'http:// .blob.core.windows.net/<容器名>/'。请阅读我上面提供的两篇文章。您可以选择blob存储和文件存储。可以通过浏览器为服务目录提供斑点存储,这对于图像很有用,而文件存储可以安装在虚拟机或云服务中。 –

回答

0

按我的经验,你可以使用CloudBlob类的upload(InputStream sourceStream, long length)从Spring MVC的MultipartFile文件上传到Azure的斑点存储,请参阅从您的代码修改下面的代码。

@RequestMapping(value = "/file/upload", method = RequestMethod.POST) 
public @ResponseBody String upload(MultipartHttpServletRequest request, 
     HttpServletResponse response) { 
    // 0. notice, we have used MultipartHttpServletRequest 
    // 1. get the files from the request object 
    Iterator<String> itr = request.getFileNames(); 
    MultipartFile mpf = request.getFile(itr.next()); 
    if (!CommonUtil.isNull(mpf)) { 
     if (mpf.getSize() > ProductCommonConstants.MAX_FILE_UPLOAD_SIZE_IN_BYTES) { 
      return CommonConstants.STR_FAILURE; 
     } 
    } 
    long fileName = Calendar.getInstance().getTimeInMillis(); 
    // Modified from your code: START 
    String storageConnectionString = "DefaultEndpointsProtocol=http;" + "AccountName=your_storage_account;" + "AccountKey=your_storage_account_key"; 
    CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString); 
    CloudBlobClient blobClient = storageAccount.createCloudBlobClient(); 
    CloudBlobContainer container = blobClient.getContainerReference("<blob-container-name>"); 
    InputStream is = null; 
    try { 
     String contentType = mpf.getContentType(); 
     if (contentType != null) { 
      is = new DataInputStream(mpf.getInputStream()); 
      long length = mpf.getSize(); 
      CloudBlockBlob blob = container.getBlockBlobReference(""+fileName); 
      blob.upload(is, length); 
     } 
    // Modified from your code: END 
    } catch (FileNotFoundException ex) { 

    } catch (IOException ex) { 

    } finally { 
     try { 
      if (is != null) { 
       is.close(); 
      } 
     } catch (IOException ignored) { 
      // Log the Exception 

     } 
    } 
    // 2. send it back to the client as <img> that calls get method 
    // we are using getTimeInMillis to avoid server cached image 
    return "/service/common/file/get/" + fileName; 
} 

对于下载或引用的blob,你需要记录BLOB到数据库的容器名称& BLOB名称。

OutputStream os = ...; // get the OutputStream from the HTTP Response 
CloudBlobContainer container = blobClient.getContainerReference("<container-name>"); 
CloudBlob blob = getBlockBlobReference("<blob-name>"); 
blob.download(os) 

欲了解更多信息,可以参考CloudBlobhttp://azure.github.io/azure-storage-java/com/microsoft/azure/storage/blob/CloudBlob.html的Javadoc,以及获得的Blob存储https://azure.microsoft.com/en-us/documentation/articles/storage-java-how-to-use-blob-storage/开始文档。

+0

谢谢..有什么办法可以将缓存的图像上传到天蓝色的存储? – Nidhee

+0

@Nidhee除了使用Azure存储SDK for Java,您还可以直接在Java中使用[Azure存储REST API](https://msdn.microsoft.com/en-us/library/azure/dd179355.aspx) 。但是,由于SDK包装了这些REST API,因此不是必需的。 –

+0

@感谢Peter。正如你上面所说的引用blob,你需要在DB中记录容器和blob名称。但对于我的前提Web应用程序,我正在给图像的路径并将其作为徽标显示。对于Azure网站,我也需要相同的功能。我尝试将网址保存到数据库中,并将其引用,但没有发生。你可以提出任何建议吗? – Nidhee