一个在B/S中弹出保存文件对话框的程序

在开发B/S模式时,遇到这样的一个问题,客户端需要执行一个程序,将客户端提交的相关信息下载到本地。我的思路是这样的,先在服务器端生成个文件,然后下载到客户端,删除。功能是实现了,但我总感觉有些勉强,又不知道有什么好方法可以解决,下面是我的程序:
package chartreport;

import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
import java.net.*;

public class JBFileSave {
  /**
   *
   * @param fileName String
   * @param response HttpServletResponse
   */
  public void downloadAttachment(String fileOnDisk, String fileName,
                                 HttpServletResponse response) {
//    String filename = "test.txt";
    //System.out.println("...............fileOnDisk = "+fileOnDisk);
    //System.out.println("...............fileName = "+fileName);
    //转换成硬盘里实际地址
    InputStream input = null;
    OutputStream output = null;
    File f = null;
    try {
      try {
        input = new FileInputStream(fileOnDisk);
        f = new File(fileOnDisk);
      }
      catch (IOException e) {
        //System.out.println("can not get attchment on disk");
      }

      byte[] buffer = getBytes(input);
      input.close();
      input = null;

      output = response.getOutputStream();

      response.setContentType("application/octet-stream");
      response.setHeader("Location", GBtoISO(fileName));
      response.setHeader("Content-Disposition",
                         "attachment; filename=/"" + GBtoISO(fileName) + "/"");
      response.setContentLength( (int) f.length());
      output.write(buffer);
      output.flush();
      output.close();
      output = null;
    }
    catch (IOException e) {
      //System.out.println("error download attachment.");
    }
    finally {
      if (input != null) {
        try {
          input.close();
        }
        catch (IOException ex) {}
      }
      if (output != null) {
        try {
          output.close();
        }
        catch (IOException ex) {}
      }
    }
  }

