简体   繁体   English

如何拍摄用户图像,存储它们,并在以后渲染它们?

[英]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.

相关问题 如何在java中存储对象并在以后检索它们? - How to store objects and retrieve them later in java? 如何将所有用户输入存储在 do while 循环中,并在 Java 程序中稍后调用它们进行打印? - How to store all the user inputs in do while loop, and call them to print later in Java program? 如何在内存中存储arrayList的不同对象并稍后检索它们? - How to store different objects of arrayList in memory and later retrieve them? 将视图存储在缓冲区中,并在以后显示 - Store views in a buffer and show them later 如何将文件保存在android存储设备上,写入文件并在以后访问它们? - How to save files on android storage, write to them, and access them later on? 在Jmeter中,如何在运行时动态存储变量并在以后使用它们? - In Jmeter, how do you store variables dynamically during runtime and use them later? 如果以后要按多个属性搜索对象,该如何存储对象? - How do I store objects if I want to search them by multiple attributes later? 在内部存储对象的 JSONArray 并稍后读取它们 ANDROID - Store JSONArray of objects internally and read them later ANDROID 将图片网址存储在sharedpreferences中,并在Recyclerview中显示它们 - Store images url in sharedpreferences and display them in Recyclerview 使用 Java Swing,如何获取用户的输入并将其存储以备后用? - Using Java Swing, how do you take an input from a user and store it for later use?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM