ãã©ã¦ã¶ããã¢ãããã¼ããããã¡ã¤ã«ãããµã¼ãã¬ããã§ä¿åããæ¹æ³ãæ¸ãã¦ããã¾ãã
ãã¼ã¸ã§ã³
ãµã¼ãã¬ããã®ãã¼ã¸ã§ã³ã¯ä»¥ä¸ã®éãã§ãã
- Servlet 5.0ï¼JakartaEE 9ï¼
åä½ç¢ºèªç¨ãããã¯ã
åä½ç¢ºèªã®ããã«ã以ä¸ã®è£½åã使ç¨ãã¾ããã
ç®æ¬¡
- ãµã¼ãã¬ããã®ä½æ
- HTMLã®ä½æ
- ãã£ã¬ã¯ããªé層ã®ä½æ
- pom.xml ã®ä½æ
- åä½ç¢ºèª
1. ãµã¼ãã¬ããã®ä½æ
ã¢ãããã¼ãããããã¡ã¤ã«ããæå®ã®ãã£ã¬ã¯ããªã«ä¿åããã¯ã©ã¹ã使ãã¾ãã
org/example/upload/FileUploadServlet.java
package org.example.upload; import java.io.File; import java.io.IOException; import java.util.concurrent.atomic.AtomicInteger; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.MultipartConfig; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.Part; @SuppressWarnings("serial") @WebServlet("/upload/file") @MultipartConfig( fileSizeThreshold = 1024 * 1024, // 1MB maxFileSize= 1024 * 1024 * 10, // 10MB maxRequestSize = 1024 * 1024 * 50 // 50MB ) public class FileUploadServlet extends HttpServlet { // ãã¡ã¤ã«éçª private static final AtomicInteger id = new AtomicInteger(); // ãã¡ã¤ã«ä¿åå private static final File uploadDir = new File("C:/tmp"); public void init() throws ServletException { // ãµã¼ãã¬ããåæåæã«ä¿åå ã使 uploadDir.mkdir(); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // ãªã¯ã¨ã¹ãã®ãã¡ã¤ã«ãåå¾ Part part = req.getPart("file"); // ä¿åç¨ã®ãã¡ã¤ã«åã使ï¼"éçª_éä¿¡ãã¡ã¤ã«å"ï¼ StringBuilder saveName = new StringBuilder(); saveName.append(id.incrementAndGet()); saveName.append("_"); saveName.append(part.getSubmittedFileName()); // ä¿åç¨ã®ãã¡ã¤ã«ã使 File saveFile = new File( uploadDir, saveName.toString() ); // ãã¡ã¤ã«ãä¿å part.write(saveFile.getAbsolutePath()); // ã¬ã¹ãã³ã¹ãè¿å´ respond(res); } public void respond(HttpServletResponse res) throws IOException { res.setContentType("text/html"); res.setCharacterEncoding("utf-8"); res.getWriter().println( "<html><body><p>File saved.</p></body></html>" ); } }
ä¿åç¨ã®ãã¡ã¤ã«åã¯ãéçª_éä¿¡ãã¡ã¤ã«åãã«ãªãã¾ããPart ã¯ã©ã¹ã®ã¡ã½ããã使ã£ã¦ããã©ã¦ã¶ããã¢ãããã¼ãããããã¡ã¤ã«åãåå¾ãã¦ãã¾ãã
part.getSubmittedFileName()
2. HTMLã®ä½æ
åä½ç¢ºèªãããå ´åãã¢ãããã¼ãç¨ã® HTML ã使ãã¾ãã
webapp/upload.html
<!DOCTYPE html> <html> <head><meta charset="utf-8"></head> <body> <form method="POST" enctype="multipart/form-data" action="/upload/file"> <p><input type="file" name="file"></p> <p><input type="submit" value="éä¿¡"></p> </form> </body> </html>
form ã® enctype ããmultipart/form-dataãã«ãã¦ããã¡ã¤ã«ãã¢ãããã¼ãã§ããããã«ãã¦ãã¾ãã
ã¾ããform ã® action ãã/upload/fileãã«ãã¦ããªã¯ã¨ã¹ãããµã¼ãã¬ããã«åãã¦ãã¾ãã
3. ãã£ã¬ã¯ããªé層ã®ä½æ
以ä¸ã®ã³ãã³ãã§ããã¸ã§ã¯ã sample-servlet ã®ãã£ã¬ã¯ããªé層ã使ãã¾ãã
mkdir sample-servlet cd sample-servlet mkdir src\main\java mkdir src\main\webapp\WEB-INF
使ããããsrc/main/java é
ä¸ã«ãµã¼ãã¬ããã®ã³ã¼ããç½®ãã¾ããHTML 㯠src/main/webapp ã«ç½®ãã¾ãã
4. pom.xml ã®ä½æ
ãã©ã«ã sample-servlet ã« pom.xml ã使ãã¾ãã
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>sample-servlet</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>11.0.15</version>
<configuration>
<scan>1</scan>
</configuration>
</plugin>
</plugins>
</build>
</project>
ãµã¼ãã¬ããã³ã³ãããèµ·åã§ããããã«ãbuild ã§ Jetty Plugin ãå®ç¾©ãã¦ãã¾ãã
5. åä½ç¢ºèª
以ä¸ã®ã³ãã³ãã§ã³ã³ãããèµ·åãã¾ãã
sample-servlet> mvn jetty:run
èµ·åå¾ã«ãã©ã¦ã¶ã§ä»¥ä¸ã® URL ãéãã¾ãã
http://localhost:8080/upload.html
ç»é¢ãéãããããã¡ã¤ã«ã鏿ãã¦éä¿¡ãã¾ãã

å¦çãå®äºããã¨ãC:/tmp ã«ãã¡ã¤ã«ãä¿åããã¾ãã