Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过JavaMVC网站实现Azure存储_Java_Spring_Azure_Azure Storage_Azure Web App Service - Fatal编程技术网

通过JavaMVC网站实现Azure存储

通过JavaMVC网站实现Azure存储,java,spring,azure,azure-storage,azure-web-app-service,Java,Spring,Azure,Azure Storage,Azure Web App Service,我有一个使用Spring和hibernate框架的java web应用程序。我正在azure上移动此web应用程序。在内部部署的web应用程序中,有一个功能,我首先将图像上载到C:中的临时文件夹中,然后访问该文件以供应用程序使用。上传文件的位置也存储在数据库中,以供进一步参考。我已经定义了基本路径,用于在属性文件中上载文件,并通过它在控制器和服务层中进行访问,以创建目录、文件名和文件路径 有人能告诉我如何在azure中使用azure存储进行同样的操作吗?感谢您的帮助 属性文件中的代码: # Ba

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

有人能告诉我如何在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;

}

}
@RequestMapping(value=“/file/upload”,method=RequestMethod.POST)
公共@ResponseBody
字符串上载(MultipartHttpServletRequest请求,
HttpServletResponse(响应){
//0.注意,我们使用了multipartTTPServletRequest
//1.从请求对象获取文件
迭代器itr=request.getFileNames();
MultipartFile mpf=request.getFile(itr.next());
如果(!CommonUtil.isNull(mpf)){
if(mpf.getSize()>ProductCommonConstants.MAX\u文件\u上传\u大小\u字节){
返回CommonConstants.STR_失败;
}
}
长文件名=Calendar.getInstance().getTimeInMillis();
最终字符串modelImageDirPath=baseUploadFilePath+“/”
+CommonConstants.TEMP_文件名;
//检查文件夹是否存在
最终文件modelmagedir=新文件(modelmagedirpath);
如果(!modelmagedir.exists()){
//创建目录
modelmagedir.mkdirs();
}
InputStream=null;
FileOutputStream=null;
试一试{
字符串contentType=mpf.getContentType();
if(contentType!=null){
is=新数据输入流(mpf.getInputStream());
//只是暂时保存文件信息
File File=新文件(modelmagedirpath+“/”+文件名);
fos=新文件输出流(文件);
//写入文件
IOUtils.副本(is、fos);
}
}捕获(FileNotFoundException ex){
}捕获(IOEX异常){
}最后{
试一试{
如果(fos!=null){
fos.close();
}
如果(is!=null){
is.close();
}
}捕获(忽略IOException){
//记录异常
}
}
//2.在调用get方法时将其发送回客户端
//我们使用getTimeInMillis来避免服务器缓存的映像
返回“/service/common/file/get/”+文件名;
}
}

根据我的经验,您可以使用
类的
上传(InputStream sourceStream,long length)
将文件从Spring MVC
多部分文件
上传到Azure Blob存储,请查看下面根据您的代码修改的代码

@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;
}
@RequestMapping(value=“/file/upload”,method=RequestMethod.POST)
public@responseBy字符串上载(MultipartTTpServletRequest请求,
HttpServletResponse(响应){
//0.注意,我们使用了multipartTTPServletRequest
//1.从请求对象获取文件
迭代器itr=request.getFileNames();
MultipartFile mpf=request.getFile(itr.next());
如果(!CommonUtil.isNull(mpf)){
if(mpf.getSize()>ProductCommonConstants.MAX\u文件\u上传\u大小\u字节){
返回CommonConstants.STR_失败;
}
}
长文件名=Calendar.getInstance().getTimeInMillis();
//从代码中修改:开始
String-storageConnectionString=“DefaultEndpointsProtocol=http;”+“AccountName=your\u storage\u account;“+”AccountKey=your\u storage\u account\u key”;
CloudStorageAccount-storageAccount=CloudStorageAccount.parse(storageConnectionString);
CloudBlobClient blobClient=storageAccount.createCloudBlobClient();
CloudBlobContainer容器=blobClient.getContainerReference(“”);
InputStream=null;
试一试{
字符串contentType=mpf.getContentType();
if(contentType!=null){
is=新数据输入流(mpf.getInputStream());
long length=mpf.getSize();
CloudBlockBlob blob=container.getBlockBlobReference(“+”文件名);
blob.upload(是,长度);
}
//从代码中修改:结束
}捕获(FileNotFoundException ex){
}捕获(IOEX异常){
}最后{
试一试{
如果(is!=null){
is.close();
}
}捕获(忽略IOException){
//记录异常
}
}   
//2.在调用get方法时将其发送回客户端
//我们使用getTimeInMillis来避免服务器缓存的映像
返回“/service/common/file/get/”+文件名;
}
要下载或引用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)
OutputStream os=…;//从HTTP响应获取OutputStream
CloudBlobContainer容器=blobClient.getContainerReference(“”);
CloudBlob blob=getBlockBlobReference(“”);
下载(操作系统)

有关更多信息,您可以参考类
CloudBlob
的Javadoc和Blob存储的入门文档。

您仍然可以使用相对路径而不是绝对路径存储这些文件。但是,请注意,在重新部署web应用程序后,这些文件将被覆盖。如果您想将这些文件存储在存储器中,可以阅读Azure存储器的java API。这是文档-以及如何获取上传blob的路径?我想将文件的路径存储在DB中。您说过需要将路径存储在DB中。这意味着您不希望覆盖您的文件。blob的路径可能如下所示:
http://.blob.core.windows.net//
。请阅读我上面提供的2篇文章。您可以在blob存储和文件存储之间进行选择。blob存储可以通过浏览器提供给目录