Amazon web services 如何在Amazon elastic beanstalk服务器上保存pdf文件?
我正在amazon elastic bean服务器上部署一个web应用程序,我使用的是免费版本。在SpringFramework中解析JSON响应后,我想将PDF文件保存在服务器上。如何在amazon elastic bean上保存pdf文件?我不想把它保存在数据库中。要存储文件,我建议使用S3存储桶。使用AmazonSDK来实现这一点。有关java,请参阅 参考这些好文章 如果我是你,我会用。这样,您就不必担心如何自己实现这些代码,而且您现在和将来在何处存储图像方面有了更大的灵活性 创建现有的Spring MVC应用程序非常容易: pom.xml PdfStore.javaAmazon web services 如何在Amazon elastic beanstalk服务器上保存pdf文件?,amazon-web-services,spring-mvc,amazon-elastic-beanstalk,spring-restcontroller,spring-rest,Amazon Web Services,Spring Mvc,Amazon Elastic Beanstalk,Spring Restcontroller,Spring Rest,我正在amazon elastic bean服务器上部署一个web应用程序,我使用的是免费版本。在SpringFramework中解析JSON响应后,我想将PDF文件保存在服务器上。如何在amazon elastic bean上保存pdf文件?我不想把它保存在数据库中。要存储文件,我建议使用S3存储桶。使用AmazonSDK来实现这一点。有关java,请参阅 参考这些好文章 如果我是你,我会用。这样,您就不必担心如何自己实现这些代码,而且您现在和将来在何处存储图像方面有了更大的灵活性 创建现有的
@StoreRestResource(path=“pdfs”)
公共接口PdfStore扩展存储{
}
就这样。现在,您将在/pdfs
上拥有一个功能齐全的(POST、PUT、GET、DELETE)基于REST的pdf服务,该服务将使用您的pdf存储来存储(和检索)存储在您的elastic bean服务器上的pdf,地址为/path/to/your/pdfs
所以
curl——上传文件example-pdf.pdf/pdfs/example-pdf.pdf
将上载example-pdf.pdf并将其存储在服务器上的/path/to/your/pdf/example pdf.pdf
,然后:
GET/pdfs/example pdf.pdf
将返回下载它
如果您想将图像存储在S3存储桶中,那么您只需将第一个依赖项更改为spring-content-S3,并将@Configuration
更新为@EnableS3Stores
,并为S3存储桶添加连接详细信息。参考指南位于手册的底部
HTH我认为这可能会有所帮助,下面的代码可以用于下载文件
@RequestMapping(value="download", method=RequestMethod.GET)
public void downloadPDFResource( HttpServletRequest request,
HttpServletResponse response)
{
//If user is not authorized - he should be thrown out from here itself
//Authorized user will download the file
String dataDirectory = request.getServletContext().getRealPath("/WEB-INF/PDF/");
Path file = Paths.get(dataDirectory, "file.pdf");
if (Files.exists(file))
{
response.setContentType("application/vnd.android.package-archive");
response.addHeader("Content-Disposition", "attachment; filename=downloadedfile.pdf");
try
{
Files.copy(file, response.getOutputStream());
response.getOutputStream().flush();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
我不明白你的解决方案是如何运作的@StoreRestResource(path=“pdfs”)公共接口PdfStore扩展了Store{}只是一些xyz path n代码将负责将其存储在s3存储桶上?基本上是的。所发生的事情是,您声明了对
spring-content fs
的依赖关系(对于文件系统,或者如果愿意,对于s3,spring-content-s3
),并创建PdfStore
接口。当您的应用程序启动时,Spring内容将查看该依赖关系和接口,并为文件系统(或s3)注入接口的实现,因此您不需要编写它。它还将研究spring content rest
依赖关系,并注入一个@Controller
实现(restapi),该实现也与接口进行通信,因此如果您希望我重构spring引导或s3的答案,并且我可以这样做,那么您也不必编写该实现。
@Configuration
@EnableFilesystemStores
@Import(RestConfiguration.class)
public class EnableFilesystemStoresConfig {
@Bean
File filesystemRoot() {
try {
return new File("/path/to/your/pdfs");
} catch (IOException ioe) {}
return null;
}
@Bean
FileSystemResourceLoader fileSystemResourceLoader() {
return new FileSystemResourceLoader(filesystemRoot().getAbsolutePath());
}
}
@StoreRestResource(path="pdfs")
public interface PdfStore extends Store<String> {
}
@RequestMapping(value="download", method=RequestMethod.GET)
public void downloadPDFResource( HttpServletRequest request,
HttpServletResponse response)
{
//If user is not authorized - he should be thrown out from here itself
//Authorized user will download the file
String dataDirectory = request.getServletContext().getRealPath("/WEB-INF/PDF/");
Path file = Paths.get(dataDirectory, "file.pdf");
if (Files.exists(file))
{
response.setContentType("application/vnd.android.package-archive");
response.addHeader("Content-Disposition", "attachment; filename=downloadedfile.pdf");
try
{
Files.copy(file, response.getOutputStream());
response.getOutputStream().flush();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}