使用mySql在JSP页面上显示图像

时间:2017-04-14 17:26:38

标签: java mysql image jsp servlets

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

2 个答案:

答案 0 :(得分:0)

将此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);
    }

}

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

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&gt; = 3.1

答案 1 :(得分:0)

网页显示网址图片。

您可以在data: URI中对图像进行编码,也可以编写服务器端代码来响应某些URL。