UFOUpload实现文件上传
1、UFOUpload介绍:UFOUpload组件是一个运行在Jsp Web Server上的程序,它解析contentType为"multipart/"的Http连接,实现文件上传的组件。
2、如何使用UFOUpload组件?
以UFO Web Server为例(其它Jsp Web Server类似),把UFOUpload_V100.jar放到\lib目录下;即可使用。
3、下面举例介绍如何使用它的文件上传功能。
示例1
getString()得到表单数据内容,isFormField()可判断是否为普通的表单项。
UFOdemo1.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>File upload</title>
</head>
<body>
//必须是multipart的表单数据。
<form name="myform" action="UFOdemo1.jsp" method="post"
enctype="multipart/form-data">
Your name: <br>
<input type="text" name="name" size="15"><br>
File:<br>
<input type="file" name="myfile"><br>
<br>
<input type="submit" name="submit" value="Commit">
</form>
</body>
</html>
UFOdemo1.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"%>
<%@ page import="java.util.*"%>
<%@ page import="com.gm365.upload.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>UFOUpload例子1</title>
</head>
<body>
<%
UFOUpload upload=new UFOUpload();
ArrayList items=upload.parseRequest(request);
if (items!=null){
for (int i=0; i<items.size(); i++){
UFOFile item=(UFOFile)items.get(i);
if (item.isFormField()) {//如果是普通表单项目,显示表单内容。
String fieldName = item.getFieldName();
if (fieldName.equals("name")){ //对应demo1.html中type="text" name="name"
out.print("the field name is: "+item.getString());//显示表单内容。
out.print("<br>");
}
}
else{//如果是上传文件,显示文件名。
out.print("the upload file name is: "+item.getPathFileName());
out.print("<br>");
}
}
}
upload.close();
%>
</body>
</html>
运行结果:仅显示表单内容,显示上传文件名,不对上传的文件进行保存。
示例2
上传两个文件到指定的目录。
UFOdemo2.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>File upload</title>
</head>
<body>
<form name="myform" action="UFOdemo2.jsp" method="post"
enctype="multipart/form-data">
File1:<br>
<input type="file" name="myfile1"><br>
File2:<br>
<input type="file" name="myfile2"><br>
<br>
<input type="submit" name="submit" value="Commit">
</form>
</body>
</html>
UFOdemo2.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"%>
<%@ page import="java.util.*"%>
<%@ page import="com.gm365.upload.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>UFOUpload例子1</title>
</head>
<body>
<%
UFOUpload upload=new UFOUpload();
ArrayList items=upload.parseRequest(request);
String uploadPath="D:\\temp";
if (items!=null){
File uploadDir=new File(uploadPath);
if (!uploadDir.exists()){
uploadDir.mkdirs();
}
for (int i=0; i<items.size(); i++){
UFOFile item=(UFOFile)items.get(i);
String fileName=item.getFileName();
if (fileName!=null){
try{
item.saveAs(uploadPath+"/"+fileName);
out.print("the upload file to: "+uploadPath+"/"+fileName);
out.print("<br>");
}
catch (Exception e){
}
}
}
}
upload.close();
%>
</body>
</html>
运行结果:显示文件上传保存的路径,并且将文件保存
示例3
上传一个文件到指定的目录,并限定文件大小。
UFOdemo3.html
<HTML>
<HEAD>
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=gb2312">
<TITLE>UFOUpload实现文件上传</TITLE>
</HEAD>
<BODY>
<>UFOUpload实现文件上传</P>
<FORM NAME="fileupload" ACTION="UFOdemo3.jsp" method="OST" ENCTYPE="multipart/form-data">
<!--必须指定表单的加密类型为 multipart/form-data -->
<>名称:<INPUT TYPE=TEXT NAME="name" SIZE=12 value="小明"></P>
<;P>性别:<INPUT TYPE=TEXT NAME="sex" SIZE=12 value="男"></P>
<;P>年龄:<INPUT TYPE=TEXT NAME="age" SIZE=12 value="28"></P>
<;P>文件:<INPUT TYPE=FILE NAME="file" SIZE=12></P>
<;P><INPUT TYPE=SUBMIT NAME="submit" VALUE="确定"></P>
</FORM>
</BODY>
</HTML>
UFOdemo3.jsp
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="com.gm365.upload.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>文件传输例子</title>
</head>
<body>
<%
boolean isMultipart=UFOUpload.isMultipartContent(request); //检查表单中是否包含文件
if (isMultipart){
UFOUpload upload=new UFOUpload();
upload.setSizeMax(204800); //允许的最大文件尺寸
upload.setAllowedFiles("gif,jpg,jpeg");
ArrayList items=upload.parseRequest(request);
if (items!=null){
String storeDir=request.getRealPath("/upload_files");
File storeDirF=new File(storeDir);
if (!storeDirF.exists()){
storeDirF.mkdirs();
}
for (int i=0; i<items.size(); i++){
UFOFile fi=(UFOFile)items.get(i);
if (fi.isFormField()){ //如果是表单字段
%>
<%=fi.getFieldName()%>: <%=fi.getString()%><BR>
<%
}
else{ //如果是文件
String fileName=fi.getFileName();
if (fileName!=null && !fileName.equals("")){
File dstFile=new File(storeDir, fileName);
System.out.println("fileName="+fileName);
System.out.println("storeDir="+storeDir);
System.out.println("dstFile="+dstFile.getAbsolutePath());
%>
文件被上传到服务上的实际位置:<%=dstFile.getAbsolutePath()%><br>
<%
fi.saveAs(dstFile);
}
}
}
}
upload.close();
}
%>
</body>
</html>
运行结果:显示UFOdemo3.html提交的表单内容,并保存上传文件于E:\UFO\webapps\ROOT\upload_files下(上传文件必须是GIF,JPG,JPEG的图片文件)。
示例4
利用Servlet来实现文件上传。
UFOdemo4.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>UFOUpload servlet Demo</title>
</head>
<body>
<form name="myform" action="/servlet/Upload" method="post"
enctype="multipart/form-data">
File:<br>
<input type="file" name="myfile"><br>
<br>
<input type="submit" name="submit" value="Commit">
</form>
</body>
</html>
Upload.java
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.gm365.upload.*;
public class Upload extends HttpServlet{
private String tempPath="d:\\temp\\buffer\\";
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
UFOUpload upload=null;
try{
upload=new UFOUpload();
ArrayList items=upload.parseRequest(request);
if (items!=null){
for (int i=0; i<items.size(); i++){
UFOFile fi=(UFOFile)items.get(i);
if (fi.isFile()){
String fileName=fi.getFileName();
if (fileName!=null){
System.out.println("fileName="+fileName);
File savedFile=new File(tempPath, fileName);
fi.saveAs(savedFile);
System.out.print("upload succeed");
}
}
}
}
}
catch (Exception e) {
//可以跳转出错页面
e.printStackTrace();
}
if (upload!=null){
upload.close();
}
}
public void init() throws ServletException{
File tempPathFile=new File(tempPath);
if (!tempPathFile.exists()){
tempPathFile.mkdirs();
}
}
}
运行结果: 将上传文件保存于d:\\temp\\buffer\\下。 |