1、jsp页面操作文件:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<html>
<head>
<title>上传文件 </title>
<link rel="stylesheet" type="text/css" href="../css/uploadfile.css">
<link rel="stylesheet" type="text/css" href="../css/footer.css">
<script type="text/javascript" src="../js/uploadfile.js"></script>
</head>
<body>
<div class="container">
<form action="../upLoadFileServlet" name="uploadform" enctype="multipart/form-data" method="post">
<div class="back"><span class="cleckback" onclick="javascript:history.back()" style="padding-right: 50px;">返回上一级</span></div>
<div class="upfile-div" align="center">
<div class="title-div">上传文件</div>
<table class="toggle-table">
<tr>
<td>上传者姓名:</td>
<td><input type="text" id="uploadfileuser" name="uploadfileusername" class="inputname"/></td>
</tr>
<tr>
<td>上 传 时 间:</td>
<td><input type="datetime-local" class="inputtime"></td>
</tr>
<tr>
<td>选择上传文件:</td>
<td><input type="file" name="uploadfile" class="seclectedfile"/></td>
</tr>
</table>
<div class="toggle-btns">
<input type="submit" value="上传" class="mybtn"/>
<input type="button" value="取消" class="mybtn" onclick="uploadUserCheck()"/>
</div>
</div>
</form>
</div>
<div class="footer"><jsp:include page="footer.jsp"></jsp:include></div>
</body>
</html>
2、uploadresult.jsp:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'uploadresult.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<p>121312313132123132</p>
<%
String msg = (String)request.getAttribute("upload.message");
if (msg != null)
{
%>
<%= msg %>
<%
}
%>
</body>
</html>
3、servlet:
package web.servlet; import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List; import javax.servlet.ServletException;
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; @SuppressWarnings("serial")
public class UpLoadFileServlet extends HttpServlet { private int cachesize = 1024 * 1024; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
request.setCharacterEncoding("gb2312");
String uploadfileUserName = request.getParameter("uploadfileusername"); // System.out.println(uploadfileUserName); DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(new File("e:/tmp/"));//设置文件缓存路径
factory.setSizeThreshold(cachesize); //设置文件上传的上限,-1表示无上限
ServletFileUpload upload = new ServletFileUpload(factory);
if (!ServletFileUpload.isMultipartContent(request)){
return;
}
try { @SuppressWarnings("unchecked")
List<FileItem> items = upload.parseRequest(request);//上传文件并解析出所有字段
Iterator<FileItem> iter = items.iterator();
while(iter.hasNext())
{
FileItem item = iter.next();
if (item.isFormField()) //表单字段
{
System.out.println(item.getFieldName());
System.out.println(item.getString());
}
else
{
if (item.getName() != null && !item.equals(""))
{
System.out.println(item.getName()); File tempFile = new File(item.getName()); //获得临时文件(创建内存文件)
String filename = tempFile.getName(); File file = new File(this.getServletContext().getRealPath("/")+"AcceptedUploadFiles",filename);
// OutputStream os = new FileOutputStream(file);
item.write(file);
request.setAttribute("upload.message", "成功上传文件:"+filename);
}
else
{
request.setAttribute("upload.message", "请选择上传文件");
} }
}
} catch (Exception e) {
request.setAttribute("upload.message", "文件上传失败");
}
//dispatcher跳转其他资源
request.getRequestDispatcher("jsp/uploadresult.jsp").forward(request,response);
} }