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

查看:148
本文介绍了我如何在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 Server上运行应用程序作为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参数或环境变量,以便您的webapp可以以编程方式检索它而无需更改代码。

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.

关于回传文件,最理想的做法是将上传位置添加为单独的< Context> 到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.

  • How to provide relative path in File class to upload any file?
  • Reliable data serving

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

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