当前位置 : 主页 > 编程语言 > java >

jsp实现多文件上传

来源:互联网 收集:自由互联 发布时间:2021-06-25
1.新建一个Dynamic Web Project项目,整体的框架如图所示: 2.UploadServlet.java里面的代码如下所示: package servlet;import java.io.File;import java.io.IOException;import java.util.Iterator;import java.util.List;impor

1.新建一个Dynamic Web Project项目,整体的框架如图所示:



2.UploadServlet.java里面的代码如下所示:

package servlet;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import util.MyUtil;

@WebServlet(name = "upload", urlPatterns = { "/upload" })
public class UploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		DiskFileItemFactory itemFactory = new DiskFileItemFactory();
		File tempDir = new File("D:\\temp");
		if (!tempDir.exists()) {
			tempDir.mkdir();
		}
		itemFactory.setRepository(tempDir);
		ServletFileUpload sfu = new ServletFileUpload(itemFactory);
		sfu.setFileSizeMax(10 * 1024 * 1024);
		List<FileItem> fileItems = null;
		RequestDispatcher dis1 = request
				.getRequestDispatcher("uploadSuccess.jsp");
		RequestDispatcher dis2 = request.getRequestDispatcher("uploadFail.jsp");
		try {
			fileItems = sfu.parseRequest(request);
			Iterator<FileItem> itr = fileItems.iterator();
			File uploadFileDir = new File("D:\\uploadFile");
			if (!uploadFileDir.exists()) {
				uploadFileDir.mkdir();
			}
			while (itr.hasNext()) {
				FileItem fitem = itr.next();
				if (!fitem.isFormField()) {
					String name = fitem.getName();
					long size = fitem.getSize();
					if ((name == null || "".equals(name)) && size == 0) {
						continue;
					}
					int index = name.lastIndexOf(File.separator);
					if (index > 0) {
						name = name.substring(index + 1, name.length());
					}
					String fileType = name.substring(name.lastIndexOf("."));
					String newName = MyUtil.getStringID() + fileType;
					File newFile = new File(uploadFileDir, newName);
					fitem.write(newFile);
				}
			}
			dis1.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			dis2.forward(request, response);
		}
	}

}

3.MyUtil.java里面的代码如下所示:
package util;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

public class MyUtil {

	public static String getStringID() {
		String id = null;
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
		id = sdf.format(date);
		return id;
	}

	public static String getParameter(HttpServletRequest request, String name) {
		return (String) request.getAttribute(name);
	}

}

4.selectFile.jsp里面的代码如下所示:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example</title>
</head>
<body>
	<form action="upload" method="post" enctype="multipart/form-data">
		<table>
			<tr>
				<td>请选择要上传的文件:</td>
				<td><input type="file" name="firstFile"></td>
			</tr>
			<tr>
				<td>请选择要上传的文件:</td>
				<td><input type="file" name="secondFile"></td>
			</tr>
			<tr>
				<td align="right"><input type="reset" value="重填"></td>
				<td><input type="submit" value="上传"></td>
			</tr>
		</table>
	</form>
</body>
</html>

5.uploadSuccess.jsp里面的代码如下所示:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example</title>
</head>
<body>
	<h2>上传成功!</h2>
</body>
</html>

6.uploadFail.jsp里面的代码如下所示:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example</title>
</head>
<body>
	<h2>上传失败!</h2>
</body>
</html>

7.在浏览器里面输入http://localhost:8080/Servlet/selectFile.jsp运行之后得到的结果如下所示:




网友评论