我有一个使用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;
}
}
您仍然可以使用相对路径而不是绝对路径存储这些文件。但是,应该重新部署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/) –
如何获取上载的路径团块?我想将文件的路径存储在数据库中。 – Nidhee
你说你需要在DB中存储路径。这意味着你不希望你的文件被覆盖。 blob的路径可能如下所示:'http:// .blob.core.windows.net/<容器名>/'。请阅读我上面提供的两篇文章。您可以选择blob存储和文件存储。可以通过浏览器为服务目录提供斑点存储,这对于图像很有用,而文件存储可以安装在虚拟机或云服务中。 –