---
title: "struts2文件上传的采用的三种方式解析"
author: "starbhhc"
date: "2012-02-06T14:00:55Z"
tags: []
categories: ["编程语言"]
slug: "struts2"
draft: false
lastmod: "2012-02-06T14:00:55Z"
id: 1395367
url: "https://starbhhc.iteye.com/blog/1395367"
---

文件上传几乎是每个项目实现的一个必须的模块。  
    上传就是将信息从个人计算机（本地计算机）传递到中央计算机（远程计算机）系统上，让网络上的人都能看到。将制作好的网页、文字、图片等发布到[互联网](http://internet.chinaitlab.com/)上去，以便让其他人浏览、欣赏。这一过程称为上传。  
    **JAVA实现文件上传的几个组件：**  
    1 SmartUpload 用的最多的一个组件，已经不再更新了，可以实现上传和[下载](http://download.chinaitlab.com/)  
    2 FileUpload Apache实现的文件上传组件，功能齐备  
    3 J2KUpload java2000实现的文件上传组件，全部使用内存，适合多个不超过10M的小文件  
    下面具体说说FileUpload Apache实现的文件上传组件。  
    1、/\** 按copy方式上传 \*/  
    [Java](http://java.chinaitlab.com/)代码  
    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  
    （savePath+"[\\\\"+getPicFileName]()（））； ，这个主要是一个文件的实际路径  
    2、我个人认为这种方式是简单易用的。按copy方式上传使用的是Apache公司的  
    org.apache.commons.io.FileUtils包里的FileUtils.java.

 

2、/\** 按字节方式上传 \*/  
    [Java](http://java.chinaitlab.com/)代码  
    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；  
    }  
     3、/\** 按字符方式上传 即“三层管道” \*/  
    Java代码  
    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；  
    }  
    **备注：**  
    第二种上传方式没有第三种上传方式效率高。  
    建议：  
    最好用第一种方式上传，次之使用第三种方式上传，最后再使用第二种方式上传。