【问题标题】:Display image on JSP page using mySql [duplicate]使用 mySql 在 JSP 页面上显示图像 [重复]
【发布时间】:2017-04-14 17:26:38
【问题描述】:

请帮助我在存储在 mySql 数据库中的 JSP 页面的特定部分中显示图像,并告诉我我们可以以任何数据类型存储图像吗?

【问题讨论】:

  • 您通常不会将图像存储在表中,而是存储图像的路径/文件名。由于许多原因,存储实际图像是有问题的。它使渲染到页面变得更加困难是不这样做的最大原因。

标签: java mysql image jsp servlets


【解决方案1】:

网页显示来自 URL 的图像。

您可以在data: URI 中对图像进行编码,或者编写服务器端代码以响应某个 URL 来提供它。

【讨论】:

    【解决方案2】:

    像这样将此 servlet 添加到您的项目中:

    package com.app.meservlets;
    
    import java.io.IOException;
    
    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;
    
    import com.app.upload.UploadPicture;
    
    /**
     * 
     * @author user-sqli date: 12/05/2016 17:42
     */
    @WebServlet(urlPatterns = "/upload", loadOnStartup = 1)
    @MultipartConfig(fileSizeThreshold = 1024 * 1024 * 2, // 2MB
    maxFileSize = 1024 * 1024 * 10, // 10MB
    maxRequestSize = 1024 * 1024 * 50)
    // 50MB
    public class ControllerUploadPicture extends HttpServlet {
    
        private static final long serialVersionUID = 1L;
    
        @Override
        protected void doPost(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            String appPath = request.getServletContext().getRealPath("");
    
            Part part = request.getPart("file");
            UploadPicture.createNewInstance().TranseferPicture(part, appPath);
    
            getServletContext().getRequestDispatcher("/show.jsp").forward(request,
                    response);
        }
    
    }
    

    并将此类添加到您的项目中:

    package com.app.upload;
    
    import java.io.File;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    import javax.servlet.http.Part;
    
    /**
     * 
     * @author user-sqli date: 12/05/2016 17:30
     */
    public class UploadPicture {
        private static final String EXTENSION = ".";
        public static final String SAVE_DIR = "uploadImage";
        private static UploadPicture uploadPicture = null;
        private static final Logger LOGGER = Logger.getLogger(UploadPicture.class
                .getName());
    
        private UploadPicture() {
        }
    
        public String TranseferPicture(Part part, String appPath) {
            String savePath = appPath + File.separator + SAVE_DIR;
            File fileSaveDir = new File(savePath);
            if (!fileSaveDir.exists()) {
                fileSaveDir.mkdir();
            }
    
            String fileName = new SimpleDateFormat("yyyyMMdd_HHmmss")
                    .format(new Date());
            String nameImage = fileName + EXTENSION + getExtensionImage(part);
            try {
                part.write(savePath + File.separator + nameImage);
                LOGGER.log(Level.FINE, "Upload  Picture to {0} ", savePath
                        + File.separator + nameImage);
    
            } catch (IOException ex) {
                LOGGER.log(Level.SEVERE, ex.toString(), ex);
            }
            return nameImage;
        }
    
        private String getExtensionImage(Part part) {
    
            return part.getContentType().split("/")[1];
        }
    
        public static UploadPicture createNewInstance() {
            if (uploadPicture == null) {
                uploadPicture = new UploadPicture();
            }
            return uploadPicture;
        }
    }
    

    在你的jsp中像这样:

    <!DOCTYPE html >
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Upload file</title>
    </head>
    <body>
        <form method="post" action="upload" enctype="multipart/form-data">
            <input type="file" name="file" /><br /> <input type="submit"
                value="Upload" />
        </form>
    
    </body>
    </html>
    

    为了拍摄这张照片:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <%@taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <!DOCTYPE html >
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    
     uploaded image sucess.
    <%-- <img  width="100px" height="100px"  src="<c:url   value="/uploadImage/${name}" />"> --%>
    </body>
    </html>
    

    这个例子需要 servlet >=3.1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-27
      • 2012-09-29
      • 2019-09-29
      • 1970-01-01
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 2013-03-27
      相关资源
      最近更新 更多