struts2文件上传的采用的三种方式解析

上传就是将信息从个人计算机(本地计算机)传递到中央计算机(远程计算机)系统上,让网络上的人都能看到。将制作好的网页、文字、图片等发布到互联网上去,以便让其他人浏览、欣赏。这一过程称为上传。

1 SmartUpload 用的最多的一个组件,已经不再更新了,可以实现上传和下载

3 J2KUpload java2000实现的文件上传组件,全部使用内存,适合多个不超过10M的小文件

1、/** 按copy方式上传 */

public String uploadFile(){     

    /**保存的具体路径*/    

    String savepath = getSavePath();     

    /**根据保存的路径创建file对象*/    


    File file = new File(savepath);     


    if(!file.exists()){     


        /**创建此文件对象路径*/    

        file.mkdirs();     
    }     

    try {     


        /**使用的是:org.apache.commons.io.FileUtils FileUtils*/    


        FileUtils.copyFile(pic, new File(file,getPicFileName()));     


    } catch (IOException e) {     

        e.printStackTrace();     
    }     

    return SUCCESS;     

}  



1、getSavePath()方法中,ServletActionContext().getServletContext().getRealPath

2、我个人认为这种方式是简单易用的。按copy方式上传使用的是Apache公司的

2、/** 按字节方式上传 */


public String uploadFile(){          

    /** 文件的写操作 */      


    FileInputStream fis = null;      


    FileOutputStream fos = null;     


        /** 保存的路径 */    

    String savepath = getSavePath();     

    /** 根据保存的路径创建file对象 */    


    File file = new File(savepath);     


    /** file对象是否存在   */    


    if (!file.exists()) {     


        /** 创建此文件对象路径  */    

        file.mkdirs();     
    }     

    try {     


        /** 创建输入流 */    


        fis = new FileInputStream(pic);     


        /** 输出流 更据文件的路径+文件名称创建文件对象 */    


        fos = new FileOutputStream(file + "//" + getPicFileName());     


        /** 读取字节   */    


        byte b[] = new byte[1024];     


        int n = 0;     


        /** 读取操作   */    


        while ((n = fis.read(b)) != -1) {     


            /** 写操作   */    


            fos.write(b, 0, n);     

        }     

        /** 关闭操作  */    


        if (fis != null) {     

            fis.close();     
        }     

        if (fos != null) {     

            fos.close();     
        }     

    } catch (Exception e) {     

        e.printStackTrace();     
    }     

    return SUCCESS;     

}   




public String uploadFile(){     

    /** 文件的写操作 */    


    BufferedReader br =null;     


    BufferedWriter bw = null;        


    /** 保存的路径 */    

    String savepath = getSavePath();     

    /** 根据保存的路径创建file对象   */    


    File file = new File(savepath);     


    /** file对象是否存在  */    


    if (!file.exists()) {                


        /** 创建此文件对象路径  */    

        file.mkdirs();     
    }     

    try {     


        /**   创建一个BufferedReader  对象*/    


        br = new BufferedReader(new InputStreamReader(new FileInputStream     

    
(pic)));                 

        bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream     

    

(file + "//" + getPicFileName())));     

                 

        // 读取字节     


        char b[] = new char[1024];     


        int n = 0;     


        // 读取操作     


        while ((n = br.read(b)) != -1) {     


            // 写操作     


            bw.write(b, 0, n);     

        }     

        // 关闭操作     


        if (br != null) {     

            br.close();     
        }     

        if (bw != null) {     

            bw.close();     
        }     

    } catch (Exception e) {     

        e.printStackTrace();     
    }     

    return SUCCESS;     

}   


引自:[url]http://developer.51cto.com/art/201105/259767.htm[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值