我如何在 java webapp 中保存和检索服务器上的图像 [英] How I save and retrieve an image on my server in a java webapp

查看:20
本文介绍了我如何在 java webapp 中保存和检索服务器上的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有另一个关于图像的问题(这似乎比我最初预测的更难)我正在使用 JSF 2.0 (apache myFaces) 开发一个 java Web 应用程序,我希望这个应用程序能够将图片上传到它将运行的服务器上的目的地.我有一个运行 mySQL Db 的 Windows r2 2008 服务器,但我不想将图像存储在数据库中,我宁愿将它存储在服务器中的某个位置,然后将路径保存为数据库中的字符串.

Here with another question on Images ( which seems to be more difficult than I initialy predicted) I'm working on a java Web app with JSF 2.0 ( apache myFaces) and I want this app to be able to upload a picture to a destination on the server it's going to run on. I have a Windows r2 2008 Server running a mySQL Db, but I don't want to store the image in the db, I'd rather store it somewhere in the server and then just save the path as a string in the db.

有人告诉我这是最好的方法,但我似乎找不到有关如何将其保存在服务器上的示例.我在 Apache tomcat 服务器上运行该应用程序作为 WAR 文件.所以我不知道我是否必须将文件保存到服务器驱动器上的路径(即 C:\images)或项目本身的特殊文件夹(在 java、html 文件中),非常感谢任何帮助.我完全迷失了,并且一整天都在试图解决这个问题.

I was told this is the best way, but I can't seem to find an example on how to save it on the server. I run the app on the Apache tomcat Server as a WAR file. so I don't know if I have to save the file to a path on the server drive (i.e. C:\images) or a special folder in the project itself ( within the java, html files) any help at all is greatly appreciated. I'm totally lost and have been stuck the whole day trying to figure this out.

我用来将图像上传到 java 类的代码是这样的(由 CodyS 提供):

The code I use to upload the image to the java class is this ( courtesy of CodyS):

InputStream is = uploadedFile.getInputStream();  
byte[] buffer = new byte[(int) uploadedFile.getSize()];
is.read(buffer); 
File f = new File("C:\\temp\\" + this.patient.getPk() + ".jpeg");   
f.createNewFile();  
FileOutputStream fos = new FileOutputStream(f);  
fos.write(buffer);  //This is where I write it to the C Drive
fos.close();
is.close(); 

我打算在服务器上运行它而不是将它写入我的 C 驱动器,但是我应该将图像存储在哪里以便以后检索并在 xhtml 文件中显示?我希望我清楚我需要什么,如果我没有,请告诉我,我会尝试以另一种方式解释.

instead of writing it to my C drive I'm going to run it on the server, but where should I store the image to later retriev and display in an xhtml file? I hope I'm being clear on what I need, let me know if I am not and I'll try to explain in another way.

推荐答案

我打算在服务器上运行它而不是将它写入我的 C 驱动器,但是我应该将图像存储在哪里以便以后检索并在 xhtml 文件中显示?

这取决于您对配置服务器的控制程度.理想的情况是在 Tomcat webapps 文件夹之外配置一个固定路径.例如,/var/webapp/upload.您可以将此路径设置为 VM 参数或环境变量,以便您的 Web 应用程序可以通过编程方式检索它,而无需更改代码.

That depends on how much control you have over configuring the server. Ideal would be to configure a fixed path outside the Tomcat webapps folder. For example, /var/webapp/upload. You can set this path as a VM argument or environment variable so that your webapp can retrieve it programmatically without the need to change the code.

例如,当指定为VM参数-Dupload.location=/var/webapp/upload时,可以如下完成上传:

For example, when specifying as VM argument -Dupload.location=/var/webapp/upload, you can complete the upload as follows:

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.

对于返回文件,最理想的是将上传位置作为单独的 添加到 Tomcat.例如

As to serving the file back, most ideal would be to add the upload location as a separate <Context> to Tomcat. E.g.

<Context docBase="/var/webapp/upload" path="/uploads" />

这样就可以通过http://example.com/uploads/foo-直接访问123456.ext

如果您对配置服务器的控制为零,那么,存储在数据库中或发送到第三方主机(例如 Amazon S3)是您最好的选择.

If you have zero control over configuring the server, then, well, storing in the DB or sending to a 3rd party host such as Amazon S3 is your best bet.

这篇关于我如何在 java webapp 中保存和检索服务器上的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