比如:将字符串中的内容写入到流中,让客户端下载,但是在服务器端不形成文件,直接将流写到客户端.
请给个例子!!

解决方案 »

  1.   

    好像每本介绍关于java socket编程的书上面都有例子的
      

  2.   

    很简单,碰巧我也写了一个,导成excel的,你可以借鉴。
    String filename = "excel.xls";
        httpServletResponse.setContentType("application/octet-stream");
        httpServletResponse.setHeader("Content-Disposition",
                                      "attachment;filename=\"" + filename + "\";");    try {
          OutputStream out = httpServletResponse.getOutputStream();
          DemoService.getInstance().inExcel(out); //把页面取得的流传入业务类,在业务类中将out写入内容即可
          或者你直接在这里往out里面写东西也一样。上面只不过是我的例子。    }
        catch (Exception e) {
          e.printStackTrace();
        }
      

  3.   

    太好了,真是多谢,我就是想要这样的例子,再问一下,比如说,我有个string要写入out,可以直接out.write(string)吗?对这方面不是很熟,现在时间紧又不想研究,所以只有靠你了.
      

  4.   

    public void writeStatus(String szFileName,String content)
    {
    try
    {
    File file = new File(szFileName);
    DataOutputStream dos=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
            dos.writeBytes(content+"\n&^");
            dos.close();
    }
    catch(Exception e){
    }

        public String[] readStatus(String szFileName)
        {
         String sz_FileContent = "";
         String[] arrReturn = null;
         BufferedInputStream bis = null;
         File file =null;
         try
         {
         file = new File(szFileName);
            bis = new BufferedInputStream(
         new FileInputStream(file));
         byte[] buff = new byte[2048];
            int bytesRead;
            while(-1 != (bytesRead = bis.read(buff, 0, buff.length)))
            {
                sz_FileContent += new String(buff,"ISO8859_1");
            }
            StringTokenizer Tmp = new StringTokenizer(sz_FileContent, "\r\n");
            int rnum = Tmp.countTokens();
            arrReturn = new String[rnum];
            for(int i = 0; i < rnum; i++)
            {
             arrReturn[i] = Tmp.nextToken();
            }
        }
        catch(Exception e)
    {}
        finally
        {
         try
         {
         if (bis != null) bis.close();
         }
         catch(Exception e){}
        }
            return arrReturn;
        }
      

  5.   

    writeStatus写入
    readStatus读出