怎么样将一个字符串中的内容中内容写入到流中,请高手举个例子! 比如:将字符串中的内容写入到流中,让客户端下载,但是在服务器端不形成文件,直接将流写到客户端.请给个例子!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 好像每本介绍关于java socket编程的书上面都有例子的 很简单,碰巧我也写了一个,导成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(); } 太好了,真是多谢,我就是想要这样的例子,再问一下,比如说,我有个string要写入out,可以直接out.write(string)吗?对这方面不是很熟,现在时间紧又不想研究,所以只有靠你了. 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; } writeStatus写入readStatus读出 Swing事件派发线程的疑惑 使用LinkedList的时候没法取出数据 高手帮我看两个后台都是什么意思(求助) 高手:留步。请问java开发的Application中,像eclipse一样,不管是不是焦点,都能使用快捷键,实现原理机制是怎么怎么样的?谢谢。 用一个java程序去监听另外一个java程序是否关闭该怎么实现? jdbc如何更改表结构 在线等! [求助]求助锚点问题以及地图循环 数据类型转换 如何生成可执行文件啊!十万火急啊! 如何将.zip文件改为.jar文件? java初学者一道基本上看不懂的题 数组看晕了
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();
}
{
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;
}
readStatus读出