好久没用Struts1了 这几天终于闲了下来 翻开了 以前的笔记和代码 有种写写S1代码的冲动 于是先搞了一个S1的单文件上传 练练手 上代码先
首先写了一个upload.jsp
然后定义了一个简单的UploadForm
然后写uploadAction
然后是在struts-config.xml里面的配置项
到此 一个最简单的文件上传就搞定了 但是还没有解决中文的乱码问题 首先想到了配置中央处理器 。
程序虽然简单 但是好久没有用了 有些淡忘 果然 最淡的墨水也胜过最强的记忆
首先写了一个upload.jsp
<h1>文件上传</h1>
<html:form action="uploadAction" method="post" enctype="multipart/form-data">
<html:file property="file"/>
<html:submit value="上传"></html:submit>
</html:form>然后定义了一个简单的UploadForm
private FormFile file ;
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}然后写uploadAction
public class UploadAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
FileForm fileForm = (FileForm) form;
String path = request.getSession().getServletContext().getRealPath("/upload");
FormFile file = fileForm.getFile();
InputStream stream = file.getInputStream();
File pathFile = new File (path);
if(!pathFile .exists() ){
pathFile.mkdirs();
}
FileOutputStream os = new FileOutputStream(path+"/"+file.getFileName());
byte buff[] = new byte[1024*4];
int len = 0 ;
while ((len = stream.read(buff))!=-1){
os.write(buff, 0, len);
}
stream.close();
os.close();
return null;
}
}
然后是在struts-config.xml里面的配置项
<form-beans>
<form-bean name="uploadForm" type="com.blacklee.form.FileForm"></form-bean>
</form-beans>
<action path="/uploadAction" name="uploadForm" type="com.blacklee.action.UploadAction"></action>
到此 一个最简单的文件上传就搞定了 但是还没有解决中文的乱码问题 首先想到了配置中央处理器 。
程序虽然简单 但是好久没有用了 有些淡忘 果然 最淡的墨水也胜过最强的记忆
本文通过一个简单的示例介绍了如何使用Struts1框架实现单文件上传功能,包括前端表单设计、后端Action处理及配置文件设置,并提到了中文乱码问题。
502

被折叠的 条评论
为什么被折叠?



