java下载文件

 前台页面

<a href="downloadAttachment.do?id=$!attachment.id">下载</a>

后台action

public String downloads(){
        try{
            String id = this.getAttachmentid();
            Attachment att = (Attachment) bean.findObject(Attachment.class, Long.parseLong(id));
            String filename = URLEncoder.encode(att.getFilename(), "UTF-8"); //设置转码,如果乱码的情况
            downLoadFile(att.getFilepath(),filename,this.getResponse()); //下载附件
        }catch(Exception e ){
            e.printStackTrace();
        }
        return null;
    }

 struts配置:

             <action name="downloadAttachment" class="*Action" method="downloadAttachment">
			<result name="success" type="stream"> 
		        <param name="inputName">downloadFile</param> 
		        <param name="bufferSize">1024</param> 
		    </result> 
		</action>

后台工具类:

public static void downLoadFile(String path,String fileName,HttpServletResponse response){        
        try{
            if(!"".equals(path)){                
                File file=new File(ServletActionContext.getRequest().getRealPath("/")+path);//构造要下载的文件    
                if(file.exists()){
                    InputStream ins=new FileInputStream(file);//构造一个读取文件的IO流对象
                    BufferedInputStream bins=new BufferedInputStream(ins);//放到缓冲流里面
                    OutputStream outs=response.getOutputStream();//获取文件输出IO流
                    BufferedOutputStream bouts=new BufferedOutputStream(outs);
                    response.setContentType(getContentType(fileName));//设置response内容的类型
                    response.setHeader("Content-disposition","attachment;filename="+fileName);//设置头部信息
                    int bytesRead = 0;
                    byte[] buffer = new byte[8192];
                    //开始向网络传输文件流
                    while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
                        bouts.write(buffer, 0, bytesRead);
                    }
                    bouts.flush();//这里一定要调用flush()方法
                    ins.close();
                    bins.close();
                    outs.close();
                    bouts.close();
                }else{
                    System.out.println("下载的文件不存在");
                }
            }else{
                System.out.println("下载文件时参数错误");
            }

        }catch(Exception e ){
            e.printStackTrace();
        }
    }
    
    /*
    设置文件类型
    */
     private static String getContentType(String fileName) {
      String fileNameTmp = fileName.toLowerCase();
      String ret = "";
      if (fileNameTmp.endsWith("txt")) {
       ret = "text/plain";
      }
      if (fileNameTmp.endsWith("gif")) {
       ret = "image/gif";
      }
      if (fileNameTmp.endsWith("jpg")) {
       ret = "image/jpeg";
      }
      if (fileNameTmp.endsWith("jpeg")) {
       ret = "image/jpeg";
      }
      if (fileNameTmp.endsWith("jpe")) {
       ret = "image/jpeg";
      }
      if (fileNameTmp.endsWith("zip")) {
       ret = "application/zip";
      }
      if (fileNameTmp.endsWith("rar")) {
       ret = "application/rar";
      }
      if (fileNameTmp.endsWith("doc")) {
       ret = "application/msword";
      }
      if (fileNameTmp.endsWith("ppt")) {
       ret = "application/vnd.ms-powerpoint";
      }
      if (fileNameTmp.endsWith("xls")) {
       ret = "application/vnd.ms-excel";
      }
      if (fileNameTmp.endsWith("html")) {
       ret = "text/html";
      }
      if (fileNameTmp.endsWith("htm")) {
       ret = "text/html";
      }
      if (fileNameTmp.endsWith("tif")) {
       ret = "image/tiff";
      }
      if (fileNameTmp.endsWith("tiff")) {
       ret = "image/tiff";
      }
      if (fileNameTmp.endsWith("pdf")) {
       ret = "application/pdf";
      }
      return ret;
     }
原文地址:https://www.cnblogs.com/estellez/p/3941845.html