解决方案 »

  1.   

    给你一下我使用的上传路径处理的函数:
    action:
    // 文件操作
    String realPath = FileUtil.getServicePathMk(FileUtil.COMPLAINTFILEPATH);
    FileUtil.fileMoveTo(upfile,new File(realPath + "\\" + model.getcImage()));FileUtil:
    import java.io.File;import org.apache.struts2.ServletActionContext;/**
     * 文件操作类
     * @author Daniel.Yim
     * 
     */
    public class FileUtil {
    //定义项目使用的文件路径
    public static final String COMPLAINTFILEPATH="/files/complaint";
    //Note 可以在此添加自己的文件路径

    /**
     * 获取服务器路径
     * 
     * 依据参数指定的相对路径获取服务器的真实文件路径,路径可能会不存在。如果路径不存在则返回null
     * 
     * @param path 定义的项目根目录的相对路径
     * @return 真实的服务器路径
     */
    public static String getServicePath(String path)
    {
    return ServletActionContext.getServletContext().getRealPath(path);
    }

    /**
     * 获取服务器路径
     * 
     * 依据参数指定的相对路径获取服务器的真实文件路径,如果路径不存在,则依据create参数进行创建
     * 
     * @param path
     * @param create
     * @return
     */
    public static String getServicePathMk(String path)
    {
    String serpath=getServicePath(path);
    if(serpath==null){
    serpath=ServletActionContext.getServletContext().getRealPath("/");
    File pathFile=createFilePath(serpath+pathConvert(path,'/','\\'));
    return pathFile.getAbsolutePath();
    }
    return serpath;
    }

    /**
     * 路径字符串转换
     * 
     * 将path中的字符c1转换为c2
     * 
     * @param path
     * @param c1
     * @param c2
     * @return
     */
    public static String pathConvert(String path,char c1,char c2)
    {
    return path.replace(c1,c2);
    }

    public static void fileMoveTo(File srcFile,File toFile)
    {
    if(srcFile!=null&&toFile!=null)
    {
    createFilePath(toFile.getParent());
    srcFile.renameTo(toFile);
    srcFile.delete();
    }
    }

    /**
     * 创建文件路径
     * @param file
     */
    public static void createFilePath(File file)
    {
    if(!file.exists())
    file.mkdirs();
    }

    public static File createFilePath(String path)
    {
    File file=null;
    if(!StringUtil.IsEmpty(path))
    {
    file=new File(path);
    createFilePath(file);
    }
    return file;
    }

    }