struts2 文件上传

 
    //在这里 import 一些包
    ·································  

public class UploadAction extends ActionSupport{

    private File image; //上传的文件

    private String imageFileName; //文件名称

    private String imageContentType; //文件类型
     // getter 、setter 方法省略
   
    public String execute() throws Exception {
       
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
        //D:\apache-tomcat-6.0.18\webapps\struts2_upload\images

        System.out.println("realpath: "+realpath);

        if (image != null) {

           
File savefile = new File(new File(realpath), imageFileName);
            if (!savefile.getParentFile().exists())

                savefile.getParentFile().mkdirs();

            FileUtils.copyFile(image, savefile);

            ActionContext.getContext().put("message", "文件上传成功");

        }


文件上传有三种上传方式,这里写的是copy的方式,
另外两种是
 1. 按字节方式上传


  • 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;       
  • }    







  •   2.按字符方式上传 即“三层管道”

  • 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;       
  • }     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值