-2
請幫助我在存儲在mySql數據庫中的JSP頁面的特定部分中顯示圖像,並告訴我可以將圖像存儲在任何數據類型中?使用mySql在JSP頁面上顯示圖像
請幫助我在存儲在mySql數據庫中的JSP頁面的特定部分中顯示圖像,並告訴我可以將圖像存儲在任何數據類型中?使用mySql在JSP頁面上顯示圖像
網頁顯示來自URL的圖像。
您可以將圖像編碼爲data:
URI,或者編寫服務器端代碼以響應某個URL來提供該圖像。
這個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>
和shoing此圖片:
<%@ 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
您通常不會將圖像存儲在表格中,而是存儲圖像的路徑/文件名。存儲實際圖像有許多原因是有問題的。這使得難以呈現到頁面是不這樣做的最大原因。 –