espnstarsky 发表于 2011-2-17 04:06

编程高手看看这个代码是讲什么?

代码实现:

public class DownServlet extends HttpServlet {

    private static final long serialVersionUID = 1332534778678L;

    private static final int byte_size = 1024;



    protected void service(HttpServletRequest request,

         HttpServletResponse response) throws ServletException, IOException {

       response.setContentLength(byte_size); // 输出到客户端的文件大小

       String fileName = "default.dmh";

       response.setContentType("text/plain");

       response.setCharacterEncoding("utf-8");

       response.setHeader("Content-disposition", "attachment; filename="

            + fileName);                              // 设置文件头

       String url = request.getParameter("url");

       URL fileUrl = new URL(url);

       HttpURLConnection httpCon = (HttpURLConnection) fileUrl

            .openConnection();

//   System.out.println("before range.......... "+httpCon.getResponseCode());

       String sProperty = "bytes=10-" + byte_size;      // -后面表示要取多少数据

       httpCon.setRequestProperty("RANGE", sProperty); // 设置偏移量

       System.out.println("after range.......... "+httpCon.getResponseCode());

       InputStream in = httpCon.getInputStream();

       System.out.println("after InputStream.......... "+httpCon.getResponseCode());

       DataInputStream dis = new DataInputStream(in); // 重新包装输入流

       OutputStream out = response.getOutputStream(); // 从httpUrlConnecction中获得输出流

       byte[] b = new byte;               // 读取多少

       dis.read(b);

       out.write(b);

       out.flush();

       out.close(); // 关闭输入输出流

       dis.close();

       in.close();

    }

}

espnstarsky 发表于 2011-2-17 12:57

顶!!!

czz2000 发表于 2011-2-17 15:24

顶!!!

lxq8432 发表于 2011-2-17 22:21

不懂哦,等高人来回答。

guess 发表于 2011-2-18 00:03

文件传输

1588848 发表于 2011-2-18 00:07

晕 你自己编译一下运行看看不就知道了啊:)

seaxp 发表于 2011-2-18 00:11

看不懂哦。。。。。。。。

mukai999 发表于 2011-2-18 10:04

等高人。

SAILOR-HB 发表于 2011-2-21 20:33

文件传输.支持断点续传
页: [1]
查看完整版本: 编程高手看看这个代码是讲什么?