WEB文件上传下载过程简介(基于Struts2)
WEB文件上传过程简介(基于Struts2),页面上传、Action收取、解析;文件导出
文件处理过程,需要注意对文件的定期清理,避免磁盘占用过多;
1. jsp页面:
<s:file required="true" name ="myFile" theme="zx2"/>
<s:form action ="myfile.action" name="form" method ="POST" enctype ="multipart/form-data"
onsubmit="return checkSubmit();">
2. Action:
private File myFile; // 设置get set方法
File tmpfile = new File(path + File.separator + imageFileName);
UserFile.copy(myFile, tmpfile); // 将文件复制一份
myFile.delete(); // 删除上传的临时文件
BufferedReader reader = new BufferedReader(new FileReader(tmpfile));
int count=0;
while( (line = checkreader.readLine()) != null)
{
line = line.trim(); // 读取文件
3. 导出文件,文件流方式输出:
DataOutputStream os = null;
os = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(fileName,true)));
OutputStreamWriter write = new OutputStreamWriter(os, "UTF-8");
BufferedWriter writer = new BufferedWriter(write);
StringBuffer buf = new StringBuffer();
writer.write(buf.toString());
writer.write("\r\n");
buf.delete(0, buf.length());
writer.close();
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。