利用jsp和servlet实现文件上传,我们首先需要为该项目的lib目录下导入两个jar包:
commons.fileupload-1.2.0.jar
commons.io-1.4.0.jar
jsp部分
我们在jsp文件中创建一个简单的表单,action提交到UploadFileServlet页面,method设置为post,第一个ingput标签中type属性设置为file,第二个input标签为提交标签, 如下面代码:
<%@ 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>Insert title here</title>
</head>
<body>
<form action="UploadFileServlet" method="post" enctype="multipart/form-data">
<input type="f