OpenShift Java - using an image from a Data dir

I am trying to create a upload-image button and then show the image on another jsp page.

I want to do this by uploading it to the app-root/data/images folder. This works with the following file path: filePath = System.getenv("OPENSHIFT_DATA_DIR") + "images/";

But how can I show this image on my jsp? I tried using:

 <BODY> <h1>SNOOP PAGE</h1> <a href="profielMijnBedrijf.jsp">Ga weer terug</a> <% String filepath = System.getenv("OPENSHIFT_DATA_DIR") + "images/"; out.println("<img src='"+filepath+"logo21.jpg'/>"); %> <img src="app-root/data/images/logo21.jpg"/> </BODY> 

Both of these options do not work. I also read that I need to create a symbolic link. But when I am in app-root/data or app-root/data/images or in app-root , the ln -s command returns the missing file operand

logo21.jpg appears in my git bash

+5
source share
4 answers

@developercorey is right (gave you +1 👍), I just feel the need to explain why:

  • The images you uploaded fall into a folder on the server
    ( String filepath = System.getenv("OPENSHIFT_DATA_DIR") + "images/" - the path to the folder on the server).
  • Your displayed HTML "<img src='"+filepath+"logo21.jpg'/> sent to the client (user browser) with the filepath url server.
  • Obviously, when a user browser tries to find an image using a server path that does not exist on the local computer, it will not work.

The best solution suggested by @developercorey is to add a new servlet or filter to send photos from the OPENSHIFT_DATA_DIR folder:

  • You will have a new url for the servlet serving your photo, something like http://your-server/uploaded/
  • And you can use <img src="http://your-server/uploaded/logo21.jpg" /> in your jsp.

Here's a snippet from How-To: Downloading and serving files using Java servlets in OpenShift

 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import javax.activation.MimetypesFileTypeMap; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; @WebServlet(name = "uploads",urlPatterns = {"/uploads/*"}) @MultipartConfig public class Uploads extends HttpServlet { private static final long serialVersionUID = 2857847752169838915L; int BUFFER_LENGTH = 4096; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); for (Part part : request.getParts()) { InputStream is = request.getPart(part.getName()).getInputStream(); String fileName = getFileName(part); FileOutputStream os = new FileOutputStream(System.getenv("OPENSHIFT_DATA_DIR") + fileName); byte[] bytes = new byte[BUFFER_LENGTH]; int read = 0; while ((read = is.read(bytes, 0, BUFFER_LENGTH)) != -1) { os.write(bytes, 0, read); } os.flush(); is.close(); os.close(); out.println(fileName + " was uploaded to " + System.getenv("OPENSHIFT_DATA_DIR")); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filePath = request.getRequestURI(); File file = new File(System.getenv("OPENSHIFT_DATA_DIR") + filePath.replace("/uploads/","")); InputStream input = new FileInputStream(file); response.setContentLength((int) file.length()); response.setContentType(new MimetypesFileTypeMap().getContentType(file)); OutputStream output = response.getOutputStream(); byte[] bytes = new byte[BUFFER_LENGTH]; int read = 0; while ((read = input.read(bytes, 0, BUFFER_LENGTH)) != -1) { output.write(bytes, 0, read); output.flush(); } input.close(); output.close(); } private String getFileName(Part part) { for (String cd : part.getHeader("content-disposition").split(";")) { if (cd.trim().startsWith("filename")) { return cd.substring(cd.indexOf('=') + 1).trim() .replace("\"", ""); } } return null; } } 
+3
source

The best way to serve user-uploaded images that you store in OPENSHIFT_DATA_DIR is to use the servlet as described here: https://forums.openshift.com/how-to-upload-and-serve-files-using-java-servlets- on-openshift? noredirect

This servlet basically takes the path / name of the image that is being requested, reads it from the file system and then directs it to the requestor.

+2
source

The OPENSHIFT_DATA_DIR directory OPENSHIFT_DATA_DIR not accessible to the website. You can make images stored in the OPENSHIFT_DATA_DIR (aka app-root/data ) directory web accessible by creating a symbolic link to them from the public OPENSHIFT_REPO_DIR .

For single use as proof of concept:

 rhc ssh -a <your_app_name> -n <your_namespace> ln -sf ${OPENSHIFT_DATA_DIR}images ${OPENSHIFT_REPO_DIR}images 

Now you can access logo21.jpg at https: // <your_app_name> - <your_namespace> .rhcloud.com / images / logo21.jpg or <img src="/images/logo21.jpg"/> .

The contents of OPENSHIFT_REPO_DIR overwritten when you push the changes, so you need to create a symbolic link using the deployment binding to re-create it every time you deploy. In .openshift/action_hooks/deploy :

 #!/bin/bash # This deploy hook gets executed after dependencies are resolved and the # build hook has been run but before the application has been started back # up again. # create the images directory if it doesn't exist if [ ! -d ${OPENSHIFT_DATA_DIR}images ]; then mkdir ${OPENSHIFT_DATA_DIR}images fi # create symlink to uploads directory ln -sf ${OPENSHIFT_DATA_DIR}images ${OPENSHIFT_REPO_DIR}images 
+1
source

You can upload the file to DATA DIRECTORY, and then copy the file from DATA DIRECTORY to any folder in the HOME DIRECTORY.

After that, you should be able to link to the image as usual on your page, but it appears. Openshift only displays items from a previous deployment or git push, so it might be better to save the file to a database and then read it directly from that database.

0
source

Source: https://habr.com/ru/post/1245850/


All Articles