[英]How to take user images, store them, and render them later?
I'm trying to build an application that takes a user image from an html form, uses a java servlet to store said image in a database, and then renders the image onto the screen with another html tag.我正在尝试构建一个应用程序,该应用程序从 html 表单中获取用户图像,使用 java servlet 将所述图像存储在数据库中,然后使用另一个 html 标记将图像呈现到屏幕上。 The HTML form to accept the image is:接受图像的 HTML 表单是:
<form action="../servlets/submitrequest" method="post" enctype="multipart/form-data">
<input type="number" name="cost" placeholder="Cost" required/><br/>
<p id="img">Image of receipt: <input type="file" name="image" required/><br/></p>
<input type="submit" id="submit" name="submit" value="Submit" onClick="success();"/><br/>
</form>
The relevent part of my servlet that saves the file is:我的 servlet 保存文件的相关部分是:
Random r = new Random();
String dir = "C://tmp/reciepts/" + r.nextInt() + ".png";
for(Part part: request.getParts()) {
if(part.getName().equalsIgnoreCase("image"))
part.write(dir);
}
The servlet then saves dir
as the image filepath in the database (postgresql).然后 servlet 将dir
保存为数据库 (postgresql) 中的图像文件路径。 However when I tried to take the image URL and display it on the web browser Chrome throws the following error:但是,当我尝试获取图像 URL 并将其显示在 Web 浏览器上时,Chrome 会引发以下错误:
Not allowed to load local resource: file:///C://tmp/images/1154006734.png
Which according to this is caused because chrome can't read local files because of security reasons.根据这个是因为chrome出于安全原因无法读取本地文件造成的。 Fair enough.很公平。 So I started looking for ways to save it to the webcontent folder, so it would be a part of the content of the web application.所以我开始寻找方法将它保存到 webcontent 文件夹,这样它就会成为 web 应用程序内容的一部分。 I followed this example and changed my servlet's code to:我按照此示例将我的 servlet 代码更改为:
String dir = request.getServletContext().getRealPath("/") + File.separator + "reciepts";
File fileSaveDir = new File(dir);
if (!fileSaveDir.exists()) {
fileSaveDir.mkdir();
}
request.getPart("image").write(dir);
Even after doing this I still received the same error:即使在这样做之后,我仍然收到同样的错误:
Not allowed to load local resource: file:///C:/Users/Colby/eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/Project_1//reciepts/image.file
So my question is this: where do I store files uploaded by the user where they can be accessed by both the web browser and the application itself?所以我的问题是:我在哪里存储用户上传的文件,它们可以被 Web 浏览器和应用程序本身访问?
You have to change the doGet of a servlet to stream back the binary data.您必须更改 servlet 的 doGet 以流回二进制数据。
response.setContentType("application/pdf");
try (
ServletOutputStream output = response.getOutputStream();
InputStream input = getServletContext().getResourceAsStream("myLocalFile.pdf");
){
//transfer buffered stream to output stream
byte[] buffer = new byte[2048];
int bytesRead = -1;
while ((bytesRead = input.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
}
Then in your HTML rather than having <img src...>
pointing to a file
have it pointing to the above servlet.然后在你的 HTML 中,而不是让<img src...>
指向一个file
,让它指向上面的 servlet。
note笔记
Of course when your web application is run from a remote user, then will have no knowledge or access to your server's file system directly.当然,当您的 Web 应用程序从远程用户运行时,将不知道或直接访问您服务器的文件系统。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.