`
Javac_MyLife
  • 浏览: 9172 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

复习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
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>


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

程序虽然简单 但是好久没有用了 有些淡忘 果然 最淡的墨水也胜过最强的记忆 
2
0
分享到:
评论
2 楼 Javac_MyLife 2010-01-20  
sbear 写道
正好想找这方面的

1 楼 sbear 2010-01-20  
正好想找这方面的

相关推荐

Global site tag (gtag.js) - Google Analytics