--JSP des Upload-Bildschirms, der zuerst angezeigt werden soll
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Java Sample</title>
</header>
<body>
<h1>Hello</h1>
<form action="upload" method="post" enctype="multipart/form-data">
<input type="hidden" name="action_name" value="upload" />
<input type="file" name="file" />
<button>Upload</button>
</form>
</body>
</html>
/ upload
package com.example.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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 com.example.action.Action;
import com.example.action.UploadAction;
@WebServlet(urlPatterns = { "/upload" })
@MultipartConfig
public class UploadController extends HttpServlet {
@Override
protected void doPost(
HttpServletRequest request,
HttpServletResponse response
)
throws ServletException, IOException {
String strActionName = request.getParameter("action_name");
Action action = getInstance(strActionName);
String forwardPath = action.execute(request);
RequestDispatcher rd = request.getRequestDispatcher(forwardPath);
rd.forward(request, response);
}
private static Action getInstance(String actionName) {
switch (actionName) {
case "upload":
return new UploadAction();
default:
return null;
}
}
}
package com.example.action;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Base64;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
public class UploadAction extends Action {
@Override
protected String processRequest(HttpServletRequest request) {
try {
Part filePart = request.getPart("file");
InputStream fileContent = filePart.getInputStream();
byte[] byteArray = getByteArray(fileContent);
String base64String = Base64.getEncoder().encodeToString(byteArray);
System.out.println("filePart: " + filePart);
System.out.println("fileContent: " + fileContent);
System.out.println("byteArray: " + byteArray);
System.out.println("base64String: " + base64String);
request.setAttribute("image", base64String);
} catch (Exception e) {}
return "image.jsp";
}
private static byte[] getByteArray(InputStream is) throws Exception {
ByteArrayOutputStream b = new ByteArrayOutputStream();
BufferedOutputStream os = new BufferedOutputStream(b);
while (true) {
int i = is.read();
if (i == -1) break;
os.write(i);
}
os.flush();
os.close();
return b.toByteArray();
}
}
--JSP zum Anzeigen von Bildern
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<jsp:useBean id="image" scope="request" type="java.lang.String" />
<html>
<head>
<meta charset="UTF-8" />
<title>Java Sample</title>
</header>
<body>
<h1>Hello</h1>
<img src="data:image/png;base64,<%= image %>" />
</body>
</html>
--Base64 kann auch im src-Attribut des img-Tags festgelegt werden, also binden Sie es ein. ――Png Es ist nicht gut, entscheidend zu sein ――Es scheint, dass Sie es durch serverseitige Verarbeitung erhalten können, aber es war nicht möglich, die Methode zu verwenden, die unmittelbar nach Google herauskam ――Wenn Sie dies bisher tun können, können Sie das Bild hochladen und der Vorschaubildschirm wird angezeigt.
Recommended Posts