而不是将其写入C驱动器,而是要在服务器上运行它,但是应将图像存储在哪里以供以后检索并显示在xhtml文件中?
这取决于您对服务器的配置有多少控制权。理想的做法是在Tomcat
webapps文件夹外部配置固定路径。例如,
/var/webapp/upload。您可以将此路径设置为VM参数或环境变量,以便您的webapp可以以编程方式检索它,而无需更改代码。
例如,当指定为VM参数时
-Dupload.location=/var/webapp/upload,您可以按照以下步骤完成上传:
Path folder = Paths.get(System.getProperty("upload.location"));String filename = FilenameUtils.getbaseName(uploadedFile.getName()); String extension = FilenameUtils.getExtension(uploadedFile.getName());Path file = Files.createTempFile(folder, filename + "-", "." + extension);try (InputStream input = uploadedFile.getInputStream()) { Files.copy(input, file, StandardCopyOption.REPLACE_EXISTING);}String uploadedFileName = file.getFileName().toString();// Now store it in DB.至于将文件送回,最理想的是将上传位置添加
<Context>为Tomcat 的单独位置。例如
<Context docbase="/var/webapp/upload" path="/uploads" />
这样,您可以通过http://example.com/uploads/foo-123456.ext直接访问它
如果您对配置服务器没有任何控制权,那么最好将其存储在数据库中或发送给第三方主机(例如Amazon S3)。
也可以看看:
- 如何在File类中提供相对路径以上传任何文件?
- 可靠的数据服务


![我如何在Java WebApp中的服务器上保存和检索图像[重复] 我如何在Java WebApp中的服务器上保存和检索图像[重复]](http://www.mshxw.com/aiimages/31/416334.png)
