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

时间:2021-08-27 10:05:27

在开发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);
这样在执行以上代码时,会弹出对话框,用户就可以选择保存路径、文件名什么的了。