欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jsp,java下载附件

程序员文章站 2024-01-29 17:33:10
...
jsp下载word,excel
<iframe src="fileName" name="realName%" id="showFileId" width="100%" height="100%"></iframe>

src:文件路径

java后台下载附件代码
 /**
    * @param response  response对象
     * @param fullName  文件全路径名称
     * @param disName   下载的显示文件
     * @param downType  下载类型 attachment(打开需要两次), file(类似inline) 
     */
  File dbFile = new File(fullName);
  FileInputStream fileIn = new FileInputStream(dbFile);
    	            
  String contentType;
  contentType = "application/msword";
  response.setContentType(contentType);
  response.setHeader("Content-Disposition", " filename=report.xls");

  StringBuffer header = null;
  if (downType != null && !"".equals(downType)) {
     header = new StringBuffer(downType + "; filename=");                
   } else {
     header = new StringBuffer("file; filename=");
   }            
   //如果系统是运行在中文平台下,则将文件名转码为ISO8859_1的编码
    disName = new String(disName.getBytes("GBK"), "ISO8859_1");
   header.append(disName);
   response.setHeader("Content-Disposition", header.toString());
   byte[] buffer = new byte[1024 * 512];
   while (true) {
         int bytes = fileIn.read(buffer);
    	if (bytes == -1) {
    	      break;
    	}
       response.getOutputStream().write(buffer, 0, bytes);
   }
   response.getOutputStream().flush();
   response.getOutputStream().close();
   fileIn.close();
   } catch (Exception e) {
      e.printStackTrace();
   }
相关标签: jsp java word