Topic: servlet的一个下载程序的问题

  Print this page

1.servlet的一个下载程序的问题 Copy to clipboard
Posted by: zhb
Posted on: 2006-08-16 13:09

<html>
<head></head>
<body>
<a href="download?id=111">hello</a><br>
</body>
</html>

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class Down1 extends HttpServlet{
  
public void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException,
java.io.IOException
  {
    String id=req.getParameter("id");
    File file=null;
    InputStream is=null;
    int fileSize=0;
    String fileName=null;
    
    //从硬盘下载
    if(id.equals("111")){
      file=new File("F:\\111.txt");
      is=new FileInputStream(file);
      fileName=file.getName();
      fileSize=(int)file.length();
    }
    resp.setContentType("application/x-msdownload");
    String str="attachment; filename="+fileName;
    resp.setHeader("Content-Disposition",str);
    resp.setContentLength(fileSize);
    
    ServletOutputStream sos=resp.getOutputStream();
    int len=0;
    byte[] data=new byte[2048];
    while((len=is.read(data))!=-1){
      sos.write(data,0,len);
    }
    is.close();
    sos.close();
  }
  public void doPost(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException,
java.io.IOException
  {
   doGet(req,resp);
  }
}

在tomcat中部署 heep://localhost:8080/jsp/down.html 点hello链接 提示111.txt 打开下载取消 但是点打开或下载后没有反应 麻烦给看下 111.txt存在 路径也没问题

2.Re:servlet的一个下载程序的问题 [Re: zhb] Copy to clipboard
Posted by: zhb
Posted on: 2006-08-16 13:11

http...笔误。。。

3.Re:servlet的一个下载程序的问题 [Re: zhb] Copy to clipboard
Posted by: zhb
Posted on: 2006-08-16 15:34

<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.io.*" %>
<%
String fileName = "111.txt".toString();
//读到流中
InputStream inStream=new FileInputStream("f:/111.txt");
//设置输出的格式
//response.reset();
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition","attachment; filename=" + fileName);
//循环取出流中的数据
byte[] b = new byte[100];
int len;
OutputStream aaa=response.getOutputStream();
while((len=inStream.readBeer) >0){

aaa.write(b,0,len);
}
aaa.close();
inStream.close();
%>

用基本相同的jsp 保存或打开正常
谁能告诉我这是什么原因啊....区别在哪????


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923