使用smartupload.jar 可轻松完成
jar包下载地址 点击打开链接
使用步骤
1 将下载后的jar包放在项目的lib目录下
2创建两个jsp文件
uploadfile.jsp(文件上传的表单页面) 和 uploadsuc.jsp(处理文件上传的页面)
uploadfiel.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>Insert title here</title>
</head>
<body>
<form action="uploadsuc.jsp" method ="post" enctype="multipart/form-data">
用户名:<input type="text" name="username">
文件上传<input type="file" name="upfile" id="upfile">
<input type="submit" value="提交">
</form>
</body>
</html>
uploadsuc.jsp代码
<%@ page language="java" import="java.util.*,com.jspsmart.upload.*" 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>上传处理页面</title>
</head>
<body>
<%
String name ="";
try{
//指定动作
SmartUpload smart = new SmartUpload();
smart.initialize(pageContext);
smart.upload();
name = smart.getRequest().getParameter("username");//注意request的获取方式
//文件保存在upfile文件夹(需要手动创建该文件夹)
smart.save("e:/upfile");
System.out.println("success");
}catch(Exception e){
System.out.println(e);
}
%>
上传者:<%=name%>
<a href="uploadfile.jsp">返回</a>
</body>
</html>
然后在自己创建的保存文件的位置就能看到上传的文件了