新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     >>计算机科学论坛<<     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论Java, J2SE, J2ME, J2EE, 以及Eclipse, NetBeans, JBuilder等Java开发环境,还有JSP, JavaServlet, JavaBean, EJB以及struts, hibernate, spring, webwork2, Java 3D, JOGL等相关技术。
    [返回] 计算机科学论坛计算机技术与应用『 Java/Eclipse 』 → 使用纯jsp+javaBean实现任意文件类型上传的例子。 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 10424 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 使用纯jsp+javaBean实现任意文件类型上传的例子。 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     admin 帅哥哟,离线,有人找我吗?
      
      
      
      威望:9
      头衔:W3China站长
      等级:计算机硕士学位(管理员)
      文章:5255
      积分:18406
      门派:W3CHINA.ORG
      注册:2003/10/5

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给admin发送一个短消息 把admin加入好友 查看admin的个人资料 搜索admin在『 Java/Eclipse 』的所有贴子 点击这里发送电邮给admin  访问admin的主页 引用回复这个贴子 回复这个贴子 查看admin的博客楼主
    发贴心情 使用纯jsp+javaBean实现任意文件类型上传的例子。

    ● 使用纯jsp+javaBean实现任意文件类型上传的例子发信人: yqzh (), 信区: J2EE
    标  题: 使用纯jsp+javaBean实现任意文件类型上传的例子。
    发信站: BBS 水木清华站 (Thu May  6 21:58:36 2004), 站内

    javaBean部分的代码:
    package upload;
    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class uploadBean {
    private String[] sourceFile = new String[255];     //源文件名
    private String[] suffix = new String[255];         //文件后缀名
    private String canSuffix = &quot;.gif.jpg.jpeg.png&quot;;    //可上传的文件后缀名
    private String objectPath = &quot;e:/&quot;;                 //目标文件目录
    private String[] objectFileName = new String[255]; //目标文件名
    private ServletInputStream sis = null;             //输入流
    private String[] description = new String[255];    //描述状态
    private long size = 100 * 1024;                    //限制大小
    private int count = 0;                             //已传输文件数目
    private byte[] b = new byte[4096];                 //字节流存放数组
    private boolean successful = true;
    private Hashtable fields = new Hashtable();
    public uploadBean() {
            }//public uploadBean()
            //设置上传文件的后缀名
    public void setSuffix(String canSuffix) {
    this.canSuffix = canSuffix;
    }
    //设置文件保存路径
    public void setObjectPath(String objectPath) {
    this.objectPath = objectPath;
    }
    //设置文件保存路径
    public void setSize(long maxSize) {
    this.size = maxSize;
    }//public void setSize(long maxSize)
    public void setSourceFile(HttpServletRequest request) throws IOException
    {
    sis = request.getInputStream();
    int a = 0;
    int k = 0;
    String s = &quot;&quot;;
    while ( (a = sis.readLine(b, 0, b.length)) != -1) {
    s = new String(b, 0, a);
    if ( (k = s.indexOf(&quot;filename=&quot;&quot;)) != -1) {
    // 取得文件数据
    s = s.substring(k + 10);
    k = s.indexOf(&quot;&quot;&quot;);
    s = s.substring(0, k);
    sourceFile[count] = s;
    k = s.lastIndexOf(&quot;.&quot;);
    suffix[count] = s.substring(k + 1);
    if (canTransfer(count)) {
    transferFile(count);
    }
    ++count;
    } else if ( (k = s.indexOf(&quot;name=&quot;&quot;)) != -1) {
    // 普通表单输入元素,获取输入元素名字
    String fieldName = s.substring(k+6, s.length()-3);
    sis.readLine(b, 0, b.length);
    StringBuffer fieldValue = new StringBuffer(b.length);
    while ( (a = sis.readLine(b, 0, b.length)) != -1) {
    s = new String(b, 0, a-2);
    if ( (b[0] == 45) &amp;&amp; (b[1] == 45) &amp;&amp; (b[2] == 45) &amp;&amp; (b[3] ==
    45) &amp;&amp; (b[4] == 45)) {
    break;
    } else {
    fieldValue.append(s);
    }
    }
    fields.put(fieldName, fieldValue.toString());
    }
    if (!successful)
    break;
    }
    }
    //取得表单元素值
    public String getFieldValue(String fieldName) {
    if (fields == null || fieldName == null) {
    return null;
    }
    return (String) fields.get(fieldName);
    }
    //取得上传文件数
    public int getCount() {
    return count;
    }
    //取得目标路径
    public String getObjectPath() {
    return objectPath;
    }
    //取得源文件名
    public String[] getSourceFile() {
    return sourceFile;
    }
    //取得目标文件名
    public String[] getObjectFileName() {
    return objectFileName;
    }
    //取得上传状态描述
    public String[] getDescription() {
    return description;
    }
    //判断上传文件的类型
    private boolean canTransfer(int i) {
    suffix[i] = suffix[i].toLowerCase();
    //这个是用来传图片的,各位可以把后缀名改掉或者不要这个条件
    if (sourceFile[i].equals(&quot;&quot;) ||
    (!(canSuffix.indexOf(&quot;.&quot;+suffix[i])&gt;=0))) {
    description[i] = &quot;ERR: File suffix is wrong.&quot;;
    return false;
    }
    else {
    return true;
    }
    }
    //上传文件转换
    private void transferFile(int i) {
    String x = Long.toString(new java.util.Date().getTime());
    try {
    objectFileName[i] = x + &quot;.&quot; + suffix[i];
    FileOutputStream out = new FileOutputStream(objectPath +
    objectFileName[i]);
    int a = 0;
    int k = 0;
    long hastransfered = 0; //标示已经传输的字节数
    String s = &quot;&quot;;
    while ( (a = sis.readLine(b, 0, b.length)) != -1) {
    s = new String(b, 0, a);
    if ( (k = s.indexOf(&quot;Content-Type:&quot;)) != -1) {
    break;
    }
    }
    sis.readLine(b, 0, b.length);
    while ( (a = sis.readLine(b, 0, b.length)) != -1) {
    s = new String(b, 0, a);
    if ( (b[0] == 45) &amp;&amp; (b[1] == 45) &amp;&amp; (b[2] == 45) &amp;&amp; (b[3] == 45)
    &amp;&amp; (b[4] == 45)) {
    break;
    }
    out.write(b, 0, a);
    hastransfered += a;
    if (hastransfered &gt;= size) {
    description[count] = &quot;ERR: The file &quot; + sourceFile[count] +
    &quot; is too large to transfer. The whole process is interrupted.&quot;;
    successful = false;
    break;
    }
    }
    if (successful) {
    description[count] = &quot;Right: The file &quot; + sourceFile[count] +
    &quot; has been transfered successfully.&quot;;
    }
    out.close();
    if (!successful) {
    sis.close();
    File tmp = new File(objectPath + objectFileName[count]);
    tmp.delete();
    }
    }
    catch (IOException ioe) {
    description[i] = ioe.toString();
    }
    }
    public static void main(String[] args) {
    System.out.println(&quot;Test OK&quot;);
    }
    }//public class uploadBean
    jsp第一部分的代码:
    &lt;%@ page contentType=&quot;text/html; charset=GBK&quot; %&gt;
    &lt;html&gt;
    &lt;head&gt;
    &lt;title&gt;
    upload1
    &lt;/title&gt;
    &lt;/head&gt;
    &lt;body bgcolor=&quot;#ffffff&quot;&gt;
    &lt;form action=&quot;upload2.jsp&quot; enctype=&quot;MULTIPART/FORM-DATA&quot; method=&quot;post&quot;&gt;
    选择文件:&lt;input type=&quot;file&quot; name=&quot;filename&quot; /&gt;
    &lt;input type=&quot;submit&quot; value=&quot;上载&quot; /&gt;
    &lt;/form&gt;
    &lt;/body&gt;
    &lt;/html&gt;
    jsp第二部分的代码:
    &lt;%@ page contentType=&quot;text/html; charset=GBK&quot; %&gt;
    &lt;jsp:useBean id=&quot;fileBean&quot; scope=&quot;page&quot; class=&quot;upload.uploadBean&quot; /&gt;
    &lt;%
    fileBean.setObjectPath(&quot;e:\&quot;);//这可以设成要存放文件的路径。
    fileBean.setSize(10000*1024);
    fileBean.setSuffix(&quot;.gif.jpg.png.jpge.html.htm&quot;);
    fileBean.setSourceFile(request);
    String [] saSourceFile = fileBean.getSourceFile();
    String [] saObjectFile = fileBean.getObjectFileName();
    String [] saDescription = fileBean.getDescription();
    String sObjectPath = fileBean.getObjectPath();
    %&gt;
    以上使用Win2000server+Weblogic7.0调试成功。




    --
    告别郁闷,靠的是汗水;
    力争上游,凭的是意志;
    精深广博,要的是交流.

    ※ 来源:·BBS 水木清华站 http://smth.org·[FROM: 166.111.194.*]                 

    索引页面|上一篇|下一篇


       收藏   分享  
    顶(0)
      




    ----------------------------------------------

    -----------------------------------------------

    第十二章第一节《用ROR创建面向资源的服务》
    第十二章第二节《用Restlet创建面向资源的服务》
    第三章《REST式服务有什么不同》
    InfoQ SOA首席编辑胡键评《RESTful Web Services中文版》
    [InfoQ文章]解答有关REST的十点疑惑

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2004/9/23 0:39:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Java/Eclipse 』的所有贴子 点击这里发送电邮给Google AdSense  访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/2 18:42:52

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    62.500ms