「java開発」commons-fileupload 1.3.1を利用してファイルをアップロードする
1.jarパッケージ:
commons-fileupload-1.3.1.jar
commons-io-1.3.2.jar
2.mavenライブラリ
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
3.JSP:
<%@ page contentType="text/html;charset=UTF-8″ language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<h1>これはアップロードテストです</h1>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="text" name="username"/>
<input type="file" name="upfile"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
4.servlet:
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
public class TestuploadServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
boolean flag = ServletFileUpload.isMultipartContent(req);
FileOutputStream fos = null;
InputStream is =null;
try {
if (flag) {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(req);
while (iter.hasNext()){
FileItemStream fis = iter.next();
is = fis.openStream();
if(fis.isFormField()){
System.out.print(fis.getFieldName());
System.out.println(“:"+ Streams.asString(is));
}else{
System.out.println(fis.getName());
String path = req.getSession().getServletContext().getRealPath(“/upload");
path = path+"/"+fis.getName();
System.out.println(path);
fos = new FileOutputStream(path);
byte[] buff = new byte[1024];
int len = 0;
while ((len=is.read(buff))>0){
fos.write(buff,0,len);
}
}
}
}
}catch (Exception e){
}
PrintWriter writer = resp.getWriter();
writer.print(“こんにちは!");
}
private static final long serialVersionUID = 1L;
}
5.web.xml
<?xml version="1.0″ encoding="UTF-8″?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0″>
<servlet>
<servlet-name>upload</servlet-name>
<servlet-class>TestuploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>upload.jsp</welcome-file>
</welcome-file-list>
</web-app>
6.テスト結果:
username:startnews24
startnews24.sql
D:\IdeaProjects_startnews24\fileuploadtest\web\upload/startnews24.sql