  /**
   *
   * @param inputStream InputStream
   * @throws IOException
   * @return byte[]
   */
  public byte[] getBytes(InputStream inputStream) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
        1024);
    byte[] block = new byte[512];
    while (true) {
      int readLength = inputStream.read(block);
      if (readLength == -1) {
        break;
      }
      byteArrayOutputStream.write(block, 0, readLength);
    }
    byte[] retValue = byteArrayOutputStream.toByteArray();
    byteArrayOutputStream.close();
    return retValue;
  }
  /**
   *
   * @param filename String
   * @throws Exception
   */
  public void deleteFile(String filename) throws Exception{
    File file = new File(filename);
    if(file.exists()){
      file.delete();
    }
  }
  public String changeCode(String str){
    String result = null;
    byte temp[];
    try {
      temp = str.getBytes("gb2312");
      result=new String(temp);
    }
    catch (java.io.UnsupportedEncodingException ex) {
      //System.out.println(ex.toString());
    }
    return result;
  }
  public String GBtoISO(String gb){
    String iso;
    try
    {
      if (gb.equals("") || gb == null)
      {
        return "";
      }
      else
      {
          gb = gb.trim();
          iso = new String(gb.getBytes(),"iso8859-1");
          return iso;
      }
    }
    catch(Exception e)
    {
      System.err.print("编码转换错误:" + e.getMessage());
      return "";
    }
  }
}
/***********************************************/
//下面是调用程序
jbFileSave.downloadAttachment(filePath+excelFileName,excelFileName,httpServletResponse);
这样在执行以上代码时,会弹出对话框,用户就可以选择保存路径、文件名什么的了。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
sysinternals程序和部分程序源码 Sysinternals_Source/ AccessEnumSource filemon434 NtfsInfoSource TokenmonSource AdRestoreSource fmifs procfeatures TVcache AutologonSource FundeleteSource regmon435 VcMonSource CacheSetSource JunctionSource SDeleteSource VxDMonSource Ctrl2CapSource NetstatpSource SecDemoSource DiskExt NewSidSource ShareEnumSource SysinternalsSuite/ accesschk.exe Diskmnt.hlp PHYSMEM.EXE Reghide.exe AccessEnum.exe Diskmon.exe pipelist.exe regjump.exe AdExplorer.chm DISKMON.HLP PORTMON.CNT Regmon.exe ADExplorer.exe DiskView.exe portmon.exe REGMON.HLP ADInsight.chm DMON.SYS PORTMON.HLP RootkitRevealer.chm ADInsight.exe du.exe procexp.chm RootkitRevealer.exe adrestore.exe efsdump.exe procexp.exe sdelete.exe Autologon.exe Eula.txt ProcFeatures.exe ShareEnum.exe autoruns.chm Filemon.exe procmon.chm ShellRunas.exe autoruns.exe FILEMON.HLP Procmon.exe sigcheck.exe autorunsc.exe handle.exe psexec.exe streams.exe Bginfo.exe hex2dec.exe psfile.exe strings.exe Cacheset.exe junction.exe psgetsid.exe sync.exe Clockres.exe ldmdump.exe Psinfo.exe tcpvcon.exe Contig.exe Listdlls.exe pskill.exe tcpview.chm Coreinfo.exe livekd.exe pslist.exe Tcpview.exe ctrl2cap.amd.sys LoadOrd.exe psloggedon.exe TCPVIEW.HLP ctrl2cap.exe logonsessions.exe psloglist.exe Volumeid.exe ctrl2cap.nt4.sys movefile.exe pspasswd.exe whois.exe ctrl2cap.nt5.sys newsid.exe psservice.exe Winobj.exe dbgview.chm ntfsinfo.exe psshutdown.exe WINOBJ.HLP Dbgview.exe pagedfrg.exe pssuspend.exe ZoomIt.exe Desktops.exe pagedfrg.hlp Pstools.chm diskext.exe pdh.dll psversion.txt Diskmnt.exe pendmoves.exe RegDelNull.exe
转自看雪的CCDeath 【文章标题】: WinMount虚拟磁盘深入研究(-)之filedisk源代码详细分析 【下载地址】: 自己搜索下载 【作者声明】: 只是感兴趣,没有其他目的。失误之处敬请诸位大侠赐教! -------------------------------------------------------------------------------- 【详细过程】 我的驱动入门三终结版,还再学习中。由于个人也有些事情要处理,研究起刘涛涛WinMount的虚拟 磁盘,而且这方面的书籍,貌似乎没见过,只有傻傻的几K代码存在,没注释什么的,整体框架也没说。 虚拟光驱用实现文件来模拟磁盘的原理,是文件系统驱动程序。 把filedisk驱动安装,查看install.txt文件。 1.Copy the driver (filedisk.sys) to %systemroot%\system32\drivers\. 2.Import filedisk.reg to the Registry. 3.Reboot. 4.Use the program filedisk.exe to mount/umount files, for an example of use see the file example.txt. 可以不用重起机子的方法,找一个动载加载驱动工具:DriverMonitor不错了。然后在“开始菜单”-> "运行"输入 "net start filedisk" 现:“ 请求的服务已经启动”。这个必须得成功才行哦。 接下来注意点。cmd后 现这个目录 C:\Documents and Settings \Administrator>,在接下来敲入 filedisk /mount 0 c:\temp\filedisk.img 8M f: C:\Documents and Settings \Administrator>filedisk /mount 0 c:\temp\filedisk.img 8M r: 回车一下。 现"FileDisk:系统找不到指定路径" 。原因就在这 "c:\temp\filedisk.img" 中的C:\temp要这个目录才行。至于 filedisk.img不是必须,会自动创建。 如果有现"FileDisk:函数不正确" 中的“filedisk /mount 0 ”中"0"代号已经被使用。可以改为"1". 查看一下,结果就一个还未格式化 8M R磁盘,查看C:\temp下生成一个filedisk.img也8M。想卸载 掉"filedisk /umount r:". 还可以创建很大的虚拟磁盘,你把"8M"改换其他的就是了。 以上如果都没现结果,基本上就没兴趣继续研究下去了,我看到了很多人初学filedisk都遇到以上这 些问题(包括我在内) ,把我折腾了半天。 先来分析驱动层代码,后来分析应用层代码。更详细的请看附件里的源代码。我对代码工程方式重新布 局,用起来更方便。 ===================================//先来分析驱动层代码 1.对filedisk.h进行分析 #define FILE_DEVICE_FILE_DISK 0x8000//用户定义范围0x8000~ #define IOCTL_FILE_DISK_OPEN_FILE CTL_CODE(FILE_DEVICE_FILE_DISK, 0x800, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS) #define IOCTL_FILE_DISK_CLOSE_FILE CTL_CODE(FILE_DEVICE_FILE_DISK, 0x801, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS) #define IOCTL_FILE_DISK_QUERY_FILE CTL_CODE(FILE_DEVICE_FILE_DISK, 0x802, METHOD_BUFFERED, FILE_READ_ACCESS) typedef struct _OPEN_FILE_INFORMATION { LARGE_INTEGER FileSize;//文件大小 BOOLEAN ReadOnly;//只读属性 USHORT Fil

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值