--Try uploading images with good old tech stack using Servlet / JSP --What I want to do this time is to convert the file sent by multipart / form-data to Base64 on the server side. --Both Servlet and JSP are amateurs
--JSP of the upload screen to be displayed first
<%@ 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>
--Throw an image to / upload
with multipart / form-data
--Controller to receive upload request
--The base sample is the same, but when ʻaction_name is ʻupload
, it feels like calling ʻUploadAction. --This time, we will receive multipart / form-data, so the point is to add
@ MultipartConfig`.
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;
}
}
}
--Action to receive and process images
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();
}
}
--Normal parameters are acquired by request.getParameter ("xxx ")
, but this time it is a file, so they are acquired by request.getPart ("xxx ")
.
――After that, I arrived at Base64 with the feeling of InputStream → byte → Base64.
――Since it's a big deal, I've made it up to the point where I pass the Base64 value to JSP and display it on the screen.
--JSP to display image
<%@ 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 can also be set in the src attribute of the img tag, so embed it. ――png It's not good to be decisive ――It seems that you can get it by server side processing, but it was not possible to use the method that came out immediately after google. ――If you can do so far, you can upload the image and the preview screen will be displayed.
--Even though you are using Servlet / JSP, the processing after receiving the file is pure Java, isn't it? --Servlet / JSP power has been wasted
Recommended Posts