package upload;
import java.io.*;
import java.util.*;
import com.sun.image.codec.jpeg.*;
import java.awt.image.*;
import java.awt.*;
import java.net.*;
import java.applet.*;
import java.sql.*;
//缩略图类,  
//本java类能将jpg图片文件,进行等比或非等比的大小转换。  
//具体使用方法  
//s_pic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))  
public class DwindlePic {
    String InputDir; // 输入图路径
    String OutputDir; // 输出图路径
    String InputFileName; // 输入图文件名
    String OutputFileName; // 输出图文件名
    int OutputWidth = 80; // 默认输出图片宽
    int OutputHeight = 80; // 默认输出图片高
    int rate = 0;
    boolean proportion = true; // 是否等比缩放标记(默认为等比缩放)
    public DwindlePic() {
        // 初始化变量
        InputDir = "";
        OutputDir = "";
        InputFileName = "";
        OutputFileName = "";
        OutputWidth = 80;
        OutputHeight = 80;
        rate = 0;
    }
    public boolean s_pic() {
        // BufferedImage image;
        // String NewFileName;
        // 建立输出文件对象
        File file = new File(OutputDir + OutputFileName);
        FileOutputStream tempout = null;
        try {
            tempout = new FileOutputStream(file);
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
        Image img = null;
        Toolkit tk = Toolkit.getDefaultToolkit();
        Applet app = new Applet();
        MediaTracker mt = new MediaTracker(app);
        try {
            img = tk.getImage(InputDir + InputFileName);
            mt.addImage(img, 0);
            mt.waitForID(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (img.getWidth(null) == -1) {
            System.out.println(" can't read,retry!" + "<BR>");
            return false;
        } else {
            int new_w;
            int new_h;
            if (this.proportion == true) { // 判断是否是等比缩放.
                // 为等比缩放计算输出的图片宽度及高度
                double rate1 = ((double) img.getWidth(null)) / (double) OutputWidth + 0.1;
                double rate2 = ((double) img.getHeight(null)) / (double) OutputHeight + 0.1;
                double rate = rate1 > rate2 ? rate1 : rate2;
                new_w = (int) (((double) img.getWidth(null)) / rate);
                new_h = (int) (((double) img.getHeight(null)) / rate);
            } else {
                new_w = OutputWidth; // 输出的图片宽度
                new_h = OutputHeight; // 输出的图片高度
            }
            BufferedImage buffImg = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
            Graphics g = buffImg.createGraphics();
            g.setColor(Color.white);
            g.fillRect(0, 0, new_w, new_h);
            g.drawImage(img, 0, 0, new_w, new_h, null);
            g.dispose();
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(tempout);
            try {
                encoder.encode(buffImg);
                tempout.close();
            } catch (IOException ex) {
                System.out.println(ex.toString());
            }
        }
        return true;
    }
    public boolean s_pic(String InputDir, String OutputDir, String InputFileName, String OutputFileName) {
        // 输入图路径
        this.InputDir = InputDir;
        // 输出图路径
        this.OutputDir = OutputDir;
        // 输入图文件名
        this.InputFileName = InputFileName;
        // 输出图文件名
        this.OutputFileName = OutputFileName;
        return s_pic();
    }
    public boolean s_pic(String InputDir, String OutputDir, String InputFileName, String OutputFileName, int width, int height, boolean gp) {
        // 输入图路径
        this.InputDir = InputDir;
        // 输出图路径
        this.OutputDir = OutputDir;
        // 输入图文件名
        this.InputFileName = InputFileName;
        // 输出图文件名
        this.OutputFileName = OutputFileName;
        // 设置图片长宽
        setW_H(width, height);
        // 是否是等比缩放 标记
        this.proportion = gp;
        return s_pic();
    }
    public void setInputDir(String InputDir) {
        this.InputDir = InputDir;
    }
    public void setOutputDir(String OutputDir) {
        this.OutputDir = OutputDir;
    }
    public void setInputFileName(String InputFileName) {
        this.InputFileName = InputFileName;
    }
    public void setOutputFileName(String OutputFileName) {
        this.OutputFileName = OutputFileName;
    }
    public void setOutputWidth(int OutputWidth) {
        this.OutputWidth = OutputWidth;
    }
    public void setOutputHeight(int OutputHeight) {
        this.OutputHeight = OutputHeight;
    }
    public void setW_H(int width, int height) {
        this.OutputWidth = width;
        this.OutputHeight = height;
    }
    public static void main(String[] a) {
        // s_pic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度)
        DwindlePic mypic = new DwindlePic();
        System.out.println(mypic.s_pic("d:/", "d:/", "200.jpg", "bbb.jpg", 130, 80, true));
    }
}
ACTION:
package upload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 文件上传类,图片类型,txt类型,zip类型
 * 
 * @author Administrator
 * 
 */
public class Upload extends ActionSupport {
    private static final long serialVersionUID = -8204063374280945416L;
    private File upload;// 路径
    private String uploadFileName;// 原文件名
    private String uploadContentType;// 文件类型
    private String imagePath;// 配置图片路径
    private String image_smallPath;// 配置小图片路径
    private String file_zipPath;// 配置文件路径
    private String image_smalllook;
    private String imagePath_look;
    private String file_zip_look;
    public String upload() throws Exception {
        // FileUtils.copyFile(doc, target);
        File image = new File(getImagePath());
        if (!image.exists()) {
            image.mkdir();
        }
        File image_small = new File(getImage_smallPath());
        if (!image_small.exists()) {
            image_small.mkdir();
        }
        File file_zip = new File(getFile_zipPath());
        if (!file_zip.exists()) {
            file_zip.mkdir();
        }
        // 得到上传文件的后缀名
        String uploadName = getUploadContentType();
        String lastuploadName = uploadName.substring(uploadName.indexOf("/") + 1, uploadName.length());
        // 得到文件的新名字
        String fileNewName = generateFileName(getUploadFileName());
        if (lastuploadName.equals("jpeg")) {
            System.out.println("图片类型文件");
            // 创建文件
            FileOutputStream fos = new FileOutputStream(getImagePath() + "/" + fileNewName);
            FileInputStream fis = new FileInputStream(getUpload());
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = fis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            DwindlePic mypic = new DwindlePic();
            // s_pic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度)
            mypic.s_pic(getImagePath() + "/", getImage_smallPath() + "/", fileNewName, fileNewName, 120, 80, true);
        } else {
            System.out.println("其他类型文件");
            // 创建文件
            FileOutputStream fos = new FileOutputStream(getFile_zipPath() + "/" + fileNewName);
            FileInputStream fis = new FileInputStream(getUpload());
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = fis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
        }
        imagePath_look = imagePath + "/" + fileNewName;
        image_smalllook = image_smallPath + "/" + fileNewName;
        file_zip_look = file_zipPath + "/" + fileNewName;
        System.out.println(imagePath_look);
        System.out.println(image_smalllook);
        System.out.println(file_zip_look);
        return SUCCESS;
    }
    /**
     * 重命名文件
     * 
     * @param fileName
     * @return
     */
    private String generateFileName(String fileName) {
        DateFormat format = new SimpleDateFormat("yyMMddHHmmss");
        String formatDate = format.format(new Date());
        int random = new Random().nextInt(10000);
        int position = fileName.lastIndexOf(".");
        String extension = fileName.substring(position);
        return formatDate + random + extension;
    }
    public File getUpload() {
        return upload;
    }
    public void setUpload(File upload) {
        this.upload = upload;
    }
    public String getUploadFileName() {
        return uploadFileName;
    }
    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }
    public String getUploadContentType() {
        return uploadContentType;
    }
    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }
    public String getImagePath() {
        return ServletActionContext.getServletContext().getRealPath(imagePath);
    }
    public void setImagePath(String imagePath) {
        this.imagePath = imagePath;
    }
    public String getImage_smallPath() {
        return ServletActionContext.getServletContext().getRealPath(image_smallPath);
    }
    public void setImage_smallPath(String imageSmallPath) {
        image_smallPath = imageSmallPath;
    }
    public String getFile_zipPath() {
        return ServletActionContext.getServletContext().getRealPath(file_zipPath);
    }
    public void setFile_zipPath(String fileZipPath) {
        file_zipPath = fileZipPath;
    }
    public String getImage_smalllook() {
        return image_smalllook;
    }
    public void setImage_smalllook(String imageSmalllook) {
        image_smalllook = imageSmalllook;
    }
    public String getImagePath_look() {
        return imagePath_look;
    }
    public void setImagePath_look(String imagePathLook) {
        imagePath_look = imagePathLook;
    }
    public String getFile_zip_look() {
        return file_zip_look;
    }
    public void setFile_zip_look(String fileZipLook) {
        file_zip_look = fileZipLook;
    }
}
Struts.xml
<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC  
         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
         "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="upload" namespace="/imager" extends="struts-default">
        <!-- 配置处理文件上传的Action -->
        <action name="upload" class="upload.UploadAction" method="upload">
            <interceptor-ref name="fileUpload">
                <!-- 通过动态设置allowTypes的属性来动态指定允许上传的文件类型 -->
                <param name="allowTypes">
                    image/png,image/gif,image/jpeg,
                    application/zip,application/vnd.ms-word,text/plain        
            </param>
                <param name="maximumSize">50485760</param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack" />
            <!-- 动态设置Action的属性值 -->
            <param name="imagePath">/image</param>
            <param name="image_smallPath">/image_small</param>
            <param name="file_zipPath">/file</param>
            <!-- 配置Struts2默认的视图资源 -->
            <result>/success.jsp</result>
            <!-- 配置该应用的input逻辑视图 -->
            <result name="input"> /index.jsp</result>
        </action>
        <action name="download" class="upload.DownloadAction">
            <result type="stream">
                <param name="contentType">application/octet-stream</param>
                <param name="inputName">inputStream</param>
                <param name="contentDisposition">attachment;filename="${fileName}"</param>
                <param name="bufferSize">4096</param>
            </result>
        </action>
    </package>
</struts> 
页面:
<%@ page language="java"  pageEncoding="utf-8"%>  
 <%@ taglib prefix="s" uri="/struts-tags" %>  
   
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
 <html>  
   <head>  
    
       
     <title>My JSP 'index.jsp' starting page</title>  
     <meta http-equiv="pragma" content="no-cache">  
     <meta http-equiv="cache-control" content="no-cache">  
     <meta http-equiv="expires" content="0">      
     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
     <meta http-equiv="description" content="This is my page">  
     <!--  
     <link rel="stylesheet" type="text/css" href="styles.css">  
     -->  
   </head>  
   <s:form method="post" action="imager/upload" enctype="multipart/form-data">  
   <s:textfield name="title" label="title"></s:textfield>  
   <s:file name="upload" label="file"></s:file>  
   <s:submit></s:submit>  
   </s:form>  
   <body>  
   
