各位,请教:程序生成一个excel存在服务器一个目录中,环境是weblogic+linux做的,现在需要下载这个xls文件,路径是对的,比如 http://127.0.0.1:8080/file/20130821.xls,现在想要把它写在一个a标签里面,用户一点击,就弹出保存的对话框,如果是XP系统就问你:打开  保存  取消,如果是win7就如图这样:
就是系统带的这样保存的功能。但是,现在遇到的问题是,浏览器不提示保存,是在浏览器里打开了,而且是乱码(我怀疑是weblogic的原因)。目标另存为的话文件的格式是html的,修改成xls可以正常显示。现在想请教大家,服务器上的xls文件需要下载到本地,有什么好的招?谢谢excel下载word下载目标另存为

解决方案 »

  1.   


    有个选项是全部文件,然后还是html后缀
      

  2.   

    代码:
    /**
     * 下载管理器
     * @description
     */
    public class DownLoadAction extends BaseAction {
    private static final long serialVersionUID = 1L;
    private String fileName = null;
    private InputStream  tempStream = null;
    private byte[] bytes = null;
    private String filePath = null; //文件地址
    private Integer id=null;
    @Resource
    private IAnnexService annexService=null;

    /**
     *  查看附件
     * @return
     */
    public InputStream getInputStream() throws Exception {
    Annex annex=annexService.find(Annex.class, id);
    File file = new File(getFileBasePath() + annex.getUrl());
    File file2=new File(getFileBasePath() + File.separator+"download"+File.separator+DecoderUtil.UtfDecoder(annex.getName()));
    file2.getParentFile().mkdir();
    FileUtil.copyFile(file, file2);
    try {
    this.setFileName(new String(file2.getName().getBytes(), "ISO8859-1"));
    tempStream = new java.io.FileInputStream(file2);//从系统磁盘文件读取数据
    bytes = new byte[tempStream.available()];
    if(tempStream != null) {
    tempStream.read(bytes);
    }
    tempStream.close();
    return new ByteArrayInputStream(bytes);
    }  catch(Exception e) {
    return null;
    }finally{
    FileUtil.deleteFile(file2.toString());
    }
    }
      

  3.   

    第一需要contentType正确,第二需要contentDisposition是attachment而不是inline
      

  4.   

    response.setHeader("content-disposition","attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
      

  5.   

    1.下载页面有没有设置MIME
    2.下载页面有没有设置文件名
    浏览器有的根据MIME判断文件类型,有的根据你提供的文件名的扩展名来判断文件类型
      

  6.   

    9楼正解,需要在web.xml里配置每种文件打开的方式。
      

  7.   


    你好,这句话写在哪里?我是html页面,一个a链接到文件的,谢谢。
      

  8.   

    你好,配置的web.xml是哪里的文件?是weblogic里面的还是项目web-inf文件夹下的?谢谢
      

  9.   


    你好,这句话写在哪里?我是html页面,一个a链接到文件的,谢谢。
    这么做下载的?