Javac_MyLife 发表于 2013-1-15 02:42:48

复习Struts1.3.8的单文件上传

好久没用Struts1了这几天终于闲了下来 翻开了 以前的笔记和代码有种写写S1代码的冲动于是先搞了一个S1的单文件上传 练练手上代码先

首先写了一个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
privateFormFile file ;public FormFile getFile() {return file;}public void setFile(FormFile file) {this.file = file;}

然后写uploadAction
public class UploadAction extends Action {@Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {// TODO Auto-generated method stubFileForm 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;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>

到此 一个最简单的文件上传就搞定了 但是还没有解决中文的乱码问题 首先想到了配置中央处理器 。

程序虽然简单 但是好久没有用了 有些淡忘 果然 最淡的墨水也胜过最强的记忆
页: [1]
查看完整版本: 复习Struts1.3.8的单文件上传