是这样的:实际存储在磁盘 项目/upload/resource/下的所有文件名 是由 上传时用 new Date()设置的(为了防止文件名重复)、而数据库里保存了 文件在磁盘上的 文件名(pathName) 跟实际文件名(trueName)
  
  例如:文件夹下的一个文件 /upload/resource/ 201110231943.txt   它在数据库里实际存着 eclipse快捷键大全.txt 跟 201110231943.txt   在下载文件时,如果不做设置,下载的文件就是 201110231943.txt 了,现在我想在 Action execute()方法里把 被下载的文件名  改回 eclipse快捷键大全.txt
   但我改来改去  弄的下载时  下载框只 显示文件 为downloadFile    连个格式 .txt 都没掉了,,更别说 eclipse快捷键大全  我的 struts.xml Action 代码如下:

解决方案 »

  1.   

    struts.xml
    <!--.......下载文件.......-->
           <action name="downloadFile" class="fileDownloadAction" method="download">
           <result name="success" type="stream">
          
           <!-- 下载文件类型定义  application/octet-stream表示无限制 -->
           <param name="contentType">application/octet-stream;charset=ISO8859-1</param>
          
           <!-- 使用经过转码的文件名作为下载文件名,downloadChineseFileName属性对应action类中的方法 getDownloadChineseFileName() -->
           <!-- 下载文件处理方法 -->
           <param name="contentDisposition">attachment,filename="${downloadChineseFileName}"</param>
          
           <!-- 下载文件输出流定义 -->
           <param name="inputName">downloadFile</param>
          
           <!-- 下载文件的缓冲大小 -->
           <param name="bufferSize">4096</param>
          
           </result>
           </action>
      

  2.   

    Action 代码:
    package com.ycx.action;import java.io.InputStream;
    import java.io.UnsupportedEncodingException;
    import java.util.List;import javax.annotation.Resource;import org.apache.struts2.ServletActionContext;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;import com.opensymphony.xwork2.ActionSupport;
    import com.ycx.dto.UploadDTO;
    import com.ycx.service.UploadService;@Component(value="fileDownloadAction")
    @Scope(value="prototype")
    public class FileDownloadAction extends ActionSupport{





    private UploadService uploadService;


    //1.下面三个属性在  下载文件时用到
    //要下载文件的 存放路径
    private final static String downloadFILEPATH = "/upload/resource/";
    //文件实际名 参数变量
    private String fileName;
    //文件在磁盘上的名字
    private String pathName;
    //文件的 id (在数据库里的id)
    private String id;

    private String downloadChineseFileName;









     //从下载文件原始存放路径  读取得到文件输出流
    public InputStream getDownloadFile()
    {
    System.out.println("getDownloadFile()被调用...");
    System.out.println("下载路径="+downloadFILEPATH+this.getPathName());

    InputStream is=ServletActionContext.getServletContext().getResourceAsStream(downloadFILEPATH+this.getPathName());
    System.out.println("is="+is);

    return ServletActionContext.getServletContext().getResourceAsStream(downloadFILEPATH+this.getPathName());
    }

    //如果下载文件名为中文,进行字符编码转换
    public String getDownloadChineseFileName()
    {
    System.out.print("getDownloadChineseFileName()被调用...文件实际名字=");
    String n=this.getFileName();
    int a=n.indexOf(".");
    System.out.println(n.substring(0, a));

    String downloadChineseFileName = n.substring(0, a);

    try {
    downloadChineseFileName=new String(downloadChineseFileName.getBytes(),"ISO8859-1");
    System.out.println("中文解码后="+downloadChineseFileName);
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    }

    return downloadChineseFileName;
    }

    //执行下载文件 方法
    public String download()
    {
    String str_id = this.getId();
    int int_id=0;

    if(str_id==null||str_id.equals(""))
    {
    return "notFound";
    }

    try {
    int_id=Integer.parseInt(str_id);
    if(int_id<0)
    {
    int_id=0;
    }
    } catch (Exception e) {
    int_id=0;
    }

    if(int_id!=0)//客户端私自修改地址栏参数 id 内容时 则id 会变成 等于0
    {
    //files 集合里 只有一条记录
    file=this.uploadService.getUploadById(int_id);

    this.setPathName(file.getPathName());//从数据库里取出文件在磁盘上的名字
    //this.setFileName(file.getFileName());//取出文件实际名字
    System.out.println("filePathName="+this.getPathName());
    System.out.println("前台传过来的fileName="+this.getFileName());


    return SUCCESS;
    }

    //会自动调用 getDownloadFile() 和  getDownloadChineseFileName() 方法

    return "notFound";
    }

    public void setFiles(List<UploadDTO> files) {
    this.files = files;
    }
    public UploadService getUploadService() {
    return uploadService;
    } @Resource(name="uploadServiceImpl")
    public void setUploadService(UploadService uploadService) {
    this.uploadService = uploadService;
    } public String getFileName() {
    return fileName;
    } public void setFileName(String fileName) {
    this.fileName = fileName;
    }


    public void setFile(UploadDTO file) {
    this.file = file;
    }
    public String getPathName() {
    return pathName;
    }
    public void setPathName(String pathName) {
    this.pathName = pathName;
    }

    }