   </body>  
 </html>
以下是经过修改的图片上传接口
工具类:
package upload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
public class uploadUtil {
    private final static String imagePath="/image";// 配置图片路径
    private final static String image_smallPath="/image_small";// 配置小图片路径
    private final static String file_zipPath="/file";// 配置文件路径
    public String uploadImage1(HttpServletRequest request, File getUpload, String getUploadContentType,
            String getUploadFileName) throws IOException {
        String getImagePath = request.getRealPath(imagePath);
        String getImage_smallPath = request.getRealPath(image_smallPath);
        File image = new File(getImagePath);
        if (!image.exists()) {
            image.mkdir();
        }
        File image_small = new File(getImage_smallPath);
        if (!image_small.exists()) {
            image_small.mkdir();
        }
        // 得到上传文件的后缀名
        String uploadName = getUploadContentType;
        String lastuploadName = uploadName.substring(uploadName.indexOf("/") + 1, uploadName.length());
        // 得到文件的新名字
        String fileNewName = generateFileName(getUploadFileName);
        FileOutputStream fos = new FileOutputStream(getImagePath + "/" + fileNewName);
        FileInputStream fis = new FileInputStream(getUpload);
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = fis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
        }
        DwindlePic mypic = new DwindlePic();
        // s_pic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度)
        mypic.s_pic(getImagePath + "/", getImage_smallPath + "/", fileNewName, fileNewName, 120, 80, true);
        return imagePath + "/" + fileNewName;
    }
    public String uploadfile(ServletActionContext context,File getUpload, String getUploadContentType,
            String getUploadFileName) throws IOException {
        
        String getFile_zipPath = context.getServletContext().getRealPath(file_zipPath);
        File file_zip = new File(getFile_zipPath);
        if (!file_zip.exists()) {
            file_zip.mkdir();
        }
        // 得到上传文件的后缀名
        String uploadName = getUploadContentType;
        String lastuploadName = uploadName.substring(uploadName.indexOf("/") + 1, uploadName.length());
        // 得到文件的新名字
        String fileNewName = generateFileName(getUploadFileName);
        // 创建文件
        FileOutputStream fos = new FileOutputStream(getFile_zipPath + "/" + fileNewName);
        FileInputStream fis = new FileInputStream(getUpload);
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = fis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
        }
        return file_zipPath + "/" + fileNewName;
    }
    /**
     * 重命名文件
     * 
     * @param fileName
     * @return
     */
    private String generateFileName(String fileName) {
        DateFormat format = new SimpleDateFormat("yyMMddHHmmss");
        String formatDate = format.format(new Date());
        int random = new Random().nextInt(10000);
        int position = fileName.lastIndexOf(".");
        String extension = fileName.substring(position);
        return formatDate + random + extension;
    }
}
页面:
<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC  
         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
         "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="upload" namespace="/imager" extends="struts-default">
        <!-- 配置处理文件上传的Action -->
        <action name="upload" class="upload.UploadAction" method="upload">
            <interceptor-ref name="fileUpload">
                <!-- 通过动态设置allowTypes的属性来动态指定允许上传的文件类型 -->
                <param name="allowTypes">
                    image/png,image/gif,image/jpeg,
                    application/zip,application/vnd.ms-word,text/plain        
            </param>
                <param name="maximumSize">50485760</param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack" />
            <!-- 动态设置Action的属性值 
            <param name="imagePath">/image</param>
            <param name="image_smallPath">/image_small</param>
            <param name="file_zipPath">/file</param>
            
            -->
            <!-- 配置Struts2默认的视图资源 -->
            <result>/success.jsp</result>
            <!-- 配置该应用的input逻辑视图 -->
            <result name="input"> /index.jsp</result>
        </action>
        <action name="download" class="upload.DownloadAction">
            <result type="stream">
                <param name="contentType">application/octet-stream</param>
                <param name="inputName">inputStream</param>
                <param name="contentDisposition">attachment;filename="${fileName}"</param>
                <param name="bufferSize">4096</param>
            </result>
        </action>
    </package>
