首页 > 代码库 > WEB文件上传下载过程简介(基于Struts2)
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();
WEB文件上传下载过程简介(基于Struts2)