如何用jsp打开一个名为ww.txt的文件,并在文件内写入“你好,欢迎来如jsp”内容,小弟初学,在现等
解决方案 »
- 讨论Java web开发中用到的各种小技术,或者说模块技术吧!做过总计,讨论讨论到底那个好用些。。。
- struts2和struts 比较 各位兄弟们帮下忙
- 大家认为SharePoint有多少发展前途?
- 图片上传的一些问题
- 上传大文件的时候,如何只显示一个静态页面,上传的页面不被用户看到
- 同一目录下两个jsp文件,一个正常,一个访问时“404 Not Found”,怎么回事?晕死
- 求JSP中,不用beans,分别显示年、月、日、时、分、秒(实时)的方法
- 请问,在jsp中如何将下划线去了??
- 那位大哥知道sina和163得了聊天室是如何实现的,使用了什么技术,请简单介绍一下
- 请问Jbuilder5和ViusalAge for Java哪个开发工具更好
- 初学struts遇到问题
- 肯请高手帮我程序提优化,规范一下,谢谢
if ( order.getContract_attached_file_URL() != null )
order_attached_file_url = get_Url ;
else
order_attached_file_url = "" ;
if ( order_attached_file_url != null && ! order_attached_file_url.trim().equals( "" ) ) {
int pos = order_attached_file_url.lastIndexOf(".") ;
if ( pos > -1 )
tmp_ext = order_attached_file_url.substring( pos ) ;
else
tmp_ext = ".txt" ;
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
Calendar now = Calendar.getInstance(); //get CurrDate
tmp_fn = "Order" + order_id + "_" + formatter.format(now.getTime() ) ;
tmp_fn = tmp_fn + tmp_ext ;
tmpLink = "/ecatic/temp/" + tmp_fn ;
tmpRealPathFile = request.getRealPath( tmpLink ) ;
resultAttachFile = ordersheet.get_orderHeader().getOrderAttach( conn, tmpRealPathFile ,get_Url ,order_id) ; //In the server, file name is : tmpRealPathFile
// open the temp file, and put data to out
File file = new File( tmpRealPathFile );
response.setContentType("application/octet-stream");
// response.setContentType("application/vnd.ms-excel") ; response.setHeader("Content-disposition" , "attachment; filename=" + file.getName() );
// response.setHeader("content-disposition","attachment;filename=\""+ FileName +".xls\""); BufferedInputStream bis = null;
BufferedOutputStream bos = null; bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(response.getOutputStream());
bos.flush();
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesRead);
}
bos.close() ;
bis.close() ; file.delete() ;
{
if (content == null || fileName == null || fileName.trim().length()==0)
{
throw new IllegalArgumentException("- [error]: 文件内容和文件名一个都不能少!");
}
BufferedReader reader = null;
BufferedWriter writer = null;
try
{
String filePath = "c:/" + fileName;
System.out.println(filePath);
File file = new File(filePath);
if (!file.exists())
{
file.createNewFile();
}
reader = new BufferedReader(new StringReader(content));
writer = new BufferedWriter(new FileWriter(file));
String tempStr = null;
while((tempStr=reader.readLine()) != null )
{
writer.write(tempStr);
writer.flush();
writer.newLine();
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (reader != null)
{
reader.close();
}
if (writer != null)
{
writer.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
<%@ page import ="java.io.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<title>文件操作</title>
</head>
<body>
<h2>jsp 中对文件的操作,其实跟java 一样</h2>
<br>
<h4>你先建立一个文件,放在%TOMCAT_HOME%\webapps\ROOT文件夹下。比如<br>
test.txt,输入如下内容:<br>
<div align="center">测试一下读取文件操作</div>
<br>
<%
String filename="test.txt";
String tempPath=request.getRealPath("/");
String file=tempPath+"\\"+filename;
File testFile=new File(file);
BufferedReader fin=new BufferedReader(new FileReader(testFile));
String str=null;
out.println("文件内容如下:<br>");
while((str=fin.readLine())!=null)
{
out.println(str+"<br>");
}
out.flush();
fin.close();
%>