【发布时间】:2016-06-21 23:55:56
【问题描述】:
我有一个使用 Spring 和 hibernate 框架的 java web 应用程序。我正在 azure 上移动这个网络应用程序。在本地网络应用程序中有一个功能,我首先将图像上传到 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;
}
}
【问题讨论】:
-
您仍然可以使用相对路径而不是绝对路径来存储这些文件。但是,请注意,在您重新部署 Web 应用程序后,这些文件将被覆盖。如果要将这些文件存储在存储中,可以阅读 Azure Storage 的 java API。这是文档 - How to use Blob storage from Java 和 How to use File Storage from Java
-
如何获取上传的blob的路径?我想将文件的路径存储在数据库中。
-
您说需要将路径存储在数据库中。这意味着您不希望您的文件被覆盖。 Blob 的路径可能如下所示:
http://<storage-account-name>.blob.core.windows.net/<container-name>/<blob-name>。请阅读我上面提供的 2 篇文章。您可以在 Blob 存储和文件存储之间进行选择。 Blob 存储可以通过浏览器提供目录,这对图像有好处,而文件存储可以挂载在 VM 或云服务中。
标签: java spring azure azure-storage azure-web-app-service