jsp中将textarea的内容保存到文本文件,怎么弄
时间:2011-11-13
来源:互联网
jsp中将textarea的内容保存到文本文件,我想手动指定要保持的文件路径和文件名,像另存为效果一样,怎么做,我用得是struts1
作者: jszjgzxl 发布时间: 2011-11-13
直接创建一个文件 然后写入吧
作者: mengxiangyue 发布时间: 2011-11-13
要实现你要的功能,代码有点多。。
告诉一下流程:
1. 页面提交textarea的内容
2. action接收内容,然后写一个方法将这些内容写入一个txt文件,存放在temp临时目录
3. 写一个文件下载的类,将2步生成的文件名下载下来。
4. 另存为效果是
//弹出下载对话框的关键代码
response.setContentType("application/x-download");
response.setHeader("Content-Disposition","attachment;filename="+ filename);
这段代码弹出来的。。
给你贴一段下载的代码:
/**
* 下载服务器中的txt文件
* @param filePath
* @param filename
* @param response
* @throws Exception
*/
public static void download(File filePath, String filename, HttpServletResponse response) throws Exception{
// TODO Auto-generated method stub
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
//File uploadFile = new File(filePath);
File uploadFile = filePath;
fis = new FileInputStream(uploadFile);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
filename = URLEncoder.encode(filename, "GBK");
//弹出下载对话框的关键代码
response.setContentType("application/x-download");
response.setHeader("Content-Disposition","attachment;filename="+ filename);
int bytesRead = 0;
//都是用输入流进行先读,然后用输出流去写,用的是缓冲输入输出流
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
try{
bos.write(buffer, 0, bytesRead);
}catch(Exception e){}
}
try{bos.flush();}catch(Exception e){}
try{bos.close();}catch(Exception e){}
try{bis.close();}catch(Exception e){}
try{fos.close();}catch(Exception e){}
try{fis.close();}catch(Exception e){}
}
告诉一下流程:
1. 页面提交textarea的内容
2. action接收内容,然后写一个方法将这些内容写入一个txt文件,存放在temp临时目录
3. 写一个文件下载的类,将2步生成的文件名下载下来。
4. 另存为效果是
//弹出下载对话框的关键代码
response.setContentType("application/x-download");
response.setHeader("Content-Disposition","attachment;filename="+ filename);
这段代码弹出来的。。
给你贴一段下载的代码:
/**
* 下载服务器中的txt文件
* @param filePath
* @param filename
* @param response
* @throws Exception
*/
public static void download(File filePath, String filename, HttpServletResponse response) throws Exception{
// TODO Auto-generated method stub
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null;
//File uploadFile = new File(filePath);
File uploadFile = filePath;
fis = new FileInputStream(uploadFile);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
filename = URLEncoder.encode(filename, "GBK");
//弹出下载对话框的关键代码
response.setContentType("application/x-download");
response.setHeader("Content-Disposition","attachment;filename="+ filename);
int bytesRead = 0;
//都是用输入流进行先读,然后用输出流去写,用的是缓冲输入输出流
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
try{
bos.write(buffer, 0, bytesRead);
}catch(Exception e){}
}
try{bos.flush();}catch(Exception e){}
try{bos.close();}catch(Exception e){}
try{bis.close();}catch(Exception e){}
try{fos.close();}catch(Exception e){}
try{fis.close();}catch(Exception e){}
}
作者: kouyiSC 发布时间: 2011-11-13
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28