出处:
codeChinakey words: cos 文件上传 jsp上传
index.html文件:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<title>无标题文档</title>
</head>
<body>
<!-- enctype的值很重要,upload.jsp为处理上传的jsp-->
<form name="form1" method="post" enctype="multipart/form-data"
action="upload.jsp">
<p>
<input name="file1" type="file">
</p>
<p>
<input name="file2" type="file">
</p>
<p> <input name="file3" type="file">
</p>
<p>
<input type="submit" name="Submit" value="上传">
</p>
</form >
</body>
</html>
upload.jsp文件 :
<%@page import="java.io.*"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@page import="com.oreilly.servlet.multipart.CoverFileRenamePolicy"%>
<%@page contentType="text/html; charset=gb2312" %>
<%
//文件上传后,保存在c:\\upload
String saveDirectory ="c:\\upload";
//每个文件最大5m,最多3个文件,所以
int maxPostSize =3 * 5 * 1024 * 1024 ;
//response的编码为"gb2312",同时采用缺省的文件名冲突解决策略,实现上传
//就这一句就完成上传了,真是很爽
MultipartRequest multi = new MultipartRequest(request, saveDirectory, maxPostSize,"gb2312");
//输出反馈信息
Enumeration files = multi.getFileNames();
while (files.hasMoreElements()) {
System.err.println("ccc");
String name = (String)files.nextElement();
File f = multi.getFile(name);
if(f!=null){
String fileName = multi.getFilesystemName(name);
String lastFileName= saveDirectory+"\\" + fileName;
out.println("上传的文件:"+lastFileName);
out.println("<hr>");
}
}
%> 相关链接:
几种上传方法介绍和比较