</struts> 
Action:
 package upload;
package upload;

 import java.io.File;
import java.io.File;
 import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequest;
 import org.apache.struts2.ServletActionContext;
import org.apache.struts2.ServletActionContext;
 import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionSupport;


 /** *//**
/** *//**
 * 文件上传类,图片类型,txt类型,zip类型
 * 文件上传类,图片类型,txt类型,zip类型
 *
 * 
 * @author Administrator
 * @author Administrator
 *
 * 
 */
 */

 public class UploadAction extends ActionSupport
public class UploadAction extends ActionSupport  {
{

 private static final long serialVersionUID = -8204063374280945416L;
    private static final long serialVersionUID = -8204063374280945416L;

 private File upload;// 路径
    private File upload;// 路径
 private String uploadFileName;// 原文件名
    private String uploadFileName;// 原文件名
 private String uploadContentType;// 文件类型
    private String uploadContentType;// 文件类型


 public String upload() throws Exception
    public String upload() throws Exception  {
{
 uploadUtil uploadutil=new uploadUtil();
           uploadUtil uploadutil=new uploadUtil();
 HttpServletRequest request = ServletActionContext.getRequest();
           HttpServletRequest request = ServletActionContext.getRequest();
 
           
 uploadutil.uploadImage1(request, getUpload(), getUploadContentType(), getUploadFileName());
           uploadutil.uploadImage1(request, getUpload(), getUploadContentType(), getUploadFileName());

 return SUCCESS;
        return SUCCESS;
 }
    }


 public File getUpload()
    public File getUpload()  {
{
 return upload;
        return upload;
 }
    }


 public void setUpload(File upload)
    public void setUpload(File upload)  {
{
 this.upload = upload;
        this.upload = upload;
 }
    }


 public String getUploadFileName()
    public String getUploadFileName()  {
{
 return uploadFileName;
        return uploadFileName;
 }
    }


 public void setUploadFileName(String uploadFileName)
    public void setUploadFileName(String uploadFileName)  {
{
 this.uploadFileName = uploadFileName;
        this.uploadFileName = uploadFileName;
 }
    }


 public String getUploadContentType()
    public String getUploadContentType()  {
{
 return uploadContentType;
        return uploadContentType;
 }
    }


 public void setUploadContentType(String uploadContentType)
    public void setUploadContentType(String uploadContentType)  {
{
 this.uploadContentType = uploadContentType;
        this.uploadContentType = uploadContentType;
 }
    }
 
    
 }
}
因为无法得到ServletActionContext 
ServletActionContext context=(ServletActionContext) ServletActionContext.getServletContext();
 context.getServletContext().getRealPath(path);
所以使用
HttpServletRequest request = ServletActionContext.getRequest();
request.getRealPath(path);