上传表单:fileupload.html
<html>
<head>
<title>fileupload Test</title>
</head>
<body>
<form action="fileupload.jsp" method="post" enctype="multipart/form-data">
姓名:<input type="text" name="uname"><br>
照片:<input type="file" name="pic"><br>
<input type ="submit" value="上传">
<input type="reset" value="重置">
</form>
</body>
</html>
FileUpload的具体上传操作与SmartUpload相比有着很高的复杂度。
FileUpload上传的基本操作步骤:
①创建磁盘工厂:DiskFileItemFactory factory = new DiskFileItemFactory();
②创建处理的工具:ServletFileUpload upload = new ServletFileUpload(factory);
③设置上传文件大小:upload.setFileSizeMax(3145728);
④接收全部内容:List<FileItem> items = upload.parseRequest(request);
接收上传文件:fileupload.jsp
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<html>
<head><title>fileupload Test</title></head>
<body>
<%
DiskFileItemFactory factory = new DiskFileItemFactory() ;
ServletFileUpload upload = new ServletFileUpload(factory) ;
upload.setFileSizeMax(3 * 1024 * 1024) ; // 只能上传3M
List<FileItem> items = upload.parseRequest(request) ; // 接收全部内容
Iterator<FileItem> iter = items.iterator() ;
while(iter.hasNext()){
FileItem item = iter.next() ;
String fieldName = item.getFieldName() ; // 取得表单控件的名称
%>
<ul><h4><%=fieldName%> --> <%=item.isFormField()%></h4>
<%
if(!item.isFormField()){ // 不是普通文本
String fileName = item.getName() ; // 取得文件的名称
String contentType = item.getContentType() ; // 文件类型
long sizeInBytes = item.getSize() ;
%>
<li>上传文件名称:<%=fileName%>
<li>上传文件类型:<%=contentType%>
<li>上传文件大小:<%=sizeInBytes%>
<%
} else {
String value = item.getString() ;
%>
<li>普通参数:<%=value%>
<%
}
%> </ul>
<%
}
%>
</body>
</html>
保存上传内容
定义上传表单:fileupload2.jsp
<html>
<head><title>fileupload Test</title></head>
<body>
<form action="fileupload2.jsp" method="post" enctype="multipart/form-data">
姓名:<input type="text" name="uname"><br>
照片:<input type="file" name="pic1"><br>
照片:<input type="file" name="pic2"><br>
照片:<input type="file" name="pic3"><br>
<input type="submit" value="上传">
<input type="reset" value="重置">
</form>
</body>
</html>
保存上传内容:fileupload2.jsp
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*,java.io.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="cn.mldn.lxh.util.*"%>
<html>
<head><title>fileupload Test</title></head>
<body>
<%
DiskFileItemFactory factory = new DiskFileItemFactory() ;
factory.setRepository(new File(this.getServletContext().getRealPath("/") + "uploadtemp")) ; // 更准确的说是一个临时文件
ServletFileUpload upload = new ServletFileUpload(factory) ;
upload.setFileSizeMax(3 * 1024 * 1024) ; // 只能上传3M
List<FileItem> items = upload.parseRequest(request) ; // 接收全部内容
Iterator<FileItem> iter = items.iterator() ;//将全部的内容变为Iterator实例
IPTimeStamp its = new IPTimeStamp(request.getRemoteAddr()) ;
while(iter.hasNext()){
FileItem item = iter.next() ;
String fieldName = item.getFieldName() ; // 取得表单控件的名称
%>
<ul><h4><%=fieldName%> --> <%=item.isFormField()%></h4>
<%
if(!item.isFormField()){ // 不是普通文本
File saveFile = null ;//定义保存文件
InputStream input = null ;//定义文件的输入流,用于读取源文件
OutputStream output = null ;//定义文件的输出流,用于保存文件
input = item.getInputStream() ;//取得上传文件的输入流
output = new FileOutputStream(new File(this.getServletContext().getRealPath("/")+"upload"+File.separator+its.getIPTimeRand()+"."+item.getName().split("\\.")[1])) ;//定义输出文件的路径
int temp = 0 ;
byte data[] = new byte[512] ;//分块保存
while((temp=input.read(data,0,512))!=-1){//依次读取内容
output.write(data) ; // 分块保存
}
input.close() ;
output.close() ;
} else {
String value = item.getString() ;//取得表单的内容
%>
<li>普通参数:<%=value%>
<%
}
%> </ul>
<%
}
%>
</body>
</html>
开发FileUpload组件的专属操作类
方法:
NO. | 方法 | 类型 | 描述 |
---|---|---|---|
1 | public FileUploadTools(HttpServletRequest request,int maxSize,String tempDir) throws Exception | 构造 | 实例化FileUploadTools类,并接收request对象、最大上传文件限制、上传临时保存目录路径 |
2 | public String getParameter(String name) | 普通 | 根据参数名称取得参数内容 |
3 | public String [] getParameterValues(String name) | 普通 | 根据参数名称取得一组参数内容 |
4 | public Map<String ,FileItem> getUploadFiles() | 普通 | 取得全部上传的文件 |
5 | public List<String> saveAll(String saveDir) throws IOException | 普通 | 自动保存全部的上传文件,并将已上传文件的名称返回给调用处 |
fileupload_demo03.htm
<html>
<head><title>fileupload Test</title></head>
<body>
<form action="fileupload_demo03.jsp" method="post" enctype="multipart/form-data">
姓名:<input type="text" name="uname"><br>
兴趣:<input type="checkbox" name="inst" value="Swing">游泳
<input type="checkbox" name="inst" value="Song">唱歌
<input type="checkbox" name="inst" value="Run">跑步<br>
照片:<input type="file" name="pic1"><br>
照片:<input type="file" name="pic2"><br>
照片:<input type="file" name="pic3"><br>
<input type="submit" value="上传">
<input type="reset" value="重置">
</form>
</body>
</html>
fileupload_demo03.jsp
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*,java.io.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="cn.mldn.lxh.util.*"%>
<html>
<head><title>fileupload Test</title></head>
<body>
<%
FileUploadTools fut = new FileUploadTools(request,3*1024*1024,this.getServletContext().getRealPath(".")+"uploadtemp") ;
String name = fut.getParameter("uname") ;
String inst[] = fut.getParameterValues("inst") ;
List<String> all = fut.saveAll(this.getServletContext().getRealPath("/")+"upload"+java.io.File.separator) ;
%>
<h3>姓名:<%=name%><h3>
<h3>兴趣:
<%
for(int x=0;x<inst.length;x++){
%>
<%=inst[x]%>、
<%
}
%>
</h3>
<%
Iterator<String> iter = all.iterator() ;
while(iter.hasNext()){
%>
<img src="../upload/<%=iter.next()%>">
<%
}
%>
</body>
</html>