| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 568 人关注过本帖
标题:UFOUpload组件实现文件上传
只看楼主 加入收藏
nbsven
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2009-4-26
结帖率:100%
收藏
 问题点数:0 回复次数:0 
UFOUpload组件实现文件上传
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>
<P>UFOUpload实现文件上传</P>
 
<FORM NAME="fileupload" ACTION="UFOdemo3.jsp" method="POST" ENCTYPE="multipart/form-data">
<!--必须指定表单的加密类型为 multipart/form-data -->
<P>名称:<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="*" %>
<%@ 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
import
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\\下。
搜索更多相关主题的帖子: UFOUpload 组件 文件 
2009-08-08 07:35
快速回复:UFOUpload组件实现文件上传
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027443 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved